https://github.com/michaelmure/go-term-text
A collection of algorithm to format text for the terminal
https://github.com/michaelmure/go-term-text
Last synced: 13 days ago
JSON representation
A collection of algorithm to format text for the terminal
- Host: GitHub
- URL: https://github.com/michaelmure/go-term-text
- Owner: MichaelMure
- License: mit
- Created: 2019-07-14T22:54:14.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2023-05-19T05:08:33.000Z (almost 2 years ago)
- Last Synced: 2025-03-27T14:21:56.188Z (about 1 month ago)
- Language: Go
- Size: 162 KB
- Stars: 51
- Watchers: 3
- Forks: 2
- Open Issues: 6
-
Metadata Files:
- Readme: Readme.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# go-term-text

[](https://godoc.org/github.com/MichaelMure/go-term-text)
[](https://goreportcard.com/report/github.com/MichaelMure/go-term-text)
[](https://codecov.io/gh/MichaelMure/go-term-text)
[](https://github.com/MichaelMure/go-term-text/blob/master/LICENSE)
[](https://gitter.im/the-git-bug/Lobby)`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 sequencesIncluded algorithms cover:
- wrapping with padding and indentation
- padding
- text length
- trimming
- alignment
- escape sequence extraction and reapplication
- escape sequence snapshot and simplification
- truncation## Example
```go
package mainimport (
"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:

For more details, have a look at the [GoDoc](https://godoc.org/github.com/MichaelMure/go-term-text).
## Origin
This package has been extracted from the [git-bug](https://github.com/MichaelMure/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