Ansi_textLibrary 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.
module Color : sig ... endmodule Attr : sig ... endmodule Style : sig ... endmodule Control : sig ... endmodule Text : sig ... endmodule Style_ranges : sig ... endmodule Text_with_style_ranges : sig ... endval quickcheck_generator_element :
element Ppx_quickcheck_runtime.Base_quickcheck.Generator.tval quickcheck_observer_element :
element Ppx_quickcheck_runtime.Base_quickcheck.Observer.tval quickcheck_shrinker_element :
element Ppx_quickcheck_runtime.Base_quickcheck.Shrinker.tval sexp_of_element : element -> Sexplib0.Sexp.tval element_of_sexp : Sexplib0.Sexp.t -> elementval __element_of_sexp__ : Sexplib0.Sexp.t -> elementtype t = element listThe primary representation of text with ANSI codes. We should be able to parse any string into this type.
include Ppx_compare_lib.Comparable.S with type t := tinclude Ppx_compare_lib.Comparable.S__local with type t := tinclude Ppx_compare_lib.Equal.S with type t := tinclude Ppx_compare_lib.Equal.S__local with type t := tinclude Ppx_quickcheck_runtime.Quickcheckable.S with type t := tval quickcheck_generator : t Base_quickcheck.Generator.tval quickcheck_observer : t Base_quickcheck.Observer.tval quickcheck_shrinker : t Base_quickcheck.Shrinker.tinclude Sexplib0.Sexpable.S with type t := tinclude Sexplib0.Sexpable.Of_sexp with type t := tval t_of_sexp : Sexplib0.Sexp.t -> tinclude Sexplib0.Sexpable.Sexp_of with type t := tval sexp_of_t : t -> Sexplib0.Sexp.tval width : t -> intThe total length of all Text elements in an Ansi_text.t.
val is_empty : t -> boolWhether the Ansi_text.t has width 0.
val to_string : t -> stringA string that includes all text and all ANSI codes.
val to_string_hum : t -> stringA string where ANSI codes have been replaced by more-human-readable names.
val to_unstyled : t -> stringA string where all ANSI codes have been removed.
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.
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.
val parse : string -> [ Ansi_text__.Ansi.t | `Text of Text.t ] listParse 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_text__.Ansi.t | `Text of Text.t ] list ->
[ Ansi_text__.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_text__.Ansi.t | `Text of Text.t ] list ->
[ Ansi_text__.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_text__.Ansi.t | `Text of Text.t ] list ->
[ Ansi_text__.Ansi.t | `Text of Text.t ] listTruncate the text at the given length.
val wrap :
width:int ->
[ Ansi_text__.Ansi.t | `Text of Text.t ] list ->
[ Ansi_text__.Ansi.t | `Text of Text.t ] list listWrap the text at the given length.
val apply : Style.t -> string -> stringA 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.
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.
module Unknown_esc : sig ... endEscape sequences we recognize structurally but don't interpret as Ansi_text.