github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/pkg/control/controldisplay/group_heading.go (about)

     1  package controldisplay
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/spf13/viper"
     8  	"github.com/turbot/go-kit/helpers"
     9  	"github.com/turbot/steampipe/pkg/constants"
    10  )
    11  
    12  type GroupHeadingRenderer struct {
    13  	title             string
    14  	severity          string
    15  	failedControls    int
    16  	totalControls     int
    17  	maxFailedControls int
    18  	maxTotalControls  int
    19  	// screen width
    20  	width  int
    21  	indent string
    22  }
    23  
    24  func NewGroupHeadingRenderer(title string, failed, total, maxFailed, maxTotal, width int, indent string) *GroupHeadingRenderer {
    25  	return &GroupHeadingRenderer{
    26  		title:             title,
    27  		failedControls:    failed,
    28  		totalControls:     total,
    29  		maxFailedControls: maxFailed,
    30  		maxTotalControls:  maxTotal,
    31  		width:             width,
    32  		indent:            indent,
    33  	}
    34  }
    35  
    36  func (r GroupHeadingRenderer) Render() string {
    37  	isDryRun := viper.GetBool(constants.ArgDryRun)
    38  
    39  	if r.width <= 0 {
    40  		// this should never happen, since the minimum width is set by the formatter
    41  		log.Printf("[WARN] group heading renderer has width of %d\n", r.width)
    42  		return ""
    43  	}
    44  
    45  	formattedIndent := fmt.Sprintf("%s", ControlColors.Indent(r.indent))
    46  	indentWidth := helpers.PrintableLength(formattedIndent)
    47  
    48  	// for a dry run we do not display the counters or graph
    49  	var severityString, counterString, graphString string
    50  	if !isDryRun {
    51  		severityString = NewSeverityRenderer(r.severity).Render()
    52  		counterString = NewCounterRenderer(
    53  			r.failedControls,
    54  			r.totalControls,
    55  			r.maxFailedControls,
    56  			r.maxTotalControls,
    57  			CounterRendererOptions{
    58  				AddLeadingSpace: true,
    59  			},
    60  		).Render()
    61  
    62  		graphString = NewCounterGraphRenderer(
    63  			r.failedControls,
    64  			r.totalControls,
    65  			r.maxTotalControls,
    66  			CounterGraphRendererOptions{
    67  				FailedColorFunc: ControlColors.CountGraphFail,
    68  			},
    69  		).Render()
    70  	}
    71  	severityWidth := helpers.PrintableLength(severityString)
    72  	counterWidth := helpers.PrintableLength(counterString)
    73  	graphWidth := helpers.PrintableLength(graphString)
    74  
    75  	// figure out how much width we have available for the title
    76  	availableWidth := r.width - counterWidth - graphWidth - severityWidth - indentWidth
    77  
    78  	// now availableWidth is all we have - if it is not enough we need to truncate the title
    79  	titleString := NewGroupTitleRenderer(r.title, availableWidth).Render()
    80  	titleWidth := helpers.PrintableLength(titleString)
    81  
    82  	// is there any room for a spacer
    83  	spacerWidth := availableWidth - titleWidth
    84  	var spacerString string
    85  	if spacerWidth > 0 && !isDryRun {
    86  		spacerString = NewSpacerRenderer(spacerWidth).Render()
    87  	}
    88  
    89  	// now put these all together
    90  	str := fmt.Sprintf("%s%s%s%s%s%s", formattedIndent, titleString, spacerString, severityString, counterString, graphString)
    91  	return str
    92  }