open-consul/command/cli/formatting.go
Ronald 7a5c8dc1eb
Copyright headers for command folder (#16705)
* copyright headers for agent folder

* Ignore test data files

* fix proto files and remove headers in agent/uiserver folder

* ignore deep-copy files

* copyright headers for agent folder

* Copyright headers for command folder

* fix merge conflicts
2023-03-28 15:12:30 -04:00

92 lines
2 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package cli
import (
"github.com/fatih/color"
"github.com/olekukonko/tablewriter"
)
// Functions to set up colorized output.
const (
noColor = -1
)
func colorize(message string, uc UiColor) string {
if uc.Code == noColor {
return message
}
attr := []color.Attribute{color.Attribute(uc.Code)}
if uc.Bold {
attr = append(attr, color.Bold)
}
return color.New(attr...).SprintFunc()(message)
}
// UiColor is a posix shell color code to use.
type UiColor struct {
Code int
Bold bool
}
// A list of colors that are useful. These are all non-bolded by default.
var (
UiColorNone UiColor = UiColor{noColor, false}
UiColorRed = UiColor{int(color.FgHiRed), false}
UiColorGreen = UiColor{int(color.FgHiGreen), false}
UiColorYellow = UiColor{int(color.FgHiYellow), false}
UiColorBlue = UiColor{int(color.FgHiBlue), false}
UiColorMagenta = UiColor{int(color.FgHiMagenta), false}
UiColorCyan = UiColor{int(color.FgHiCyan), false}
)
// Functions that are useful for table output.
const (
Yellow = "yellow"
Green = "green"
Red = "red"
)
var colorMapping = map[string]int{
Green: tablewriter.FgGreenColor,
Yellow: tablewriter.FgYellowColor,
Red: tablewriter.FgRedColor,
}
// Table is passed to UI.Table to provide a nicely formatted table.
type Table struct {
Headers []string
Rows [][]Cell
}
// Cell is a single entry for a table.
type Cell struct {
Value string
Color string
}
// NewTable creates a new Table structure that can be used with UI.Table.
func NewTable(headers ...string) *Table {
return &Table{
Headers: headers,
}
}
// AddRow adds a row to the table.
func (t *Table) AddRow(cols []string, colors []string) {
var row []Cell
for i, col := range cols {
if i < len(colors) {
row = append(row, Cell{Value: col, Color: colors[i]})
} else {
row = append(row, Cell{Value: col})
}
}
t.Rows = append(t.Rows, row)
}