text

package
v0.0.21 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2022 License: BSD-2-Clause Imports: 6 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	ESC       = "\x1b"
	CSI       = ESC + "[" // Control Sequence Introducer
	Reset     = CSI + "0m"
	Bold      = CSI + "1m"
	Normal    = CSI + "22m"
	Black     = CSI + "30m"
	Red       = CSI + "31m"
	Green     = CSI + "32m"
	Yellow    = CSI + "33m"
	Blue      = CSI + "34m"
	Magenta   = CSI + "35m"
	Cyan      = CSI + "36m"
	White     = CSI + "37m"
	BGBlack   = CSI + "40m"
	BGRed     = CSI + "41m"
	BGGreen   = CSI + "42m"
	BGYellow  = CSI + "43m"
	BGBlue    = CSI + "44m"
	BGMagenta = CSI + "45m"
	BGCyan    = CSI + "46m"
	BGWhite   = CSI + "47m"

	// Setup brown as an alias for yellow
	Brown   = Yellow
	BGBrown = BGYellow

	// WolfMUD specific meta colors
	Good   = Green
	Info   = Yellow
	Bad    = Red
	Prompt = Magenta
)

ANSI escape sequences for setting colors. These sequences can be concatenated into strings or appended to slice directly. This is preferable to calling Colorize with embedded colour place holders due to the slower performance of Colorize.

Variables

This section is empty.

Functions

func Colorize

func Colorize(in []byte) []byte

Colorize returns a []byte with color place holders replaced with their ANSI escape sequence equivalent. Color place holders have the format [COLOR] where COLOR represents the name of the color (uppercased) to be used. For example:

Colorize([]byte("[RED]Hello [GREEN]World![DEFAULT]"))

Would return a []byte with [RED] and [GREEN] replaced with the ANSI escape sequences \x1b[31m and \x1b[32m respectively causing Hello to be displayed in red and World! to be displayed in green.

The returned slice is always a copy even it the original contains no colors.

Use of this function is discouraged due to relatively poor performance. It's main use is to render text from files, such as those loaded when the server is initially started. In code it is better to use the ANSI escape sequence constants directly.

func Fold

func Fold(in []byte, width int) []byte

Fold takes a []byte and attempts to reformat it so lines have a maximum length of the passed width. Fold will only split lines on whitespace when reformatting. This may result in lines longer than the given width when a word is too long and cannot be split. A width of zero or less indicates no folding will be done but line endings will still be changed from Unix '\n' to network '\r\n' line endings and trailing whitespace, except line feeds '\n', will be removed.

Fold will handle multibyte runes. However it cannot handle 'wide' runes - those that are wider than a normal single character when displayed. This is because the required information is actually contained in the font files of the font in use at the 'client' end.

For example the Chinese for 9 is 九 (U+4E5D). Even in a monospaced font 九 will take up the space of two columns.

For combining characters Fold will assume combining marks are zero width. For example 'a' plus a combining grave accent U+0061 U+0300 will be counted as a single character. However it is better to use an actual latin small letter a with grave 'à' U+00E0. Either should work as expected.

It is expected that the end of line markers for incoming data are Unix line feeds (LF, '\n') and outgoing data will have network line endings, carriage return + line feed pairs (CR+LF, '\r\n').

func List added in v0.0.16

func List(items []string) string

List returns a slice of strings as a comma separated list with 'and' between the last items. If an empty slice is passed the returned string will be empty. If a slice with only one element is passed the returned string will be equal to the passed element.

For example List([]string{"A", "B", "C"}) returns "A, B and C".

func TitleFirst added in v0.0.4

func TitleFirst(s string) string

TitleFirst will return the passed string with the first rune in the string Titlecased.

func Uncomment added in v0.0.9

func Uncomment(re string) string

Uncomment takes a string with comments and returns the string with whitespace and comments removed. Comments are expected to be delimited with a '#' character followed by at least one whitespace. When a string is uncommented each line will be stripped of leading and trailing whitespace. Comments will be removed from the '#'+whitespace separator to the end of the line.

The prime motivation for this is to allow regular expressions to be commented inline - in raw string literals - similar to Perl's /x modifier.

Example (Compile)

Example of a commented regular expression, uncommenting it and compiling it.

package main

import (
	"fmt"
	"regexp"

	"code.wolfmud.org/WolfMUD.git/text"
)

func main() {
	r := regexp.MustCompile(text.Uncomment(`
		de     # Match 'de'
		.      # Then any character
		[a-z]  # Then a lowercase letter
	`))
	fmt.Println(r.FindString("abcdefghi"))

}
Output:

defg
Example (Simple)

Example of a commented regular expression and uncommenting it.

package main

import (
	"fmt"

	"code.wolfmud.org/WolfMUD.git/text"
)

func main() {
	text := text.Uncomment(`
		(?m)         # Match in multi-line mode
		(?:          # Start a non-capture, alternating group
		  \s*#\s.*$  # Match line ending in a  '#' delimited comment
		|            # OR
		  ^\s+       # Leading whitespace
		|            # OR
		  \s*\n      # Optional trailing whitespace, followed by a new line
		)            # End group
	`)
	fmt.Println(text)

}
Output:

(?m)(?:\s*#\s.*$|^\s+|\s*\n)

func Unfold added in v0.0.14

func Unfold(in []byte) []byte

Unfold unwraps long folded lines in the passed []byte keeping only significant whitespace. Line feeds that create blank lines or are before an indented line are kept. Trailing whitespace before a line feed is removed. For example:

The quick brown \n
fox jumps\n
\n
  over the\n
lazy dog.

Would be unfolded to:

The quick brown fox jumps

  over the lazy dog.

If a line starts with one or more CSIm ANSI escape sequences before any indenting whitespace, the escape sequences will be treated as zero width and ignored preserving the indent.

Types

This section is empty.

Jump to

Keyboard shortcuts

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