Ansi_textSourceLibrary for parsing text that contains ANSI escape codes.
Implements types for parsing the whole spec, but with a focus on representing Select Graphic Rendition (SGR) codes through the Style module, Control Sequence Introducer (CSI) codes through the Control module, and terminal hyperlinks (OSC-8) through the Hyperlink module.
More details on SGR: https://en.wikipedia.org/wiki/ANSI_escape_code#Select_Graphic_Rendition_parameters
More details on CSI: https://en.wikipedia.org/wiki/ANSI_escape_code#Control_Sequence_Introducer_commands
The primary type Ansi_text.t is defined in text_with_ansi.ml, and represents text as a list of `Text and Ansi.t elements. The functions defined in parser.ml and input_output.ml handle parsing to/from strings. These serve three primary purposes: 1) stripping out ANSI codes or rendering them in a more human-readable form for use in expect-tests. 2) performing text-wrapping and similar operations that depend on string-length in a way that ignores ANSI codes and respects Unicode character widths. 3) simplifying ANSI codes in text that contains lots of them.
The Text_with_style_ranges module is intended for applying styles to specific ranges within a text or for parsing text with such styles. This is used by the version of patdiff4 in the Fe web-ui to extract the ranges of the styles that patdiff applied.
The primary representation of text with ANSI codes. We should be able to parse any string into this type.
include Ppx_compare_lib.Comparable.S__local with type t := tinclude Ppx_compare_lib.Equal.S__local with type t := tinclude Ppx_quickcheck_runtime.Quickcheckable.S with type t := tinclude Sexplib0.Sexpable.S with type t := tinclude Sexplib0.Sexpable.Of_sexp with type t := tinclude Sexplib0.Sexpable.Sexp_of with type t := tA string where ANSI codes have been replaced by more-human-readable names.
Map over the elements; if f outputs None the element is unchanged. This makes it easy to write an f that applies only to `Style elements, for example.
Best-effort to reduce the ansi-codes needed to express the same styles.
Determines what styles are active at the end of the string. Assumes there are no styles active at the start of the text.
Split at an index in the printable text, terminating and restoring active styles.
Parse a string (possibly) containing ANSI escape sequences into structured form. Malformed sequences are captured as Unknown or Other in the result. The resulting Text_with_ansi.t can always be converted back to a semantically equivalent string via Text_with_ansi.to_string.
val pad :
?char:char ->
?style:Style.t ->
width:int ->
[ Ansi.t | `Text of Text.t ] list ->
[ Ansi.t | `Text of Text.t ] listPad the text to the given length with spaces or another character.
val center :
?char:char ->
?style:Style.t ->
width:int ->
[ Ansi.t | `Text of Text.t ] list ->
[ Ansi.t | `Text of Text.t ] listCenter the text within the given length, using the given character (default=' '). The left side can end up with one fewer padding character than the right.
val truncate :
width:int ->
[ Ansi.t | `Text of Text.t ] list ->
[ Ansi.t | `Text of Text.t ] listTruncate the text at the given length.
val wrap :
width:int ->
[ Ansi.t | `Text of Text.t ] list ->
[ Ansi.t | `Text of Text.t ] list listWrap the text at the given length.
A string where the given styles are turned on at the start and off at the end. Note that this does not parse or minimize the string.
Print a string with ANSI style codes converted into a more human-readable format.
Remove redundant ANSI style codes from a string. Note that this is best-effort, and the resulting string is not guaranteed to be globally minimal.
Remove all ANSI style codes from a string.
Prepares a pair of strings for printing as two columns in a single buffer. Wraps both strings to the given width, then pads the shorter list to the same length with empty strings, then pads each left-side line to the width with spaces.
Escape sequences we recognize structurally but don't interpret as Ansi_text.