Current section

Files

Jump to
string_width src string_width.gleam
Raw

src/string_width.gleam

import gleam/int
import gleam/list
import gleam/string
import string_width/internal/ansi
import string_width/internal/tables
// sources:
// UAX #11 east asian width: https://www.unicode.org/reports/tr11/
// Grapheme Clusters in Terminals: https://mitchellh.com/writing/grapheme-clusters-in-terminals
// get-east-asian-width: https://github.com/sindresorhus/get-east-asian-width
// string-width: https://github.com/sindresorhus/string-width
// ansi-regex: https://github.com/chalk/ansi-regex
// fast-string-truncated-width: https://github.com/fabiospampinato/fast-string-truncated-width/
// musl-libc wcwidth: https://git.musl-libc.org/cgit/musl/tree/src/ctype/wcwidth.c
// Mode 2027: https://github.com/contour-terminal/terminal-unicode-core
// https://gitlab.freedesktop.org/terminal-wg/specifications/-/issues/36
// -- OPTIONS ------------------------------------------------------------------
/// Options to change the default behaviour of the functions in this library.
/// If you are unsure what to do here, the defaults should work great!
pub opaque type Options {
Options(
count_ansi_codes: Bool,
ambiguous_as_wide: Bool,
mode: Mode,
tab_width: Int,
tab_offset: Int,
)
}
type Mode {
ModeWcwidth
Mode2027
Mode2027Ext
}
const default_options = Options(
count_ansi_codes: False,
ambiguous_as_wide: False,
mode: ModeWcwidth,
tab_width: 8,
tab_offset: 0,
)
/// Start building up new options.
pub fn new() -> Options {
default_options
}
/// Measure grapheme clusters instead of individual code points.
///
/// Most terminal emulators do not handle grapheme clusters well and will
/// instead show their decomposition. To make sure a given string always fits
/// even on those terminals, the functions in this package will copy this
/// behaviour as well.
///
/// If you enable this option, the returned numbers will more accurately represent
/// the width of a string on a website or in an editor.
///
/// **NOTE:** Grapheme support in terminals is highly experimental and subject
/// to ongoing discussion! When working on CLIs or TUIs, you do not want to use
/// this option most of the time.
///
/// See also [Grapheme Clusters and Terminal Emulators](https://mitchellh.com/writing/grapheme-clusters-in-terminals)
/// for a better explanation on how terminals behave.
pub fn mode_2027(options: Options) -> Options {
Options(..options, mode: Mode2027)
}
/// Measure grapheme clusters instead of individual code points, and always
/// treat grapheme clusters with more than one non-modifier character as wide.
///
/// This is a custom extension to Unicode Core. Many single grapheme clusters
/// would currently still render as wide glyphs in many contexts, since font
/// shaping would not be able to collapse them into a single narrow "ligature".
///
/// This mode is an additional heuristic that tries to handle more such cases.
///
/// See `mode_2027` for more explanation on the implications.
pub fn mode_2027_ext(options: Options) -> Options {
Options(..options, mode: Mode2027Ext)
}
/// The default mode and the mode suitable for most terminals.
///
/// Measures individual code points, similar to the libc `wcwidth` function
/// that almost all of these terminals use.
///
/// Note that except for `fold_raw`, this library will still always process
/// grapheme clusters as a unit.
pub fn mode_wcwidth(options: Options) -> Options {
Options(..options, mode: ModeWcwidth)
}
/// Some characters are marked by Unicode as "ambiguous", meaning they may
/// occupy 1 or 2 cells, depending on the context, current language, selected
/// font, surrounding text, and more.
///
/// Unicode recommends treating these characters as narrow by default,
/// but you can change this behaviour using this option.
pub fn ambiguous_as_wide(options: Options) -> Options {
Options(..options, ambiguous_as_wide: True)
}
/// Do not ignore ansi escape sequences, and count them as regular characters.
///
/// You can enable this option as an optimisation if you are sure that your
/// string doesn't contain any ansi escape codes.
pub fn count_ansi_codes(options: Options) -> Options {
Options(..options, count_ansi_codes: True)
}
/// Change the number of columns between tab stops. (Default: 8)
///
/// Whenever a tab character `\t` is encountered, the current column number will
/// be rounded up to the next multiple of this number.
pub fn with_tab_width(options: Options, tab_width: Int) -> Options {
Options(..options, tab_width:)
}
/// Define an _offset_ for tab stop calculations. (Default: 0)
///
/// If you do print text not at the first column, but indented by some spaces,
/// you can set this option to this number to make sure tab stops are correctly
/// calculated.
pub fn at_tab_offset(options: Options, tab_offset: Int) -> Options {
Options(..options, tab_offset:)
}
// -- MEASURING STRINGS --------------------------------------------------------
/// A custom type making it easy to differentiate the width (columns) and
/// height (rows).
pub type Size {
Size(rows: Int, columns: Int)
}
/// Get the number of columns required to print a line in a terminal.
///
/// If multiple lines are given, the length of the longest line is returned.
///
/// ### Examples
///
/// ```gleam
/// line("äöüè")
/// // --> 4
///
/// line("안녕하세요")
/// // --> 10
///
/// line("👩‍👩‍👦‍👦")
/// // --> 8
///
/// line("\u{1B}[31mhello\u{1B}[39m")
/// // --> 5
/// ```
pub fn line(str: String) -> Int {
let Size(columns:, rows: _) = dimensions_with(str, default_options)
columns
}
/// Like `line`, but use custom options.
///
/// ### Example
///
/// ```gleam
/// let options =
/// new()
/// |> mode
///
/// line_with("👩‍👩‍👦‍👦", options)
/// // --> 2
/// ```
pub fn line_with(str: String, options: Options) -> Int {
let Size(columns:, rows: _) = dimensions_with(str, options)
columns
}
/// The required number of rows and columns to display the given text.
///
/// The number of rows is equal to the number of lines, while the number of
/// columns represents the maximum line width.
///
/// ### Examples
///
/// ```gleam
/// dimensions("안녕하세요")
/// // --> #(1, 10)
///
/// dimensions("hello,\n안녕하세요")
/// // --> #(2, 10)
/// ```
pub fn dimensions(str: String) -> Size {
dimensions_with(str, default_options)
}
/// Like `dimensions`, but use custom options.
pub fn dimensions_with(str: String, options: Options) -> Size {
let #(str, ranges, range_width) = prepare_measure(options, str)
let fun = fn(state, chr, width) {
let #(rows, cols_max, cols_curr) = state
case chr {
"\n" -> #(rows + 1, int.max(cols_max, cols_curr), options.tab_offset)
"\t" -> #(rows, cols_max, tab(options, cols_curr))
_ -> #(rows, cols_max, cols_curr + width)
}
}
let on_range = fn(state, ansi, length) {
fun(state, ansi, range_width * length)
}
let on_chars = case options.mode {
ModeWcwidth -> fn(state, str) { fold_chars_raw(options, str, state, fun) }
Mode2027 -> fn(state, str) { fold_chars_2027(options, str, state, fun) }
Mode2027Ext -> fn(state, str) {
fold_chars_2027_ext(options, str, state, fun)
}
}
let #(rows, cols_max, cols_curr) =
fold_parts(str, 0, ranges, #(0, 0, options.tab_offset), on_chars, on_range)
let columns = int.max(cols_max, cols_curr) - options.tab_offset
case cols_curr > 0 {
True -> Size(rows: rows + 1, columns:)
False -> Size(rows:, columns:)
}
}
/// Round up to the next tab boundary.
fn tab(options: Options, col: Int) -> Int {
{ col / options.tab_width + 1 } * options.tab_width
}
// magic hook that allows us to not be stupid with FFI and still get
// _most_ of the performance (we are now like 50% slower, or back to old speed).
@external(javascript, "./string_width_ffi.mjs", "prepare_measure")
fn prepare_measure(
options: Options,
str: String,
) -> #(String, List(#(Int, Int)), Int) {
let ansi_ranges = case options.count_ansi_codes {
True -> []
False -> ansi.match(str)
}
#(str, ansi_ranges, 0)
}
// -- LAYOUT UTILS -------------------------------------------------------------
/// Limit the dimensions of a string, either by wrapping on white space
/// characters, or by truncating the last line and appending an ellipsis.
///
/// The lines of the resulting string are left aligned and are at most `columns`
/// wide. If a single word is longer than the maximum allowed line width, the
/// word is broken into 2 pieces at the grapheme level. If the string gets
/// truncated, this function will keep collecting ANSI sequences to make sure
/// all formatting is preserved.
///
/// A commonly used ellipsis character is `"…"`, also known as
/// U+2026 HORIZONTAL ELLIPSIS.
///
/// ### Examples
///
/// ```gleam
/// limit("Hello World", Size(rows: 1, columns: 10), ellipsis: "...")
/// // --> "Hello W..."
///
/// limit("Hello World", Size(rows: 2, columns: 5), ellipsis: "...")
/// // --> "Hello\nWorld"
/// ```
pub fn limit(
str: String,
to max_size: Size,
ellipsis ellipsis: String,
) -> String {
limit_with(str, max_size, default_options, ellipsis)
}
type LimitState {
LimitState(
str: String,
row: Int,
col: Int,
spaces: String,
spaces_width: Int,
non_spaces: String,
non_spaces_width: Int,
overflow: String,
overflow_width: Int,
)
}
fn limit_state_col(state: LimitState) {
state.col + state.spaces_width + state.non_spaces_width + state.overflow_width
}
/// Like `limit`, but also customise the options used for measuring.
pub fn limit_with(
str: String,
to max_size: Size,
using options: Options,
ellipsis ellipsis: String,
) -> String {
let ellipsis_width = line_with(ellipsis, options)
let initial =
LimitState(
str: "",
row: 0,
col: 0,
spaces: "",
spaces_width: 0,
non_spaces: "",
non_spaces_width: 0,
overflow: "",
overflow_width: 0,
)
// we encode a few states, but we do it this way such that spread works...
// - if state.row == max_size.rows, we have reached the end and should do nothing
// - if we have oveflow, we are at the last line and stuff doesn't fit.
// - if we have non_spaces or overflow, we are collecting word characters.
// - if we do not, we are collecting spaces _before_ a word.
// - on the first space we collect, if we notice that we have word characters,
// we try to push this word into the accumulator.
// if it doesn't fit on the last line, we have overflow, and can append the ellipsis.
// afterwards, we set row = max_sie.rows to indicate we are finished.
let push_space = fn(state: LimitState, piece: String, width: Int) {
let new_col = limit_state_col(state)
// make sure we definitely have room for the ellipsis on the last line
let max_col = case state.row + 1 >= max_size.rows {
True -> max_size.columns - ellipsis_width
False -> max_size.columns
}
case state.non_spaces == "" && state.overflow == "" {
// we do not have non-spaces, so we are still collecting spaces.
True ->
case state.str {
// skip spaces at the start, since we skip them at the end.
"" -> state
_ ->
LimitState(
..state,
spaces: state.spaces <> piece,
spaces_width: state.spaces_width + width,
)
}
// we have non-spaces, so we want to push this word into the accumulator
// and then start again collecting spaces.
False ->
// would it fit into the line just normally?
case new_col <= max_col {
True -> {
// it fits normally, nothing special required!
let str =
state.str <> state.spaces <> state.non_spaces <> state.overflow
// does our new space fit though?
case state.row + 1 >= max_size.rows && new_col + width > max_col {
False ->
LimitState(
str:,
row: state.row,
col: new_col,
spaces: piece,
spaces_width: width,
non_spaces: "",
non_spaces_width: 0,
overflow: "",
overflow_width: 0,
)
True ->
LimitState(
str:,
row: state.row,
col: new_col,
spaces: "",
spaces_width: 0,
non_spaces: "",
non_spaces_width: 0,
overflow: piece,
overflow_width: width,
)
}
}
False ->
// it doesn't; if we are at the last line, truncate. else, wrap.
case state.row + 1 >= max_size.rows {
True ->
// we are at the last line.
LimitState(
..state,
str: state.str <> state.spaces <> state.non_spaces <> ellipsis,
row: state.row + 1,
)
False ->
LimitState(
str: state.str <> "\n" <> state.non_spaces <> state.overflow,
row: state.row + 1,
col: state.non_spaces_width + state.overflow_width,
spaces: piece,
spaces_width: width,
non_spaces: "",
non_spaces_width: 0,
overflow: "",
overflow_width: 0,
)
}
}
}
}
let state = {
// TODO: this may be fold_raw-able
use state, piece <- fold_with(str, options, initial)
case state.row >= max_size.rows {
// reached the end, but we still want to collect ansi sequences
True ->
case is_ansi_component(piece.piece, options) {
True -> LimitState(..state, str: state.str <> piece.piece)
False -> state
}
False ->
case piece.piece {
// Line break that we recognise as such
"\n" | "\r\n" -> {
let state = push_space(state, "", 0)
LimitState(
..state,
str: state.str <> "\n",
row: state.row + 1,
col: 0,
)
}
"\t"
| // White_Space=Y, Non line-break
" "
| "\u{a0}"
| "\u{1680}"
| "\u{2000}"
| "\u{2001}"
| "\u{2002}"
| "\u{2003}"
| "\u{2004}"
| "\u{2005}"
| "\u{2006}"
| "\u{2007}"
| "\u{2008}"
| "\u{2009}"
| "\u{200a}"
| "\u{202f}"
| "\u{205f}"
| "\u{3000}" -> push_space(state, piece.piece, piece.width)
// not a space or a line break
_ -> {
let max_col = case state.row + 1 >= max_size.rows {
True -> max_size.columns - ellipsis_width
False -> max_size.columns
}
case limit_state_col(state) + piece.width > max_col {
True -> {
// these non_space characters no longer fit the line,
// but what if overflow is too long as well?
case
state.non_spaces_width + state.overflow_width + piece.width
> max_size.columns
{
True ->
// it _is_! do we have room for another row?
case state.row + 1 < max_size.rows {
True ->
LimitState(
str: state.str
<> state.spaces
<> state.non_spaces
<> "\n",
col: 0,
row: state.row + 1,
non_spaces: state.overflow <> piece.piece,
non_spaces_width: state.overflow_width + piece.width,
spaces: "",
spaces_width: 0,
overflow: "",
overflow_width: 0,
)
False ->
// no, word to long this is the last row, sorry.
LimitState(
..state,
str: state.str
<> state.spaces
<> state.non_spaces
<> ellipsis,
row: state.row + 1,
)
}
False ->
LimitState(
..state,
overflow: state.overflow <> piece.piece,
overflow_width: state.overflow_width + piece.width,
)
}
}
False ->
// everything's fine and normal :)
LimitState(
..state,
non_spaces: state.non_spaces <> piece.piece,
non_spaces_width: state.non_spaces_width + piece.width,
)
}
}
}
}
}
case
state.row >= max_size.rows
|| { state.non_spaces == "" && state.overflow == "" }
{
// either reached the end, or we have no word characters left.
True -> state.str
False -> {
let new_col = limit_state_col(state)
// would it fit into the line just normally?
case new_col <= max_size.columns {
True -> state.str <> state.spaces <> state.non_spaces <> state.overflow
False ->
// it doesn't; if we are at the last line, truncate. else, wrap.
case state.row + 1 >= max_size.rows {
True -> state.str <> state.spaces <> state.non_spaces <> ellipsis
False -> state.str <> "\n" <> state.non_spaces <> state.overflow
}
}
}
}
}
/// Replace all tab characters found in the string with the amount of spaces
/// that this tab would have had otherwise.
///
/// This makes it save to prepend to a line, without changing the spacing produced
/// by tabs anymore.
///
/// ### Examples
///
/// ```gleam
/// tabs_to_spaces("Hello\tWorld")
/// // --> "Hello World" // 3 spaces
/// ```
pub fn tabs_to_spaces(str: String) -> String {
tabs_to_spaces_with(str, default_options)
}
/// Like `tabs_to_spaces`, but customise the opttions as well.
///
/// **NB:** You can use `at_tab_offset` and `with_tab_width` to change the measure of
/// tab characters inside the string!
///
/// ```gleam
/// let options = new() |> with_tab_width(4)
/// tabs_to_spaces_with("hi\tcutie~", options)
/// // --> "hi cutie~"
/// ```
pub fn tabs_to_spaces_with(str: String, using options: Options) -> String {
use acc, piece <- fold_with(str, options, from: "")
case piece.piece {
"\t" -> acc <> string.repeat(" ", piece.width)
_ -> acc <> piece.piece
}
}
/// Control the text or box alignment on the horizontal axis.
pub type Alignment {
Left
Center
Right
}
/// Control the box alignment on the vertical axis.
pub type Placement {
Top
Middle
Bottom
}
/// Position the string area inside a bigger box without changing text alignment.
/// The box will be filled with the space character.
///
/// If a line is already bigger than the maximum width, it will not be changed.
/// If there are more lines than the maximum amount of rows, the extra lines
/// will still be kept.
/// If the space strings' width does not evenly divide the missing amount of
/// columns, the extra spacer will overflow the max width.
///
/// ### Examples
///
/// ```gleam
/// position("X", in: Size(3, 3), align: Center, place: Middle, with: "o")
/// // --> "ooo\noXo\nooo"
/// ```
pub fn position(
str: String,
in bounding_box: Size,
align alignment: Alignment,
place placement: Placement,
with space: String,
) -> String {
position_with(str, bounding_box, alignment, placement, default_options, space)
}
/// Like `position`, but use custom options to measure each line.
pub fn position_with(
str: String,
in bounding_box: Size,
horizontal alignment: Alignment,
vertical placement: Placement,
using options: Options,
with space: String,
) -> String {
let space_width = line_with(space, options)
let size = dimensions_with(str, options)
let align = case alignment {
Left -> fn(line, line_width) {
let missing = div_up(bounding_box.columns - line_width, space_width)
line <> string.repeat(space, missing)
}
Right -> {
let missing_left =
div_up(bounding_box.columns - size.columns, space_width)
let space_left = string.repeat(space, missing_left)
fn(line, line_width) {
let missing =
{ bounding_box.columns - line_width } / space_width - missing_left
space_left <> line <> string.repeat(space, missing)
}
}
Center -> {
let missing_total =
div_up(bounding_box.columns - size.columns, space_width)
let missing_left = missing_total / 2
let space_left = string.repeat(space, missing_left)
let space_right = string.repeat(space, missing_total - missing_left)
fn(line, line_width) {
let missing =
{ bounding_box.columns - line_width } / space_width - missing_total
space_left <> line <> string.repeat(space, missing) <> space_right
}
}
}
let str = do_align(str, options, align)
let missing_rows = bounding_box.rows - size.rows
case missing_rows > 0 {
True -> {
let space_row =
string.repeat(space, div_up(bounding_box.columns, space_width))
case placement {
Top -> str <> string.repeat("\n" <> space_row, missing_rows)
Bottom -> string.repeat(space_row <> "\n", missing_rows) <> str
Middle -> {
let top = missing_rows / 2
let bottom = missing_rows - top
string.repeat(space_row <> "\n", top)
<> str
<> string.repeat("\n" <> space_row, bottom)
}
}
}
False -> str
}
}
/// Align each line horizontally, using a spacer character or string as a filler.
/// Each line of the return value will be at least `max_width` wide.
///
/// If a line is already longer than the max width, it will not be changed.
/// If the space strings width does not evenly divide the missing amount of
/// columns, the extra spacer will overflow the max width. If there is an uneven
/// amount of lines while vertically centering, box will be slightly pushed
/// towards the top.
///
/// ### Examples
///
/// ```gleam
/// align("hello", to: 10, align: Left, with: " ")
/// // --> "hello "
///
/// align("12345", to: 8, align: Right, with: "0")
/// // --> "00012345"
///
/// align(" Welcome ", to: 20, align: Center, with: "==")
/// // --> "====== Welcome ======" // (len = 21)
/// ```
pub fn align(
str: String,
to max_width: Int,
align alignment: Alignment,
with space: String,
) -> String {
align_with(str, max_width, alignment, default_options, space)
}
/// Like `align`, bu use custom options for measure.
pub fn align_with(
str: String,
to max_width: Int,
align alignment: Alignment,
using options: Options,
with space: String,
) -> String {
let space_width = line_with(space, options)
use line, line_width <- do_align(str, options)
let missing = div_up(max_width - line_width, space_width)
case missing > 0 {
True ->
case alignment {
Left -> line <> string.repeat(space, missing)
Right -> string.repeat(space, missing) <> line
Center -> {
let left = missing / 2
let right = missing - left
string.repeat(space, left) <> line <> string.repeat(space, right)
}
}
False -> line
}
}
fn do_align(str, options, align: fn(String, Int) -> String) {
let push_line = fn(state) {
let #(acc, line, line_width) = state
let line = align(line, line_width)
case acc {
"" -> #(line, "", 0)
_ -> #(acc <> "\n" <> line, "", 0)
}
}
let state = #("", "", 0)
let state = {
use state, piece <- fold_with(str, options, state)
let #(acc, line, line_width) = state
case piece.piece {
"\n" | "\n\r" -> push_line(state)
"\t" -> #(
acc,
line <> string.repeat(" ", piece.width),
line_width + piece.width,
)
_ -> #(acc, line <> piece.piece, line_width + piece.width)
}
}
let #(result, _, _) = push_line(state)
result
}
/// Integer division, rounding up
fn div_up(numerator: Int, denom: Int) {
{ numerator + denom - 1 } / denom
}
// -- FOLD ---------------------------------------------------------------------
/// Returns true if a given component string recieved in `fold` is an ANSI
/// escape sequence.
pub fn is_ansi_component(str: String, options: Options) -> Bool {
case options.count_ansi_codes {
True -> False
False ->
case str {
"\u{1b}" <> _ | "\u{9b}" <> _ | "\u{9d}" <> _ -> True
_ -> False
}
}
}
/// The measured pieces of a string, passed to you by `fold` and `fold_with`
pub type Piece {
Piece(piece: String, row: Int, column: Int, width: Int)
}
/// A higher-level `fold` that keeps track of the position inside of the string.
///
/// Handles tabs and newlines, and always passes full grapheme clusters,
/// regardless of options. Concatenating the graphemes produces the original string.
///
/// The `with` function is called with `(state, grapheme, width, row, col)`.
pub fn fold(
over str: String,
from state: state,
with fun: fn(state, Piece) -> state,
) -> state {
fold_with(str, default_options, state, fun)
}
/// A higher-level `fold` that keeps track of the position inside of the string
///
/// Handles tabs and newlines, and always passes full grapheme clusters,
/// regardless of options. Concatenating the graphemes produces the original string.
///
/// The `with` function is called with `(state, grapheme, width, row, col)`.
pub fn fold_with(
over str: String,
using options: Options,
from state: state,
with fun: fn(state, Piece) -> state,
) -> state {
let fun = fn(state, piece, width) {
let #(state, row, column) = state
let width = case piece {
"\t" -> tab(options, column) - column
_ -> width
}
let state = fun(state, Piece(piece:, row:, column:, width:))
case piece {
"\n" -> #(state, row + 1, options.tab_offset)
_ -> #(state, row, column + width)
}
}
let on_chars = case options.mode {
ModeWcwidth -> fn(state, str) {
fold_chars_wcwidth(options, str, state, fun)
}
Mode2027 -> fn(state, str) { fold_chars_2027(options, str, state, fun) }
Mode2027Ext -> fn(state, str) {
fold_chars_2027_ext(options, str, state, fun)
}
}
let ansi_ranges = case options.count_ansi_codes {
True -> []
False -> ansi.match(str)
}
let on_range = fn(state, ansi, _) { fun(state, ansi, 0) }
let initial = #(state, 0, options.tab_offset)
let #(state, _, _) =
fold_parts(str, 0, ansi_ranges, initial, on_chars, on_range)
state
}
/// Iterate over the measured components of a string. Components are either
/// graphemes or codepoints, depending on the `mode` option,
/// or other undivisible sequences, like ANSI escape codes.
///
/// This is a lower-level utility compared to the others in this package. It does
/// not by itself keep track of any additional state, and does not for example
/// handle newlines or tabs. Instead, you can use fold to implement all kinds of
/// higher-level layout primitives.
///
/// Concatenating all components is guaranteed to produce the original string.
///
/// ### Examples
///
/// ```gleam
/// // A slower string_width.line implementation that doesn't handle tabs
/// fold_raw("hello", new(), from: 0, with: fn(so_far, _chr, width) { width + so_far })
/// // --> 5
/// ```
///
/// ```gleam
/// // truncate a string after 50 characters, but keep all ansi sequences.
/// use #(total, acc), chr, width <- fold_raw(input, new(), from: #(0, ""))
/// case total >= 50 {
/// True -> case chr {
/// "\u{1b}" <> _ -> #(total, acc <> chr)
/// _ -> #(total, acc)
/// }
///
/// False -> #(total + width, acc <> chr)
/// }
/// ```
pub fn fold_raw(
over string: String,
using options: Options,
from state: state,
with fun: fn(state, String, Int) -> state,
) -> state {
let ansi_ranges = case options.count_ansi_codes {
True -> []
False -> ansi.match(string)
}
let on_chars = case options.mode {
ModeWcwidth -> fn(state, str) { fold_chars_raw(options, str, state, fun) }
Mode2027 -> fn(state, str) { fold_chars_2027(options, str, state, fun) }
Mode2027Ext -> fn(state, str) {
fold_chars_2027_ext(options, str, state, fun)
}
}
let on_range = fn(state, ansi, _) { fun(state, ansi, 0) }
fold_parts(string, 0, ansi_ranges, state, on_chars, on_range)
}
fn fold_parts(
over string: String,
at offset: Int,
ranges ranges: List(#(Int, Int)),
from state: state,
on_chars on_chars: fn(state, String) -> state,
on_range on_range: fn(state, String, Int) -> state,
) -> state {
case ranges {
[#(start, length), ..ranges] if start == offset -> {
let #(range_slice, string) = unsafe_split(string, length)
let state = on_range(state, range_slice, length)
let offset = start + length
fold_parts(string, offset, ranges, state, on_chars, on_range)
}
[#(start, length), ..ranges] -> {
let #(before, at_range) = unsafe_split(string, start - offset)
let #(range_slice, string) = unsafe_split(at_range, length)
let state =
state
|> on_chars(before)
|> on_range(range_slice, length)
let offset = start + length
fold_parts(string, offset, ranges, state, on_chars, on_range)
}
[] ->
case string {
"" -> state
_ -> on_chars(state, string)
}
}
}
fn fold_chars_raw(
options: Options,
string: String,
state: state,
fun: fn(state, String, Int) -> state,
) -> state {
use state, cp <- fold_codepoints(string, state)
fun(state, utf_codepoint_to_string(cp), wcwidth(options, cp))
}
fn fold_chars_wcwidth(
options: Options,
string: String,
state: state,
fun: fn(state, String, Int) -> state,
) -> state {
use state, grapheme <- fold_graphemes(string, state)
let width = {
use acc, cp <- fold_codepoints(grapheme, 0)
acc + wcwidth(options, cp)
}
fun(state, grapheme, width)
}
fn fold_chars_2027(
options: Options,
string: String,
state: state,
fun: fn(state, String, Int) -> state,
) -> state {
use state, grapheme <- fold_graphemes(string, state)
let width = measure_mode_2027(options, grapheme)
fun(state, grapheme, width)
}
fn fold_chars_2027_ext(
options: Options,
string: String,
state: state,
fun: fn(state, String, Int) -> state,
) -> state {
use state, grapheme <- fold_graphemes(string, state)
let width = measure_mode_2027_ext(options, grapheme)
fun(state, grapheme, width)
}
fn measure_mode_2027(options: Options, string: String) -> Int {
use max, cp <- fold_codepoints(string, 0)
case cp {
// unicode core: VS16 forces a cluster to be wide
0xfe0f -> 2
_ -> int.max(max, wcwidth(options, cp))
}
}
fn measure_mode_2027_ext(options: Options, string: String) -> Int {
// The Unicode Core spec (https://github.com/contour-terminal/terminal-unicode-core)
// proposing Mode 2027 says the following:
//
// > Therefore, extending a grapheme cluster with consecutively added codepoints
// > will not move the cursor except for variation selector 16 (VS16) that may
// > have caused the width of the grapheme cluster to change to wide (2 grid cells).
//
// I do not agree.
//
// Many grapheme clusters include multiple letters with a width, which cannot
// be printed and combined into a single cell. (examples: च्छे षि ‍র্য バ)
//
// Therefore, in addition to the rules in this proposal, having 2 or more
// non-modifier codepoints with width in a cluster forces it to wide.
//
// This more accurately represents what font shaping would produce in
// non-terminal applications, and not a single terminal in my tests implements
// the aformentioned spec correctly (in my understanding) anyways.
use max, cp <- fold_codepoints(string, 0)
case cp {
// unicode core: VS16 forces a cluster to be wide
0xfe0f -> 2
_ -> {
let width = wcwidth(options, cp)
case width > 0 && max > 0 {
// extension: 2 or more codepoints with width force a cluster to be wide
True ->
case is_spacing_mark(cp) {
True -> int.max(max, width)
False -> 2
}
False -> int.max(max, width)
}
}
}
}
fn wcwidth(options: Options, cp: Int) -> Int {
// see: https://git.musl-libc.org/cgit/musl/tree/src/ctype/wcwidth.c
case cp {
// cheap optimization for ascii
_ if cp <= 0x1f -> 0
_ if cp <= 0x7e -> 1
_ ->
case is_ignorable(cp) {
True -> 0
False ->
case
is_wide(cp) || { options.ambiguous_as_wide && is_ambiguous(cp) }
{
True -> 2
False -> 1
}
}
}
}
// -- TABLE ACCESSORS ----------------------------------------------------------
// the data for these functions is generated by the generate.gleam script.
// the general idea is heavily inspired by the tables used in musl libc.
fn is_ignorable(cp: Int) -> Bool {
case cp <= 0x1ffff {
True -> table_lookup(tables.ignorable, cp)
False -> cp >= 0xe0000 && cp <= 0xe0fff
}
}
fn is_ambiguous(cp: Int) -> Bool {
case cp <= 0xffff {
True -> table_lookup(tables.ambiguous, cp)
False -> {
{ cp >= 0x1f100 && cp <= 0x1f10a }
|| { cp >= 0x1f110 && cp <= 0x1f12d }
|| { cp >= 0x1f130 && cp <= 0x1f169 }
|| { cp >= 0x1f170 && cp <= 0x1f18d }
|| { cp >= 0x1f18f && cp <= 0x1f190 }
|| { cp >= 0x1f19b && cp <= 0x1f1ac }
|| { cp >= 0xe0100 && cp <= 0xe01ef }
|| { cp >= 0xf0000 && cp <= 0xffffd }
|| { cp >= 0x100000 && cp <= 0x10FFFD }
}
}
}
fn is_wide(cp: Int) -> Bool {
case cp <= 0x1ffff {
True -> table_lookup(tables.wide, cp)
False -> cp >= 0x20000 && cp <= 0x40000
}
}
fn is_spacing_mark(cp: Int) -> Bool {
case cp <= 0x1ffff {
True -> table_lookup(tables.spacing_mark, cp)
False -> False
}
}
// -- FFI ----------------------------------------------------------------------
// size is not supported
@external(javascript, "./string_width_ffi.mjs", "table_lookup")
fn table_lookup(table: BitArray, value: Int) -> Bool {
let hi = int.bitwise_shift_right(value, 8)
let md = int.bitwise_shift_right(int.bitwise_and(value, 0xff), 3)
let lo = int.bitwise_and(value, 7)
let lvl1_skip = hi * 8
let assert <<_:size(lvl1_skip), lvl1:int, _:bits>> = table
let lvl2_skip = { lvl1 * 32 + md } * 8
let assert <<_:size(lvl2_skip), lvl3:int, _:bits>> = table
int.bitwise_shift_right(lvl3, lo) % 2 != 0
}
@external(javascript, "./string_width_ffi.mjs", "fold_graphemes")
fn fold_graphemes(
str: String,
state: state,
fun: fn(state, String) -> state,
) -> state {
case string.pop_grapheme(str) {
Ok(#(grapheme, str)) -> fold_graphemes(str, fun(state, grapheme), fun)
Error(Nil) -> state
}
}
@external(erlang, "string_width_ffi", "fold_codepoints")
@external(javascript, "./string_width_ffi.mjs", "fold_codepoints")
fn fold_codepoints(
str: String,
state: state,
fun: fn(state, Int) -> state,
) -> state {
use state, cp <- list.fold(string.to_utf_codepoints(str), state)
fun(state, string.utf_codepoint_to_int(cp))
}
@external(erlang, "erlang", "split_binary")
@external(javascript, "./string_width_ffi.mjs", "unsafe_split")
fn unsafe_split(str: String, at: Int) -> #(String, String)
@external(erlang, "string_width_ffi", "utf_codepoint_to_string")
@external(javascript, "./string_width_ffi.mjs", "utf_codepoint_to_string")
fn utf_codepoint_to_string(cp: Int) -> String