diff --git a/src/string_width.gleam b/src/string_width.gleam index a776a35..e47d0d4 100644 --- a/src/string_width.gleam +++ b/src/string_width.gleam @@ -11,8 +11,12 @@ //// [padding](#padding)[[_with](#padding_with)], //// [position](#position)[[_with](#position_with)], //// [scroll](#scroll)[[_with](#scroll_with)], -//// [tabs_to_spaces](#tabs_to_spaces)[[_with](#tabs_to_spaces_with)], -//// [inline_styles](#inline_styles) +//// [tabs_to_spaces](#tabs_to_spaces)[[_with](#tabs_to_spaces_with)] +//// +//// #### ANSI escape sequence helpers +//// [inline_styles](#inline_styles), +//// [is_ansi_component](#is_ansi_component), +//// [strip_ansi](#strip_ansi) //// //// #### Options Builder //// [new](#new), @@ -23,7 +27,7 @@ //// #### Advanced //// [fold](#fold)[[_with](#fold_with)], //// [fold_raw](#fold_raw), [fold_raw_pieces](#fold_raw_pieces), -//// [is_ansi_component](#is_ansi_component) +//// //// import gleam/int @@ -48,14 +52,16 @@ import string_width/internal/tables // v3.2.0: // x collect/reset SGR codes like reflow does -// - make align trim lines? // x padding layout, switch position to use padding internally // x remove do_measure -// - make sure position -> padding only measures the string once +// x make sure position -> padding only measures the string once // - rewrite align (think about trimming) +// - make align trim lines? // - join/columns layout (grid? table?) +// - join_vertical might also be intereseting for alignment/spacing +// x ansi module // x drop_left/drop_right - padding with negative margins? -// - cut/viewort/scroll(!!) function as an alternative to position with overflow? +// x cut/viewort/scroll(!!) function as an alternative to position with overflow? // provide the area you want to view and we will compute the padding for that. // x change the module header to not list _with functions separately // x add "Back to top" links to all functions @@ -70,6 +76,7 @@ import string_width/internal/tables // - think _again_ about changing fold to be called with an EOS marker at the end // - remove max_width from align // - position with overflow-hidden +// - limit -> reflow/fit that doesn't respect newlines in the original string // - split stuff into 2/3 packages? // -- OPTIONS ------------------------------------------------------------------ @@ -1263,6 +1270,8 @@ fn spacer_width(options: Options, str: String) -> Int { } } +// -- ANSI HELPERS ------------------------------------------------------------- + /// Make sure [SGR ansi codes](https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters) /// (these are the ones you'd use for colors and styling!) never wrap /// around a line. This makes it save to use other layout functions on the @@ -1332,7 +1341,32 @@ pub fn inline_styles(str: String) -> String { } } -// -- FOLD --------------------------------------------------------------------- +/// Strip _all_ ansi sequences from a string, using the same matching algorithm +/// that this library uses internally. +/// +/// This makes it safe to enable `count_ansi_codes`, or print a string without +/// having to worry that it might mess with the terminal and/or colors. +/// +/// Unexpected characters inside of escape sequences are ignored (swallowed). +/// +/// ### Examples +/// +/// ```gleam +/// strip_ansi("\u{1b}[0;33;49;3;9;4mhi~\u{1b}[0m") +/// // --> "hi~" +/// +/// strip_ansi("check out \u{1b}]8;;https://gleam.run\u{7}Gleam!\u{1b}]8;;\u{7}") +/// // --> "check out Gleam!" +/// ``` +/// +///