Thanks to visit codestin.com
Credit goes to pkg.go.dev

ansiwrap

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2020 License: BSD-3-Clause Imports: 4 Imported by: 6

README

ansiwrap

ANSI escape sequence / unicode aware text wrapping for Go.

Code of Conduct | Contribution Guidelines

GitHub release GoDoc Travis Go Report Card License

Why ansiwrap?

Old text wrappers can't handle ANSI escape sequence formatting, but ansiwrap knows what to do!

Features

  • ANSI escape sequence aware
  • unicode aware
  • Greedy and balanced text wrapping
  • Flexible indenting

Example

Screenshot

Code
package main

import (
	"fmt"
	"github.com/manifoldco/ansiwrap"
)

// text including ANSI formatting
const text = "Sweet jujubes tootsie roll cotton candy lollipop. Marzipan donut" +
	" gummies tiramisu fruitcake sesame snaps icing danish.  Ice cream sesame snaps" +
	" candy lemon drops toffee oat cake pastry gummies soufflé. Candy chocolate cake" +
	" ice cream tiramisu \033[35mmarshmallow\033[0m sesame snaps" +
	" \033[35mmarshmallow\033[0m tootsie roll. Pastry cupcake biscuit. Tiramisu" +
	" chocolate sweet sugar plum. Pastry cupcake tart chocolate cake gingerbread" +
	" toffee cake gummi bears oat cake. Icing ice cream danish danish bonbon" +
	" \033[35mmarshmallow\033[0m \033[35mmarshmallow\033[0m gummi bears chocolate bar." +
	" Sesame snaps icing chocolate liquorice macaroon sugar plum macaroon ice cream." +
	" Tootsie roll sugar plum oat cake jelly toffee. Sweet cotton candy candy" +
	" caramels carrot cake cotton candy tiramisu gummies cheesecake. Sweet marzipan" +
	" topping jujubes sesame snaps. Cupcake cake jelly beans candy canes ice cream" +
	" jelly gummies."

func main() {
	// Add your favorite ANSI escape code library, and terminal width reporter.

	// A bold and blue label
	label := "\033[1;34mCupcake ipsum:\033[0m "

	// count the printable characters in label
	rc := ansiwrap.RuneCount(label)

	// Wrap the text to 80 chars, aligning with the end of the label
	// Lines are wrapped with minimal raggedness, as the text is long enough
	// to have ansiwrap choose the Balanced algorithm.
	fmt.Println(ansiwrap.WrapIndent(label+text, 70, 0, rc))
}

For more examples and documentation, see the GoDoc reference

Documentation

Overview

Package ansiwrap implements ANSI terminal escape code aware text wrapping.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Balanced

func Balanced(str string, width int) string

Balanced is equivalent to Wrap, but always using the Balanced algorithm.

Example
fmt.Println(Balanced("balanced output is much less ragged than greedy output", 40))
Output:

balanced output is much less
ragged than greedy output

func BalancedIndent

func BalancedIndent(str string, width, firstIndent, restIndent int) string

BalancedIndent is equivalent to WrapIndent, but always using the Balanced algorithm.

func Greedy

func Greedy(str string, width int) string

Greedy is equivalent to Wrap, but always using the Greedy algorithm.

Example
fmt.Println(Greedy("balanced output is much less ragged than greedy output", 40))
Output:

balanced output is much less ragged than
greedy output

func GreedyIndent

func GreedyIndent(str string, width, firstIndent, restIndent int) string

GreedyIndent is equivalent to WrapIndent, but always using the Greedy algorithm.

func RuneCount

func RuneCount(str string) int

RuneCount counts the number of printable runes in str. In addition to ignoring non-printable unicode characters, it ignores all ANSI escape sequences.

func Wrap

func Wrap(str string, width int) string

Wrap wraps the given string str so that each line is at most width printable characters wide.

Newlines are inserted at unicode space characters. If the distance between two space characters is larger than width, no newline is inserted.

str is interpreted as UTF-8.

Both non-printable unicode characters, and ANSI terminal escape sequences are ignored for counting purposes.

Wrap selects the best function between Balanced and Greedy, based on the length of str and width.

The Balanced algorithm wraps lineswith minimal raggedness using the 'Divide & Conquer' algorithm described at http://xxyxyz.org/line-breaking/

The Greedy algorithm greedily fills a line as close to width as possible before continuing to the next line.

func WrapIndent

func WrapIndent(str string, width, firstIndent, restIndent int) string

WrapIndent wraps lines following the same rules as Wrap, in addition to exposing optional indent values firstIndent and restIndent.

firstIndent will indent the first line by that number of spaces. restIndent will indent all remaining lines by that number of spaces. Indent values are taken into account when calculating wrapping.

Example (FirstIndent)
fmt.Println("Example:")
fmt.Println(WrapIndent("firstIndent can create an indent", 12, 4, 0))
Output:

Example:
    firstIndent can
create an indent
Example (RestIndent)
fmt.Println(WrapIndent("restIndent can create a hanging indent", 12, 0, 2))
Output:

restIndent
  can create
  a hanging
  indent

Types

This section is empty.

Jump to

Keyboard shortcuts

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