text

package
v3.3.2+incompatible Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2018 License: MIT Imports: 5 Imported by: 198

Documentation

Index

Constants

View Source
const (
	EscapeReset     = EscapeStart + "0" + EscapeStop
	EscapeStart     = "\x1b["
	EscapeStartRune = rune(27) // \x1b
	EscapeStop      = "m"
	EscapeStopRune  = 'm'
)

Constants

Variables

View Source
var ANSICodesSupported = areANSICodesSupported()

ANSICodesSupported will be true on consoles where ANSI Escape Codes/Sequences are supported.

Functions

func DisableColors

func DisableColors()

DisableColors (forcefully) disables color coding globally.

func EnableColors

func EnableColors()

EnableColors (forcefully) enables color coding globally.

func FilterStrings

func FilterStrings(s []string, f func(string) bool) []string

FilterStrings filters the slice 's' to items which return truth when passed to 'f'.

func FixedLengthString

func FixedLengthString(s string, length int, snipIndicator string) string

FixedLengthString returns the given string with a fixed length. For ex.:

FixedLengthString("Ghost", 0, "~") == "Ghost"
FixedLengthString("Ghost", 1, "~") == "~"
FixedLengthString("Ghost", 3, "~") == "Gh~"
FixedLengthString("Ghost", 5, "~") == "Ghost"
FixedLengthString("Ghost", 7, "~") == "Ghost  "

func GetLongestLineLength

func GetLongestLineLength(s string) int

GetLongestLineLength returns the length of the longest "line" within the argument string. For ex.:

GetLongestLineLength("Ghost!\nCome back here!\nRight now!") == 15

func InsertRuneEveryN

func InsertRuneEveryN(s string, r rune, n int) string

InsertRuneEveryN inserts the rune every N characters in the string. For ex.:

InsertRuneEveryN("Ghost", '-', 1) == "G-h-o-s-t"
InsertRuneEveryN("Ghost", '-', 2) == "Gh-os-t"
InsertRuneEveryN("Ghost", '-', 3) == "Gho-st"
InsertRuneEveryN("Ghost", '-', 4) == "Ghos-t"
InsertRuneEveryN("Ghost", '-', 5) == "Ghost"

func RepeatAndTrim

func RepeatAndTrim(s string, maxRunes int) string

RepeatAndTrim repeats the given string until it is as long as maxRunes. For ex.:

RepeatAndTrim("Ghost", 0) == ""
RepeatAndTrim("Ghost", 5) == "Ghost"
RepeatAndTrim("Ghost", 7) == "GhostGh"
RepeatAndTrim("Ghost", 10) == "GhostGhost"

func RuneCountWithoutEscapeSeq

func RuneCountWithoutEscapeSeq(s string) int

RuneCountWithoutEscapeSeq is similar to utf8.RuneCountInString, except for the fact that it ignores escape sequences in the counting. For ex.:

RuneCountWithoutEscapeSeq("") == 0
RuneCountWithoutEscapeSeq("Ghost") == 5
RuneCountWithoutEscapeSeq("\x1b[33mGhost\x1b[0m") == 5
RuneCountWithoutEscapeSeq("\x1b[33mGhost\x1b[0") == 5

func TrimTextWithoutEscapeSeq

func TrimTextWithoutEscapeSeq(s string, n int) string

TrimTextWithoutEscapeSeq trims a string to the given length accounting for escape sequences For ex.:

TrimTextWithoutEscapeSeq("Ghost", 3) == "Gho"
TrimTextWithoutEscapeSeq("Ghost", 6) == "Ghost"
TrimTextWithoutEscapeSeq("\x1b[33mGhost\x1b[0m", 3) == "\x1b[33mGho\x1b[0m"
TrimTextWithoutEscapeSeq("\x1b[33mGhost\x1b[0m", 6) == "\x1b[33mGhost\x1b[0m"

func WrapText

func WrapText(s string, n int) string

WrapText wraps a string to the given length using a newline. For ex.:

WrapText("Ghost", 1) == "G\nh\no\ns\nt"
WrapText("Ghost", 2) == "Gh\nos\nt"
WrapText("Ghost", 3) == "Gho\nst"
WrapText("Ghost", 4) == "Ghos\nt"
WrapText("Ghost", 5) == "Ghost"
WrapText("Ghost", 6) == "Ghost"
WrapText("Jon\nSnow", 2) == "Jo\nn\nSn\now"
WrapText("Jon\nSnow\n", 2) == "Jo\nn\nSn\now\n"

Types

type Align

type Align int

Align denotes how text is to be aligned horizontally.

const (
	AlignDefault Align = iota // same as AlignLeft
	AlignLeft                 // "left        "
	AlignCenter               // "   center   "
	AlignJustify              // "justify   it"
	AlignRight                // "       right"
)

Align enumerations

func (Align) Apply

func (a Align) Apply(text string, maxLength int) string

Apply aligns the text as directed. Examples:

  • AlignDefault.Apply("Jon Snow", 12) returns "Jon Snow "
  • AlignLeft.Apply("Jon Snow", 12) returns "Jon Snow "
  • AlignCenter.Apply("Jon Snow", 12) returns " Jon Snow "
  • AlignJustify.Apply("Jon Snow", 12) returns "Jon Snow"
  • AlignRight.Apply("Jon Snow", 12) returns " Jon Snow"

