github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/pkg/control/controldisplay/summary_status_row.go (about) 1 package controldisplay 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/turbot/go-kit/helpers" 8 "github.com/turbot/steampipe/pkg/constants" 9 "github.com/turbot/steampipe/pkg/control/controlexecute" 10 "golang.org/x/text/language" 11 "golang.org/x/text/message" 12 ) 13 14 type SummaryStatusRowRenderer struct { 15 resultTree *controlexecute.ExecutionTree 16 width int 17 status string 18 } 19 20 func NewSummaryStatusRowRenderer(resultTree *controlexecute.ExecutionTree, width int, status string) *SummaryStatusRowRenderer { 21 return &SummaryStatusRowRenderer{ 22 resultTree: resultTree, 23 width: width, 24 status: status, 25 } 26 } 27 28 func (r *SummaryStatusRowRenderer) Render() string { 29 txtColorFunction := ControlColors.StatusColors[r.status] 30 graphColorFunction := ControlColors.GraphColors[r.status] 31 32 count := -1 33 switch r.status { 34 case constants.ControlOk: 35 count = r.resultTree.Root.Summary.Status.Ok 36 case constants.ControlSkip: 37 count = r.resultTree.Root.Summary.Status.Skip 38 case constants.ControlInfo: 39 count = r.resultTree.Root.Summary.Status.Info 40 case constants.ControlAlarm: 41 count = r.resultTree.Root.Summary.Status.Alarm 42 case constants.ControlError: 43 count = r.resultTree.Root.Summary.Status.Error 44 default: 45 // we can safely panic here, since the status enum check should have been 46 // done by the executor. this is here for unit tests mostly 47 panic(fmt.Sprintf("unknown status: %s", r.status)) 48 } 49 countString := r.getPrintableNumber(count, txtColorFunction) 50 51 graph := NewCounterGraphRenderer( 52 count, 53 count, 54 r.resultTree.Root.Summary.Status.TotalCount(), 55 CounterGraphRendererOptions{ 56 FailedColorFunc: graphColorFunction, 57 }, 58 ).Render() 59 60 statusStr := fmt.Sprintf("%s ", txtColorFunction(strings.ToUpper(r.status))) 61 spaceAvailableForSpacer := r.width - (helpers.PrintableLength(statusStr) + helpers.PrintableLength(countString) + helpers.PrintableLength(graph)) 62 spacer := NewSpacerRenderer(spaceAvailableForSpacer) 63 64 return fmt.Sprintf( 65 "%s%s%s%s", 66 statusStr, 67 spacer.Render(), 68 countString, 69 graph, 70 ) 71 } 72 73 func (r *SummaryStatusRowRenderer) getPrintableNumber(number int, cf colorFunc) string { 74 p := message.NewPrinter(language.English) 75 s := p.Sprintf("%d", number) 76 return fmt.Sprintf("%s ", cf(s)) 77 }