Categorygithub.com/MichaelMure/go-term-text
modulepackage
0.3.1
Repository: https://github.com/michaelmure/go-term-text.git
Documentation: pkg.go.dev

# README

go-term-text

Build Status GoDoc Go Report Card codecov GitHub license Gitter chat

go-term-text is a go package implementing a collection of algorithms to help format and manipulate text for the terminal.

In particular, go-term-text:

  • support wide characters (chinese, japanese ...) and emoji
  • handle properly ANSI escape sequences

Included algorithms cover:

  • wrapping with padding and indentation
  • padding
  • text length
  • trimming
  • alignment
  • escape sequence extraction and reapplication
  • escape sequence snapshot and simplification
  • truncation

Example

package main

import (
	"fmt"
	"strings"

	"github.com/MichaelMure/go-term-text"
)

func main() {
	input := "The \x1b[1mLorem ipsum\x1b[0m text is typically composed of " +
    		"pseudo-Latin words. It is commonly used as \x1b[3mplaceholder\x1b[0m" +
    		" text to examine or demonstrate the \x1b[9mvisual effects\x1b[0m of " +
    		"various graphic design. 一只 A Quick \x1b[31m敏捷的狐 Fox " +
    		"狸跳过了\x1b[0mDog一只懒狗。"

	output, n := text.Wrap(input, 60,
            text.WrapIndent("\x1b[34m<-indent-> \x1b[0m"),
            text.WrapPad("\x1b[33m<-pad-> \x1b[0m"),
    )

	fmt.Printf("output has %d lines\n\n", n)

	fmt.Println("|" + strings.Repeat("-", 58) + "|")
	fmt.Println(output)
	fmt.Println("|" + strings.Repeat("-", 58) + "|")
}

This will print:

example output

For more details, have a look at the GoDoc.

Origin

This package has been extracted from the git-bug project. As such, its aim is to support this project and not to provide an all-in-one solution. Contributions as welcome though.

Contribute

PRs accepted.

License

MIT

# Packages

No description provided by the author

# Functions

ApplyTermEscapes apply the extracted terminal escapes to the edited line.
ExtractTermEscapes extract terminal escapes out of a line and returns a new line without terminal escapes and a slice of escape items.
LeftPad left pad each line of the given text.
LeftPadMaxLine pads a line on the left by a specified amount and pads the string on the right to fill the maxLength.
Len return the length of a string in a terminal, while ignoring the terminal escape sequences.
LineAlign align the given line as asked and apply the needed padding to match the given lineWidth, while ignoring the terminal escape sequences.
LineAlignCenter align the given line on the center and apply the needed left padding, while ignoring the terminal escape sequences.
LineAlignLeft align the given line on the left while ignoring the terminal escape sequences.
LineAlignRight align the given line on the right and apply the needed left padding to match the given lineWidth, while ignoring the terminal escape sequences.
MaxLineLen return the length in a terminal of the longest line, while ignoring the terminal escape sequences.
OffsetEscapes is a utility function to offset the position of a collection of EscapeItem.
TrimSpace remove the leading and trailing whitespace while ignoring the terminal escape sequences.
TruncateMax truncate a line if its length is greater than the given length.
Wrap a text for a given line size.
WrapAlign configure the text alignment for Wrap().
WrapPad configure the indentation on the first line for Wrap().
WrapLeftPadded wrap a text for a given line size with a left padding.
WrapPad configure the padding with a string for Wrap().
WrapPadded configure the padding with a number of space characters for Wrap().
WrapWithPad wrap a text for a given line size with a custom left padding Handle properly terminal color escape code.
WrapWithPad wrap a text for a given line size with a custom left padding This function also align the result depending on the requested alignment.
WrapWithPadIndent wrap a text for a given line size with a custom left padding and a first line indent.
WrapWithPadIndentAlign wrap a text for a given line size with a custom left padding and a first line indent.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

No description provided by the author
No description provided by the author
EscapeItem hold the description of terminal escapes in a line.
No description provided by the author

# Interfaces

No description provided by the author

# Type aliases

No description provided by the author
No description provided by the author
No description provided by the author
WrapOption is a functional option for the Wrap() function.