func (Align) HTMLProperty

func (a Align) HTMLProperty() string

HTMLProperty returns the equivalent HTML horizontal-align tag property.

func (Align) MarkdownProperty

func (a Align) MarkdownProperty() string

MarkdownProperty returns the equivalent Markdown horizontal-align separator.

type Color

type Color int

Color represents a single color to render with.

const (
	Reset Color = iota
	Bold
	Faint
	Italic
	Underline
	BlinkSlow
	BlinkRapid
	ReverseVideo
	Concealed
	CrossedOut
)

Base colors -- attributes in reality

const (
	FgBlack Color = iota + 30
	FgRed
	FgGreen
	FgYellow
	FgBlue
	FgMagenta
	FgCyan
	FgWhite
)

Foreground colors

const (
	FgHiBlack Color = iota + 90
	FgHiRed
	FgHiGreen
	FgHiYellow
	FgHiBlue
	FgHiMagenta
	FgHiCyan
	FgHiWhite
)

Foreground Hi-Intensity colors

const (
	BgBlack Color = iota + 40
	BgRed
	BgGreen
	BgYellow
	BgBlue
	BgMagenta
	BgCyan
	BgWhite
)

Background colors

const (
	BgHiBlack Color = iota + 100
	BgHiRed
	BgHiGreen
	BgHiYellow
	BgHiBlue
	BgHiMagenta
	BgHiCyan
	BgHiWhite
)

Background Hi-Intensity colors

func (Color) GetEscapeSeq

func (c Color) GetEscapeSeq() string

GetEscapeSeq returns the ANSI escape sequence for the color.

func (Color) Sprint

func (c Color) Sprint(a ...interface{}) string

Sprint colorizes and prints the given string(s).

func (Color) Sprintf

func (c Color) Sprintf(format string, a ...interface{}) string

Sprintf formats and colorizes and prints the given string(s).

type Colors

type Colors []Color

Colors represents an array of Color objects to render with. Example: Colors{FgCyan, BgBlack}

func (Colors) GetEscapeSeq

func (c Colors) GetEscapeSeq() string

GetEscapeSeq returns the ANSI escape sequence for the colors set.

func (Colors) Sprint

func (c Colors) Sprint(a ...interface{}) string

Sprint colorizes and prints the given string(s).

func (Colors) Sprintf

func (c Colors) Sprintf(format string, a ...interface{}) string

Sprintf formats and colorizes and prints the given string(s).

type Cursor

type Cursor rune

Cursor helps move the cursor on the console in multiple directions.

const (
	// CursorDown helps move the Cursor Down X lines
	CursorDown Cursor = 'B'

	// CursorLeft helps move the Cursor Left X characters
	CursorLeft Cursor = 'D'

	// CursorRight helps move the Cursor Right X characters
	CursorRight Cursor = 'C'

	// CursorUp helps move the Cursor Up X lines
	CursorUp Cursor = 'A'

	// EraseLine helps erase all characters to the Right of the Cursor in the
	// current line
	EraseLine Cursor = 'K'
)

func (Cursor) Sprint

func (c Cursor) Sprint() string

Sprint prints the Escape Sequence to move the Cursor once.

func (Cursor) Sprintn

func (c Cursor) Sprintn(n int) string

Sprintn prints the Escape Sequence to move the Cursor "n" times.

type Format

type Format int

Format denotes the "case" to use for text.

const (
	FormatDefault Format = iota // default_Case
	FormatLower                 // lower
	FormatTitle                 // Title
	FormatUpper                 // UPPER
)

Format enumerations

func (Format) Apply

func (tc Format) Apply(text string) string

Apply converts the text as directed.

type VAlign

type VAlign int

VAlign denotes how text is to be aligned vertically.

const (
	VAlignDefault VAlign = iota // same as VAlignTop
	VAlignTop                   // "top\n\n"
	VAlignMiddle                // "\nmiddle\n"
	VAlignBottom                // "\n\nbottom"
)

VAlign enumerations

func (VAlign) Apply

func (va VAlign) Apply(lines []string, maxLines int) []string

Apply aligns the lines vertically. Examples:

  • VAlignTop.Apply({"Game", "Of", "Thrones"}, 5) returns {"Game", "Of", "Thrones", "", ""}
  • VAlignMiddle.Apply({"Game", "Of", "Thrones"}, 5) returns {"", "Game", "Of", "Thrones", ""}
  • VAlignBottom.Apply({"Game", "Of", "Thrones"}, 5) returns {"", "", "Game", "Of", "Thrones"}

func (VAlign) ApplyStr

func (va VAlign) ApplyStr(text string, maxLines int) []string

ApplyStr aligns the string (of 1 or more lines) vertically. Examples:

  • VAlignTop.ApplyStr("Game\nOf\nThrones", 5) returns {"Game", "Of", "Thrones", "", ""}
  • VAlignMiddle.ApplyStr("Game\nOf\nThrones", 5) returns {"", "Game", "Of", "Thrones", ""}
  • VAlignBottom.ApplyStr("Game\nOf\nThrones", 5) returns {"", "", "Game", "Of", "Thrones"}

func (VAlign) HTMLProperty

func (va VAlign) HTMLProperty() string

HTMLProperty returns the equivalent HTML vertical-align tag property.

Jump to

Keyboard shortcuts

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