Documentation ¶
Overview ¶
Package brimtext contains tools for working with text. Probably the most complex of these tools is Align, which allows for formatting "pretty tables".
This is the latest stable version of the package.
For the latest development version of the package, switch to the master branch at https://github.com/gholt/brimtext or use github.com/gholt/brimtext as the import path.
Also, you'd want to use http://godoc.org/github.com/gholt/brimtext for the development documentation.
Index ¶
- Variables
- func Align(data [][]string, opts *AlignOptions) string
- func AllEqual(compare string, values ...string) bool
- func ClosestANSIForeground(red int, green int, blue int) []byte
- func ClosestANSIForegroundString(value string) []byte
- func GetTTYWidth() int
- func HumanSize(b int64, appendBytes string) string
- func OrdinalSuffix(number int) string
- func Sentence(value string) string
- func ThousandsSep(v int64, sep string) string
- func ThousandsSepU(v uint64, sep string) string
- func Wrap(text string, width int, indent1 string, indent2 string) string
- type ANSIEscapeCodes
- type AlignOptions
- type Alignment
- type StringSliceToLowerSort
Constants ¶
This section is empty.
Variables ¶
var ANSIEscape = ANSIEscapeCodes{ Reset: []byte{27, '[', '0', 'm'}, Bold: []byte{27, '[', '1', 'm'}, BBlack: []byte{27, '[', '4', '0', 'm'}, BRed: []byte{27, '[', '4', '1', 'm'}, BGreen: []byte{27, '[', '4', '2', 'm'}, BYellow: []byte{27, '[', '4', '3', 'm'}, BBlue: []byte{27, '[', '4', '4', 'm'}, BMagenta: []byte{27, '[', '4', '5', 'm'}, BCyan: []byte{27, '[', '4', '6', 'm'}, BWhite: []byte{27, '[', '4', '7', 'm'}, FBlack: []byte{27, '[', '3', '0', 'm'}, FRed: []byte{27, '[', '3', '1', 'm'}, FGreen: []byte{27, '[', '3', '2', 'm'}, FYellow: []byte{27, '[', '3', '3', 'm'}, FBlue: []byte{27, '[', '3', '4', 'm'}, FMagenta: []byte{27, '[', '3', '5', 'm'}, FCyan: []byte{27, '[', '3', '6', 'm'}, FWhite: []byte{27, '[', '3', '7', 'm'}, }
ANSIEscape provides ease of access to common ANSI Escape Codes.
Functions ¶
func Align ¶
func Align(data [][]string, opts *AlignOptions) string
Align will format a table according to options. If opts is nil, NewDefaultAlignOptions is used.
func ClosestANSIForeground ¶
ClosestANSIForeground translates the RGB values to the closest ANSIEscape sequence for that foreground color.
func ClosestANSIForegroundString ¶
ClosestANSIForegroundString translates the CSS-style color (e.g. "#ac8" "#ffee66") string to the closest ANSIEscape sequence for that foreground color.
func GetTTYWidth ¶
func GetTTYWidth() int
GetTTYWidth returns the width of controlling TTY if it can or 80.
func HumanSize ¶
Returns a more readable size format, such as HumanSize(1234567, "") giving "1M". For values less than 1K, it is common that no suffix letter should be added; but the appendBytes parameter is provided in case clarity is needed.
func OrdinalSuffix ¶
OrdinalSuffix returns "st", "nd", "rd", etc. for the number given (1st, 2nd, 3rd, etc.).
func Sentence ¶
Sentence converts the value into a sentence, uppercasing the first character and ensuring the string ends with a period. Useful to output better looking error.Error() messages, which are all lower case with no trailing period by convention.
func ThousandsSep ¶
ThousandsSep returns the number formatted using the separator at each thousands position, such as ThousandsSep(1234567, ",") giving 1,234,567.
func ThousandsSepU ¶
ThousandsSepU returns the number formatted using the separator at each thousands position, such as ThousandsSepU(1234567, ",") giving 1,234,567.
Types ¶
type ANSIEscapeCodes ¶
type ANSIEscapeCodes struct { Reset []byte Bold []byte BBlack, BRed, BGreen, BYellow, BBlue, BMagenta, BCyan, BWhite []byte FBlack, FRed, FGreen, FYellow, FBlue, FMagenta, FCyan, FWhite []byte }
ANSIEscapeCodes is the defining structure for the more commonly used ANSIEscape global variable.
type AlignOptions ¶
type AlignOptions struct { // Widths indicate the desired widths of each column. If nil or if a value // is 0, no rewrapping will be done. Widths []int Alignments []Alignment // FirstDR etc. control what is output for situations with a prepended // display row, First row output with Down and Right connections, etc. FirstDR string FirstLR string FirstFirstDLR string FirstDLR string FirstDL string // RowFirstUD etc. control situations for each data row output. RowFirstUD string RowSecondUD string RowUD string RowLastUD string // LeaveTrailingWhitespace should be set true if the last cell of data row // needs spaces to fill to the end (usually needed when setting RawLastUD). LeaveTrailingWhitespace bool // FirstNilFirstUDR etc. control situations when the first nil data row is // encountered. Can be used to separate the header from the rest of the // rows. FirstNilFirstUDR string FirstNilLR string FirstNilFirstUDLR string FirstNilUDLR string FirstNilLastUDL string // NilFirstUDR etc. control situations when the second and subsequent nil // data rows are encountered. Can be used to separate rows from each other. NilFirstUDR string NilLR string NilFirstUDLR string NilUDLR string NilLastUDL string // LastUR etc. control what is output for situations with an appended // display row. LastUR string LastLR string LastFirstULR string LastULR string LastUL string // NilBetweenEveryRow will add a nil data row between all rows; use to emit // FirstNil* and Nil* row separators. NilBetweenEveryRow bool }
func NewBoxedAlignOptions ¶
func NewBoxedAlignOptions() *AlignOptions
NewBoxedAlignOptions gives:
&AlignOptions{ FirstDR: "+=", FirstLR: "=", FirstFirstDLR: "=+=", FirstDLR: "=+=", FirstDL: "=+", RowFirstUD: "| ", RowSecondUD: " | ", RowUD: " | ", RowLastUD: " |", LeaveTrailingWhitespace: true, FirstNilFirstUDR: "+=", FirstNilLR: "=", FirstNilFirstUDLR: "=+=", FirstNilUDLR: "=+=", FirstNilLastUDL: "=+", NilFirstUDR: "+-", NilLR: "-", NilFirstUDLR: "-+-", NilUDLR: "-+-", NilLastUDL: "-+", LastUR: "+=", LastLR: "=", LastFirstULR: "=+=", LastULR: "=+=", LastUL: "=+", NilBetweenEveryRow: true, }
Which will format tables like:
+==========+=============+========+==========+ | | Bob | Sue | John | +==========+=============+========+==========+ | Hometown | San Antonio | Austin | New York | +----------+-------------+--------+----------+ | Mother | Bessie | Mary | Sarah | +----------+-------------+--------+----------+ | Father | Rick | Dan | Mike | +==========+=============+========+==========+
func NewDefaultAlignOptions ¶
func NewDefaultAlignOptions() *AlignOptions
NewDefaultAlignOptions gives:
&AlignOptions{RowSecondUD: " ", RowUD: " "}
Which will format tables like:
Bob Sue John Hometown San Antonio Austin New York Mother Bessie Mary Sarah Father Rick Dan Mike
func NewSimpleAlignOptions ¶
func NewSimpleAlignOptions() *AlignOptions
NewSimpleAlignOptions gives:
return &AlignOptions{ FirstDR: "+-", FirstLR: "-", FirstFirstDLR: "-+-", FirstDLR: "-+-", FirstDL: "-+", RowFirstUD: "| ", RowSecondUD: " | ", RowUD: " | ", RowLastUD: " |", LeaveTrailingWhitespace: true, FirstNilFirstUDR: "+-", FirstNilLR: "-", FirstNilFirstUDLR: "-+-", FirstNilUDLR: "-+-", FirstNilLastUDL: "-+", LastUR: "+-", LastLR: "-", LastFirstULR: "-+-", LastULR: "-+-", LastUL: "-+", }
Which will format tables like:
+----------+-------------+--------+----------+ | | Bob | Sue | John | +----------+-------------+--------+----------+ | Hometown | San Antonio | Austin | New York | | Mother | Bessie | Mary | Sarah | | Father | Rick | Dan | Mike | +----------+-------------+--------+----------+
func NewUnicodeBoxedAlignOptions ¶
func NewUnicodeBoxedAlignOptions() *AlignOptions
NewUnicodeBoxedAlignOptions gives:
&AlignOptions{ FirstDR: "\u2554\u2550", FirstLR: "\u2550", FirstFirstDLR: "\u2550\u2566\u2550", FirstDLR: "\u2550\u2564\u2550", FirstDL: "\u2550\u2557", RowFirstUD: "\u2551 ", RowSecondUD: " \u2551 ", RowUD: " \u2502 ", RowLastUD: " \u2551", LeaveTrailingWhitespace: true, FirstNilFirstUDR: "\u2560\u2550", FirstNilLR: "\u2550", FirstNilFirstUDLR: "\u2550\u256c\u2550", FirstNilUDLR: "\u2550\u256a\u2550", FirstNilLastUDL: "\u2550\u2563", NilFirstUDR: "\u255f\u2500", NilLR: "\u2500", NilFirstUDLR: "\u2500\u256b\u2500", NilUDLR: "\u2500\u253c\u2500", NilLastUDL: "\u2500\u2562", LastUR: "\u255a\u2550", LastLR: "\u2550", LastFirstULR: "\u2550\u2569\u2550", LastULR: "\u2550\u2567\u2550", LastUL: "\u2550\u255d", NilBetweenEveryRow: true, }
Which will format tables like:
╔══════════╦═════════════╤════════╤══════════╗ ║ ║ Bob │ Sue │ John ║ ╠══════════╬═════════════╪════════╪══════════╣ ║ Hometown ║ San Antonio │ Austin │ New York ║ ╟──────────╫─────────────┼────────┼──────────╢ ║ Mother ║ Bessie │ Mary │ Sarah ║ ╟──────────╫─────────────┼────────┼──────────╢ ║ Father ║ Rick │ Dan │ Mike ║ ╚══════════╩═════════════╧════════╧══════════╝
type StringSliceToLowerSort ¶
type StringSliceToLowerSort []string
StringSliceToLowerSort provides a sort.Interface that will sort a []string by their strings.ToLower values. This isn't exactly a case insensitive sort due to Unicode situations, but is usually good enough.
func (StringSliceToLowerSort) Len ¶
func (s StringSliceToLowerSort) Len() int
func (StringSliceToLowerSort) Swap ¶
func (s StringSliceToLowerSort) Swap(x int, y int)