brimtext

package
v0.0.0-...-62917fb Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2015 License: BSD-3-Clause, BSD-3-Clause Imports: 5 Imported by: 0

README

BrimText

Text Tools for Go

Package brimtext contains tools for working with text. Probably the most complex of these tools is Align, which allows for formatting "pretty tables".

API Documentation

This is the latest stable version of the package.
For the latest development version of the package, switch to the master branch
or use github.com/gholt/brimtext as the import path.
Also, you'd want to use the Dev API Documentation.

Copyright Gregory Holt. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.

Example Code

package main

import (
    "fmt"

    "gopkg.in/gholt/brimtext.v1"
)

func main() {
    example := 1
    fmt.Printf("This is the %d%s example:\n\n",
        example, brimtext.OrdinalSuffix(example))
    fmt.Println(brimtext.Align([][]string{
        []string{"", "Bob", "Sue", "John"},
        []string{"Hometown", "San Antonio", "Austin", "New York"},
        []string{"Mother", "Bessie", "Mary", "Sarah"},
        []string{"Father", "Rick", "Dan", "Mike"},
    }, brimtext.NewDefaultAlignOptions()))

    example++
    fmt.Printf("This is the %d%s example:\n\n",
        example, brimtext.OrdinalSuffix(example))
    fmt.Println(brimtext.Align([][]string{
        []string{"", "Bob", "Sue", "John"},
        nil,
        []string{"Hometown", "San Antonio", "Austin", "New York"},
        []string{"Mother", "Bessie", "Mary", "Sarah"},
        []string{"Father", "Rick", "Dan", "Mike"},
    }, brimtext.NewSimpleAlignOptions()))

    example++
    fmt.Printf("This is the %d%s example:\n\n",
        example, brimtext.OrdinalSuffix(example))
    data := [][]int{
        {8, 20, 11},
        {5, 11, 10},
        {3, 9, 1},
        {1200000, 2400000, 1700000},
    }
    table := [][]string{[]string{"", "Bob", "Sue", "John"}}
    for rowNum, values := range data {
        row := []string{""}
        prefix := ""
        switch rowNum {
        case 0:
            row[0] = "Shot Attempts"
        case 1:
            row[0] = "Shots Made"
        case 2:
            row[0] = "Shots Missed"
        case 3:
            row[0] = "Salary"
            prefix = "$"
        }
        for _, v := range values {
            row = append(row, prefix+brimtext.ThousandsSep(int64(v), ","))
        }
        table = append(table, row)
    }
    opts := brimtext.NewUnicodeBoxedAlignOptions()
    opts.Alignments = []brimtext.Alignment{
        brimtext.Right,
        brimtext.Right,
        brimtext.Right,
        brimtext.Right,
    }
    fmt.Println(brimtext.Align(table, opts))
}

Example Output

This is the 1st example:

         Bob         Sue    John
Hometown San Antonio Austin New York
Mother   Bessie      Mary   Sarah
Father   Rick        Dan    Mike

This is the 2nd example:

+----------+-------------+--------+----------+
|          | Bob         | Sue    | John     |
+----------+-------------+--------+----------+
| Hometown | San Antonio | Austin | New York |
| Mother   | Bessie      | Mary   | Sarah    |
| Father   | Rick        | Dan    | Mike     |
+----------+-------------+--------+----------+

This is the 3rd example:

╔═══════════════╦════════════╤════════════╤════════════╗
║               ║        Bob │        Sue │       John ║
╠═══════════════╬════════════╪════════════╪════════════╣
║ Shot Attempts ║          8 │         20 │         11 ║
╟───────────────╫────────────┼────────────┼────────────╢
║    Shots Made ║          5 │         11 │         10 ║
╟───────────────╫────────────┼────────────┼────────────╢
║  Shots Missed ║          3 │          9 │          1 ║
╟───────────────╫────────────┼────────────┼────────────╢
║        Salary ║ $1,200,000 │ $2,400,000 │ $1,700,000 ║
╚═══════════════╩════════════╧════════════╧════════════╝

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

Constants

This section is empty.

Variables

View Source
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 AllEqual

func AllEqual(compare string, values ...string) bool

AllEqual returns true if all the values each equal the compare string.

func ClosestANSIForeground

func ClosestANSIForeground(red int, green int, blue int) []byte

ClosestANSIForeground translates the RGB values to the closest ANSIEscape sequence for that foreground color.

func ClosestANSIForegroundString

func ClosestANSIForegroundString(value string) []byte

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

func HumanSize(b int64, appendBytes string) string

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

func OrdinalSuffix(number int) string

OrdinalSuffix returns "st", "nd", "rd", etc. for the number given (1st, 2nd, 3rd, etc.).

func Sentence

func Sentence(value string) string

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

func ThousandsSep(v int64, sep string) string

ThousandsSep returns the number formatted using the separator at each thousands position, such as ThousandsSep(1234567, ",") giving 1,234,567.

func ThousandsSepU

func ThousandsSepU(v uint64, sep string) string

ThousandsSepU returns the number formatted using the separator at each thousands position, such as ThousandsSepU(1234567, ",") giving 1,234,567.

func Wrap

func Wrap(text string, width int, indent1 string, indent2 string) string

Wrap wraps text for more readable output.

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 Alignment

type Alignment int
const (
	Left Alignment = iota
	Right
	Center
)

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) Less

func (s StringSliceToLowerSort) Less(x int, y int) bool

func (StringSliceToLowerSort) Swap

func (s StringSliceToLowerSort) Swap(x int, y int)

Jump to

Keyboard shortcuts

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