Consul is a distributed, highly available, and data center aware solution to connect and configure applications across dynamic, distributed infrastructure.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.0 KiB

// Copyright (c) HashiCorp, Inc.
[COMPLIANCE] License changes (#18443) * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at <Blog URL>, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
1 year ago
// SPDX-License-Identifier: BUSL-1.1
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)
}