Launch Week Day 5: Introducing Reachability for PHP.Learn More
Socket
Book a DemoSign in
Socket

zgo.at/termtext

Package Overview
Dependencies
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

zgo.at/termtext

Go Modules
Version
v1.5.0
Version published
Created
Source

termtext is a Go package to deal with monospace text as interpreted by terminals.

Mostly intended to neatly align/wrap stuff terminal. There are a few tricky bits with this:

  • Multiple codepoints can be combined to render one character (or "grapheme cluster" in Unicode speak).
  • Some characters are rendered as double-width, such as East-Asian characters, some emojis, and a number of others.
  • A single tab can render as multiple spaces, and the number of spaces depends on its position in the string.
  • Some characters aren't actually printed to the screen, such as the zero-width space and escape sequences to set the colour.

This package takes care of these issue.

Import as zgo.at/termtext – godoc: https://godocs.io/zgo.at/termtext

The main function is termtext.Width():

Width("\ta")                → 9    Tab expands to 8 spaces, followed by "a".
Width("a\t")                → 8    Tab expands to 7 spaces.
Width("🧑\t🧑")             → 10   🧑 is double-width
Width("\x1b[1mbold\x1b[0m") → 4    Escape sequences are ignored.

You can configure the tab width by setting termtext.TabWidth.

There are a few other functions too:

Expand()            Expand tabs.
Slice()             Slice a string by display width, like str[n:m].

AlignLeft()         Align a string, filling up the remainder with spaces.
AlignRight()
AlignCenter()

Wrap()              Wrap a string. This is a simple wrap which just breaks
                    if a line's width is too long.
WordWrap()          Word-wrap a string: lines are at most w wide, but don't
                    break in the middle of words.

FAQs

Package last updated on 22 May 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts