//// #### Measure //// [line](#line), [line_with](#line_with), //// [dimensions](#dimensions), [dimensions_with](#dimensions_with), //// [get_terminal_size](#get_terminal_size) //// //// #### Layout //// [limit](#limit), [limit_with](#limit_with), //// [position](#position), [position_with](#position_with), //// [align](#align), [align_with](#align_with), //// [tabs_to_spaces](#tabs_to_spaces), [tabs_to_spaces_with](#tabs_to_spaces_with) //// //// #### Options Builder //// [new](#new), //// [ambiguous_as_wide](#ambiguous_as_wide), [count_ansi_codes](#count_ansi_codes), //// [mode_2027](#mode_2027), [mode_2027_ext](#mode_2027_ext), [mode_wcwidth](#mode_wcwidth), //// [at_tab_offset](#at_tab_offset), [with_tab_width](#with_tab_width) //// //// #### Advanced //// [fold](#fold), [fold_with](#fold_with), [fold_raw](#fold_raw), [fold_raw_pieces](#fold_raw_pieces), //// [is_ansi_component](#is_ansi_component) //// 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 // https://docs.rs/unicode-width/latest/unicode_width/#rules-for-determining-width // https://github.com/muesli/reflow/ // https://github.com/charmbracelet/x/ // TODO: in limit, tabs are counted as their original width, not the width where they will be placed // v4: // - fix labeled arguments in position_with const max_safe_integer = 0x1fffffffffffff // -- 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 the text you print doesn't start at the first column, but is instead /// indented somehow, 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 size of the terminal, if available. /// /// Checks the built-in methods for the target, as well as the `LINES` and /// `COLUMNS` environment variables. pub fn get_terminal_size() -> Result(Size, Nil) { case do_get_terminal_size() { Ok(#(rows, columns)) -> Ok(Size(rows:, columns:)) _ -> Error(Nil) } } @external(erlang, "string_width_ffi", "get_terminal_size") @external(javascript, "./string_width_ffi.mjs", "get_terminal_size") fn do_get_terminal_size() -> Result(#(Int, Int), Nil) /// 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 #(_, columns, _) = do_measure(str, default_options) columns } /// Like `line`, but use custom options. /// /// ### Example /// /// ```gleam /// let options = /// new() /// |> mode_2027 /// /// line_with("👩‍👩‍👦‍👦", options) /// // --> 2 /// ``` pub fn line_with(str: String, options: Options) -> Int { let #(_, columns, _) = do_measure(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("안녕하세요") /// // --> Size(rows: 1, columns: 10) /// /// dimensions("hello,\n안녕하세요") /// // --> Size(rows: 2, columns: 10) /// ``` pub fn dimensions(str: String) -> Size { let #(rows, columns, _) = do_measure(str, default_options) Size(rows:, columns:) } /// Like `dimensions`, but use custom options. pub fn dimensions_with(str: String, options: Options) -> Size { let #(rows, columns, _) = do_measure(str, options) Size(rows:, columns:) } fn do_measure(str: String, options: Options) -> #(Int, Int, Int) { let #(str, ranges, range_width) = prepare_measure(options, str) let fun = fn(state, chr, width) { let #(rows, cols_max, cols_min, cols_curr) = state case chr { "\n" -> #( rows + 1, int.max(cols_max, cols_curr), int.min(cols_max, cols_curr), options.tab_offset, ) "\t" -> #(rows, cols_max, cols_min, cols_curr + tab(options, cols_curr)) _ -> #(rows, cols_max, cols_min, 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 initial = #(0, 0, max_safe_integer, options.tab_offset) let #(rows, cols_max, cols_min, cols_curr) = fold_parts(str, 0, ranges, initial, on_chars, on_range) let cols_max = int.max(cols_max, cols_curr) - options.tab_offset let rows = case cols_curr > options.tab_offset { True -> rows + 1 False -> rows } let cols_min = case rows > 0 { True -> int.min(cols_min, cols_curr) - options.tab_offset False -> 0 } #(rows, cols_max, cols_min) } /// Get the width of a tab at the given column. fn tab(options: Options, col: Int) -> Int { { col / options.tab_width + 1 } * options.tab_width - col } // 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: 10), 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 } // find the grapheme cluster boundary to set the new overflow. fn limit_state_overflow( state: LimitState, piece: String, options: Options, ) -> #(String, String, Int) { let str = state.non_spaces <> state.overflow <> piece let #(before, after, width) = { use #(before, after, width), piece <- fold_with(str, options, #("", "", 0)) case piece.column + piece.width > state.non_spaces_width { True -> #(before, after <> piece.piece, width + piece.width) False -> #(before <> piece.piece, after, width) } } case before == "" { True -> #(before, after, width) False -> #(state.spaces <> before, after, 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 commit = fn(max_col: Int, 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 is_last_line = state.row + 1 >= max_size.rows 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 LimitState( ..initial, str:, row: state.row, col: new_col, spaces: piece, spaces_width: width, ) } False -> { // it doesn't; if we are at the last line, truncate. else, wrap. case is_last_line { True -> { let #(truncated, _, _) = limit_state_overflow(state, "", options) LimitState( ..initial, str: state.str <> truncated <> ellipsis, row: state.row + 1, col: max_size.columns, ) } False -> LimitState( ..initial, 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, ) } } } } } let state = { use state, piece, width <- fold_raw(str, options, initial) // io.debug(#(state, piece, width)) let is_last_line = state.row + 1 >= max_size.rows let max_col = case is_last_line { True -> max_size.columns - ellipsis_width False -> max_size.columns } case state.row >= max_size.rows { // reached the end, but we still want to collect ansi sequences True -> case is_ansi_component(piece, options) { True -> LimitState(..state, str: state.str <> piece) False -> state } False -> case piece { // Line break that we recognise as such "\n" -> { let state = commit(max_col, state, "", 0) LimitState( ..state, str: state.str <> "\n", row: state.row + 1, col: 0, ) } "\t" -> { // tab has a different width depending on whether or not we will wrap let curr_col = limit_state_col(state) let width = case curr_col <= max_col { True -> tab(options, curr_col) False -> tab(options, state.non_spaces_width + state.overflow_width) } commit(max_col, state, piece, width) } // White_Space=Y " " | "\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}" -> commit(max_col, state, piece, width) // not a space or a line break _ -> { case limit_state_col(state) + width <= max_col { True -> // everything's fine and normal :) LimitState( ..state, non_spaces: state.non_spaces <> piece, non_spaces_width: state.non_spaces_width + width, ) False -> { // 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 + width > max_size.columns { True -> { let #(truncated, overflow, overflow_width) = limit_state_overflow(state, piece, options) // it _is_! do we have room for another row? case is_last_line { False -> LimitState( ..initial, str: state.str <> truncated <> "\n", row: state.row + 1, non_spaces: overflow, non_spaces_width: overflow_width, ) True -> // no, word to long this is the last row, sorry. LimitState( ..initial, str: state.str <> truncated <> ellipsis, row: state.row + 1, col: max_size.columns, ) } } False -> LimitState( ..state, overflow: state.overflow <> piece, overflow_width: state.overflow_width + width, ) } } } } } } } // commit the last word, if any let state = commit(max_size.columns, state, "", 0) state.str } /// Replace all tab characters found in the string with the amount of spaces /// that this tab would have had otherwise. /// /// This makes it safe 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_raw_pieces(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. /// /// **Tip:** If you only want to align a string on one axis, you can provide /// `0` for the orthogonal one! Extra characters and lines will 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 #(str_rows, str_cols_max, str_cols_min) = do_measure(str, options) let space_width = line_with(space, options) // horizontal align - we can skip this if every line is at least columns wide let str = case bounding_box.columns > str_cols_min { True -> { 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 - str_cols_max, 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 - str_cols_max, 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 } } } do_align(str, options, align) } False -> str } // vertical align - we can also skip this if the string has more lines already let missing_rows = bounding_box.rows - str_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. /// /// ### 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) /// /// align("Trans\nrights\nare\nhuman\nrights", to: 7, align: Right, with: " ") /// // --> " Trans\n rights\n are\n human\n rights" /// ``` 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_raw_pieces(str, options, state) let #(acc, line, line_width) = state case piece.piece { "\n" -> 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. /// /// Intended to be used as a basis for custom layout algorithms. 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. /// /// Intended to be used as a basis for custom layout algorithms. pub fn fold_with( over str: String, using options: Options, from state: state, with fun: fn(state, Piece) -> state, ) -> state { let #(state, fun) = piece_reducer(state, options, fun) 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 #(state, _, _) = fold_parts(str, 0, ansi_ranges, state, on_chars, on_range) state } /// A low-level fold that 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 function does not by itself keep track of any additional state, and /// doesn't for example handle newlines or tabs like `fold` would. /// If you know that your algorithm is safe and won't break up graphemes (or /// maybe this is acceptable), you can use this fold variant instead as a /// performance optimisation. /// /// 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. /// let options = new() /// use #(total, acc), chr, width <- fold_raw(input, options, from: #(0, "")) /// case total >= 50 { /// True -> case is_ansi_component(chr, options) { /// True -> #(total, acc <> chr) /// False -> #(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) } /// A version of `fold_raw` that does keep track of some state for you to /// handle tabs, and count the current row/column, passing `Piece` values to /// you instead. pub fn fold_raw_pieces( over string: String, using options: Options, from state: state, with fun: fn(state, Piece) -> state, ) -> state { let ansi_ranges = case options.count_ansi_codes { True -> [] False -> ansi.match(string) } let #(state, fun) = piece_reducer(state, options, fun) 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) } let #(state, _, _) = fold_parts(string, 0, ansi_ranges, state, on_chars, on_range) state } fn piece_reducer( state: state, options: Options, fun: fn(state, Piece) -> state, ) -> #( #(state, Int, Int), fn(#(state, Int, Int), String, Int) -> #(state, Int, Int), ) { let initial = #(state, 0, options.tab_offset) let fun = fn(state, piece, width) { let #(state, row, column) = state let width = case piece { "\t" -> tab(options, column) _ -> width } let state = fun(state, Piece(piece:, row:, column:, width:)) case piece { "\n" -> #(state, row + 1, options.tab_offset) _ -> #(state, row, column + width) } } #(initial, fun) } 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 if max > 0 -> 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 max == 0 { True -> wcwidth(options, cp) False -> case cp { // unicode core: VS16 forces a cluster to be wide 0xfe0f -> 2 _ -> { case wcwidth(options, cp) { 0 -> max // extension: 2 or more codepoints with width force a cluster to be wide width -> case is_spacing_mark(cp) { True -> int.max(max, width) False -> 2 } } } } } } 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