ui

package
v3.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 27, 2026 License: MIT Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultPadding specifies the number of spaces between columns in a table.
	DefaultPadding = 2

	// DefaultWriter specifies the output io.Writer for the Table.Print method.
	DefaultWriter io.Writer = os.Stdout

	// DefaultHeaderFormatter specifies the default Formatter for the table header.
	DefaultHeaderFormatter Formatter

	// DefaultFirstColumnFormatter specifies the default Formatter for the first column cells.
	DefaultFirstColumnFormatter Formatter

	// DefaultWidthFunc specifies the default WidthFunc for calculating column widths
	DefaultWidthFunc WidthFunc = utf8.RuneCountInString
)

These are the default properties for all Tables created from this package and can be modified.

Functions

func ClearScreen

func ClearScreen()

func ConsoleTitle

func ConsoleTitle(title string) string

func CreateStandardTable

func CreateStandardTable(header []string) *tablewriter.Table

func CreateTableWithColumnAndRowSeparator

func CreateTableWithColumnAndRowSeparator(header []string) *tablewriter.Table

func CreateTableWithColumnSeparator

func CreateTableWithColumnSeparator(header []string) *tablewriter.Table

func PadRight

func PadRight(str, pad string, lenght int) string

func PrintConsoleTitle

func PrintConsoleTitle(title string)

func PromptForBool

func PromptForBool(question string) (answer bool)

func PromptForEditor

func PromptForEditor(question string) (answer string)

func PromptForInt

func PromptForInt(question string) (answer int)

func PromptForLanguage

func PromptForLanguage(question string) (answer string)

func PromptForMultilineString

func PromptForMultilineString(question string) (answer string)

func PromptForPassword

func PromptForPassword(question string) string

func PromptForString

func PromptForString(question string) (answer string)

func PromptForYesNo

func PromptForYesNo(question string, defaultVal string) (answer bool)

func PromptForYesNoList

func PromptForYesNoList(question string) bool

func RenderTable

func RenderTable(tc TableConverter)

func Sequence

func Sequence(str, pad string, length int) string

Types

type ColumnToTableRenderer

type ColumnToTableRenderer struct {
	IncludeDescription bool
	Columns            *models.ColumnList
}

func (*ColumnToTableRenderer) Header

func (d *ColumnToTableRenderer) Header() []string

func (*ColumnToTableRenderer) Rows

func (d *ColumnToTableRenderer) Rows() [][]string

type Formatter

type Formatter func(string, ...interface{}) string

Formatter functions expose a fmt.Sprintf signature that can be used to modify the display of the text in either the header or first column of a Table. The formatter should not change the width of original text as printed since column widths are calculated pre-formatting (though this issue can be mitigated with increased padding).

tbl.WithHeaderFormatter(func(format string, vals ...interface{}) string {
  return strings.ToUpper(fmt.Sprintf(format, vals...))
})

A good use case for formatters is to use ANSI escape codes to color the cells for a nicer interface. The package color (https://github.com/fatih/color) makes it easy to generate these automatically: http://godoc.org/github.com/fatih/color#Color.SprintfFunc

type SortColumnById

type SortColumnById []models.Column

func (SortColumnById) Len

func (a SortColumnById) Len() int

func (SortColumnById) Less

func (a SortColumnById) Less(i, j int) bool

func (SortColumnById) Swap

func (a SortColumnById) Swap(i, j int)

type SortColumnByName

type SortColumnByName []models.Column

func (SortColumnByName) Len

func (a SortColumnByName) Len() int

func (SortColumnByName) Less

func (a SortColumnByName) Less(i, j int) bool

func (SortColumnByName) Swap

func (a SortColumnByName) Swap(i, j int)

type Table

type Table interface {
	WithHeaderFormatter(f Formatter) Table
	WithFirstColumnFormatter(f Formatter) Table
	WithPadding(p int) Table
	WithWriter(w io.Writer) Table
	WithWidthFunc(f WidthFunc) Table

	AddRow(vals ...interface{}) Table
	Print()
}

Table describes the interface for building up a tabular representation of data. It exposes fluent/chainable methods for convenient table building.

WithHeaderFormatter and WithFirstColumnFormatter sets the Formatter for the header and first column, respectively. If nil is passed in (the default), no formatting will be applied.

New("foo", "bar").WithFirstColumnFormatter(func(f string, v ...interface{}) string {
  return strings.ToUpper(fmt.Sprintf(f, v...))
})

WithPadding specifies the minimum padding between cells in a row and defaults to DefaultPadding. Padding values less than or equal to zero apply no extra padding between the columns.

New("foo", "bar").WithPadding(3)

WithWriter modifies the writer which Print outputs to, defaulting to DefaultWriter when instantiated. If nil is passed, os.Stdout will be used.

New("foo", "bar").WithWriter(os.Stderr)

WithWidthFunc sets the function used to calculate the width of the string in a column. By default, the number of utf8 runes in the string is used.

AddRow adds another row of data to the table. Any values can be passed in and will be output as its string representation as described in the fmt standard package. Rows can have less cells than the total number of columns in the table; subsequent cells will be rendered empty. Rows with more cells than the total number of columns will be truncated. References to the data are not held, so the passed in values can be modified without affecting the table's output.

New("foo", "bar").AddRow("fizz", "buzz").AddRow(time.Now()).AddRow(1, 2, 3).Print()
// Output:
// foo                              bar
// fizz                             buzz
// 2006-01-02 15:04:05.0 -0700 MST
// 1                                2

Print writes the string representation of the table to the provided writer. Print can be called multiple times, even after subsequent mutations of the provided data. The output is always preceded and followed by a new line.

func New

func New(columnHeaders ...interface{}) Table

New creates a Table instance with the specified header(s) provided. The number of columns is fixed at this point to len(columnHeaders) and the defined defaults are set on the instance.

type TableConverter

type TableConverter interface {
	Rows() [][]string
	Header() []string
}

type WidthFunc

type WidthFunc func(string) int

A WidthFunc calculates the width of a string. By default, the number of runes is used but this may not be appropriate for certain character sets. The package runewidth (https://github.com/mattn/go-runewidth) could be used to accomodate multi-cell characters (such as emoji or CJK characters).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL