text

package
v0.0.0-...-de190fe Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2020 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Align

func Align(s string, width int, align Alignment) string

func CountLines

func CountLines(s string) int

CountLines returns the number of lines in s.

func DisplayWidth

func DisplayWidth(s string) int

DisplayWidth returns the display width of s. If s is a multiline string this returns the display width of the longest line.

func IsMultiline

func IsMultiline(s string) bool

IsMultiline returns true if s contains newlines.

func JoinLines

func JoinLines(lines []string) string

JoinLines joins lines together with newline characters.

func MaxDisplayWidth

func MaxDisplayWidth(lines []string) (max int)

MaxDisplayWidth returns the display width of the longest line in the lines slice.

func PadCenter

func PadCenter(s string, width int) string

func PadJustify

func PadJustify(s string, width int) string

func PadLeft

func PadLeft(s string, width int) string

func PadRight

func PadRight(s string, width int) string

func Spaces

func Spaces(num int) string

Spaces returns a string which consist only of the specified number of spaces. Will panic is num is negative.

func SplitLines

func SplitLines(s string) []string

SplitLines splits s on newline characters and returns a slice of lines.

func Truncate

func Truncate(s string, width int) string

Truncate truncates s to a maximum width. The truncated string includes an ellipsis as the last rune. If the display width of s is shorter than width, it is returned unaltered. For truncate to work as expected s must not contain newlines.

func WrapWords

func WrapWords(s string, width int) string

This is based on the naive implementation found here: https://www.rosettacode.org/wiki/Word_wrap#Go

TODO: properly handle ansi escape sequences when wrapping colored strings into multiple lines.

Types

type Alignment

type Alignment int
const (
	AlignLeft Alignment = iota
	AlignRight
	AlignCenter
	AlignJustify
)

type Text

type Text struct {
	// Alignment controls the alignment of the text. Default is to align left.
	Alignment Alignment
	// Style is applied to the text after alignment and word wrapping.
	Style *style.Style
	// Text contains the text that should be rendered. Can contain newlines or
	// even ANSI escape sequences.
	Text string
	// WordWrap controls the word wrapping behaviour. If true, words are
	// wrapped onto multiple lines depending on the desired render width.
	WordWrap bool
}

Text is a console.Renderable that produces aligned and styled text.

func New

func New(text string) Text

New creates a new Text.

func (Text) Measure

func (t Text) Measure(maxWidth int) measure.Measurement

Measure implements console.Renderable.

func (Text) Render

func (t Text) Render(width int) string

Render implements console.Renderable.

Jump to

Keyboard shortcuts

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