termtext – zgo.at/termtext Index | Files

package termtext

import "zgo.at/termtext"

Package termtext deals with monospace text as interpreted by terminals.

Index

Variables

var TabWidth = 8

Number of spaces to count a tab as.

var Widths map[rune]int

Widths sets the width for the given runes.

This can be useful in some cases where the written text is different. For example when encoding JSON some characters get written as a \-escape. To account for this, you can override the width for those:

termtext.Escapes = map[rune]int{
	'\b': 2,
	'\f': 2,
	'\n': 2,
	'\r': 2,
	'\t': 2,
}

Functions

func AlignCenter

func AlignCenter(s string, w int) string

AlignCenter centre-aligns a string, filling up any remaining width with spaces.

Tabs will be expanded to spaces.

func AlignLeft

func AlignLeft(s string, w int) string

AlignLeft left-aligns a string, filling up any remaining width with spaces.

Tabs will be expanded to spaces.

func AlignRight

func AlignRight(s string, w int) string

AlignRight right-aligns a string, filling up any remaining width with spaces.

Tabs will be expanded to spaces.

func Expand

func Expand(s string) string

Expand tabs to spaces.

func Slice

func Slice(s string, start, stop int) string

Slice a string by character index. This works the same as str[n:m] slicing.

Tabs will be expanded to spaces.

func Width

func Width(s string) int

Width gets the display width of a string.

The "display width" is the number of columns a string will occupy in a monospace terminal.

func WordWrap

func WordWrap(s string, w int, prefix string) string

WordWrap wraps lines to be at most w characters wide, but doesn't break in the middle of words.

Lines will be prefixed with prefix. The prefix isn't counted in line length calculations.

Tabs will be expanded to spaces.

func Wrap

func Wrap(s string, w int, prefix string) string

Wrap lines to be at most w characters wide.

Lines will be prefixed with prefix. The prefix isn't counted in line length calculations.

This does not use word wrap, use WordWrap() for this instead.

Tabs will be expanded to spaces.

Source Files

termtext.go

Version
v1.5.0 (latest)
Published
May 22, 2024
Platform
linux/amd64
Imports
5 packages
Last checked
3 hours ago

Tools for package owners.