string_width

Package Version Hex Docs

gleam add string_width@2
import string_width

pub fn main() {
  string_width.dimensions("hello,\n안녕하세요", [])
  // --> #(2, 10)

  string_width.line("👩‍👩‍👦‍👦", [string_width.HandleGraphemeClusters])
  // --> 2
}

A small package estimating the required number of cells when printing a string on the terminal. It supports all Gleam targets and passes the tests of the NPM string-width package.

Limitations

Sources

The table lookup technique used by this library is heavily based on the musl libc wcwidth implementation. I built updated tables using the Unicode 16.0 data, and added support for ambiguous characters. It also uses the regex of the ansi-regex npm package, and the test cases of the string-width npm package.

Search Document