github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/pkg/control/controldisplay/summary.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/control/controlexecute"
     9  )
    10  
    11  type SummaryRenderer struct {
    12  	resultTree *controlexecute.ExecutionTree
    13  	width      int
    14  }
    15  
    16  func NewSummaryRenderer(resultTree *controlexecute.ExecutionTree, width int) *SummaryRenderer {
    17  	return &SummaryRenderer{
    18  		resultTree: resultTree,
    19  		width:      width,
    20  	}
    21  }
    22  
    23  func (r SummaryRenderer) Render() string {
    24  	availableWidth := r.width
    25  
    26  	// first build the severity block - if it exists, it will be used to dictate the max width
    27  	severityRows := NewSummarySeverityRenderer(r.resultTree, availableWidth).Render()
    28  	// now get the length of the longest row from the severity block (if any)
    29  	severityWidth := 0
    30  	for _, row := range severityRows {
    31  		if w := helpers.PrintableLength(row); w > severityWidth {
    32  			severityWidth = w
    33  		}
    34  	}
    35  	if severityWidth > 0 {
    36  		availableWidth = severityWidth
    37  	}
    38  
    39  	// now do the summary row - this is the next longest row
    40  	summaryRow := NewSummaryTotalRowRenderer(r.resultTree, availableWidth).Render()
    41  	// if there is no severity block, use the summary row to dictate the max width
    42  	if severityWidth == 0 {
    43  		availableWidth = helpers.PrintableLength(summaryRow)
    44  	}
    45  
    46  	okStatusRow := NewSummaryStatusRowRenderer(r.resultTree, availableWidth, "ok").Render()
    47  	skipStatusRow := NewSummaryStatusRowRenderer(r.resultTree, availableWidth, "skip").Render()
    48  	infoStatusRow := NewSummaryStatusRowRenderer(r.resultTree, availableWidth, "info").Render()
    49  	alarmStatusRow := NewSummaryStatusRowRenderer(r.resultTree, availableWidth, "alarm").Render()
    50  	errorStatusRow := NewSummaryStatusRowRenderer(r.resultTree, availableWidth, "error").Render()
    51  
    52  	titleLine := fmt.Sprintf("%s\n", ControlColors.GroupTitle("Summary"))
    53  
    54  	// build the summary
    55  	var summaryLines = []string{
    56  		titleLine,
    57  		// status summaries
    58  		okStatusRow,
    59  		skipStatusRow,
    60  		infoStatusRow,
    61  		alarmStatusRow,
    62  		errorStatusRow,
    63  	}
    64  	// if there is a severity block, add it
    65  	if len(severityRows) > 0 {
    66  		summaryLines = append(summaryLines, "") // blank line
    67  		summaryLines = append(summaryLines, severityRows...)
    68  	}
    69  	// now add the summary
    70  	summaryLines = append(summaryLines,
    71  		"", // blank line
    72  		// summary row
    73  		summaryRow,
    74  	)
    75  
    76  	return strings.Join(summaryLines, "\n")
    77  }