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

     1  package controldisplay
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"golang.org/x/text/language"
     7  	"golang.org/x/text/message"
     8  )
     9  
    10  type CounterRendererOptions struct {
    11  	AddLeadingSpace bool
    12  }
    13  
    14  type CounterRenderer struct {
    15  	failedControls int
    16  	totalControls  int
    17  
    18  	maxFailedControls int
    19  	maxTotalControls  int
    20  
    21  	addLeadingSpace bool
    22  }
    23  
    24  func CounterRendererMinWidth() int {
    25  	return 8
    26  }
    27  
    28  func NewCounterRenderer(failedControls, totalControls, maxFailedControls, maxTotalControls int, options CounterRendererOptions) *CounterRenderer {
    29  	return &CounterRenderer{
    30  		failedControls:    failedControls,
    31  		totalControls:     totalControls,
    32  		maxFailedControls: maxFailedControls,
    33  		maxTotalControls:  maxTotalControls,
    34  		addLeadingSpace:   options.AddLeadingSpace,
    35  	}
    36  }
    37  
    38  /* Render returns the counter string in format "<failed> / <total>.
    39  The alignment depends on the maximum failed and maximum total parameters, as the counters are aligned as follows:
    40  "  3 /   123"
    41  " 13 /    23"
    42  "111 /   123"
    43  "  1 /     4"
    44  "  1 / 1,020"
    45  
    46  minimum counter string is " n / m "
    47  
    48  // NOTE: adds a trailing space
    49  */
    50  
    51  const minimumCounterWidth = 7
    52  
    53  func (r CounterRenderer) Render() string {
    54  	p := message.NewPrinter(language.English)
    55  	// get strings for fails and total - format with commas for thousands
    56  	failedString := p.Sprintf("%d", r.failedControls)
    57  	totalString := p.Sprintf("%d", r.totalControls)
    58  	// get max strings - format with commas for thousands
    59  	maxFailedString := p.Sprintf("%d", r.maxFailedControls)
    60  	maxTotalString := p.Sprintf("%d", r.maxTotalControls)
    61  
    62  	// calculate the width of the fails and total columns
    63  	failedWidth := len(maxFailedString)
    64  	if !r.addLeadingSpace {
    65  		failedWidth = len(failedString)
    66  	}
    67  	totalWidth := len(maxTotalString)
    68  
    69  	// build format string, specifying widths of failedString and totalString
    70  	// this will generate a format string like: "%3s / %4s "
    71  	// (adds a trailing space)
    72  	formatString := fmt.Sprintf("%%%ds %%s %%%ds ", failedWidth, totalWidth)
    73  
    74  	if r.failedControls == 0 {
    75  		return fmt.Sprintf(formatString,
    76  			ControlColors.CountZeroFail(failedString),
    77  			ControlColors.CountZeroFailDivider("/"),
    78  			ControlColors.CountTotalAllPassed(totalString))
    79  	}
    80  
    81  	str := fmt.Sprintf(formatString,
    82  		ControlColors.CountFail(failedString),
    83  		ControlColors.CountDivider("/"),
    84  		ControlColors.CountTotal(totalString))
    85  	return str
    86  }