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

     1  package controldisplay
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io"
     7  	"strings"
     8  
     9  	"github.com/turbot/steampipe/pkg/constants"
    10  	"github.com/turbot/steampipe/pkg/control/controlexecute"
    11  	"github.com/turbot/steampipe/pkg/display"
    12  	"github.com/turbot/steampipe/pkg/utils"
    13  )
    14  
    15  const MaxColumns = 200
    16  
    17  type TextFormatter struct {
    18  	FormatterBase
    19  }
    20  
    21  func (tf TextFormatter) Format(_ context.Context, tree *controlexecute.ExecutionTree) (io.Reader, error) {
    22  	renderer := NewTableRenderer(tree)
    23  	widthConstraint := utils.NewRangeConstraint(renderer.MinimumWidth(), MaxColumns)
    24  	renderedText := renderer.Render(widthConstraint.Constrain(display.GetMaxCols()))
    25  	res := strings.NewReader(fmt.Sprintf("\n%s\n", renderedText))
    26  	return res, nil
    27  }
    28  
    29  func (tf TextFormatter) FileExtension() string {
    30  	return constants.TextExtension
    31  }
    32  
    33  func (tf TextFormatter) Name() string {
    34  	return constants.OutputFormatText
    35  }
    36  
    37  func (tf TextFormatter) Alias() string {
    38  	return constants.OutputFormatBrief
    39  }