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

     1  package controldisplay
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/turbot/go-kit/helpers"
     7  	"github.com/turbot/steampipe/pkg/constants"
     8  )
     9  
    10  type ErrorRenderer struct {
    11  	error error
    12  
    13  	// screen width
    14  	width  int
    15  	indent string
    16  }
    17  
    18  func NewErrorRenderer(err error, width int, indent string) *ErrorRenderer {
    19  	return &ErrorRenderer{
    20  		error:  err,
    21  		width:  width,
    22  		indent: indent,
    23  	}
    24  }
    25  
    26  func (r ErrorRenderer) Render() string {
    27  	status := NewResultStatusRenderer(constants.ControlError)
    28  	statusString := status.Render()
    29  	statusWidth := helpers.PrintableLength(statusString)
    30  	formattedIndent := fmt.Sprintf("%s", ControlColors.Indent(r.indent))
    31  	indentWidth := helpers.PrintableLength(formattedIndent)
    32  
    33  	// figure out how much width we have available for the error message
    34  	availableWidth := r.width - statusWidth - indentWidth
    35  	errorMessage := helpers.TruncateString(r.error.Error(), availableWidth)
    36  	errorString := fmt.Sprintf("%s", ControlColors.StatusError(errorMessage))
    37  
    38  	// now put these all together
    39  	str := fmt.Sprintf("%s%s%s", formattedIndent, statusString, errorString)
    40  	return str
    41  }