gap/styling

Types

The Highlightertakes a string representation of the item that was not matching and should return a string representation that can be used to visually indicate that it is a non-matching item.

The default implementation of the highlighters uses the gleam_community/ansi library to set a different color for the item, but any type if indication can be used as long as it returns a valid string

pub type Highlighter =
  fn(String) -> String

Highlighters to use for indicating matches / non-matches

first is used to highlight non-matches in the first string/list second is used to highlight non-matches in the second string/list matching is used to highlight matches in the both strings/lists

pub type Highlighters {
  Highlighters(
    first: Highlighter,
    second: Highlighter,
    matching: Highlighter,
  )
}

Constructors

  • Highlighters(
      first: Highlighter,
      second: Highlighter,
      matching: Highlighter,
    )

Part is used to indicate to a custom serializer if it should produce a serialization based on a segment with items or the final string that contains already serialized segments

pub type Part(a) {
  Part(acc: String, part: List(a), highlight: Highlighter)
  All(all: String)
}

Constructors

  • Part(acc: String, part: List(a), highlight: Highlighter)

    acc the already serialized part of the result, part is the current segment that should be serialized and appended and highlighter is the Highlighter that can be used to indicate non-matching items

  • All(all: String)

    all is a string representing all serialized segments. This can be useful if some string should be prepended/appended to the final result

A Serializercan be used to create string representation of the comparison results

See serialize for adding custom serializers and mk_generic_serializer

pub type Serializer(a) =
  fn(Part(a)) -> String

Styling of a Comparison

See from_comparison

pub opaque type Styling(a)

Functions

pub fn first_highlight_default(string: String) -> String

Default highlighter for the first string/list in the comparison

pub fn from_comparison(comparison: Comparison(a)) -> Styling(a)

Create a new Styling from a Comparison

The Styling can be customized by adding highlighters and a serializer See highlight and serialize

pub fn highlight(
  styling: Styling(a),
  first: fn(String) -> String,
  second: fn(String) -> String,
  matching: fn(String) -> String,
) -> Styling(a)

Add highlighters to the Styling

The highlighters are used to mark the matching/non-matching items in the first/second list/string

pub fn mk_generic_serializer(
  separator: String,
  around: fn(String) -> String,
) -> fn(Part(a)) -> String

Creates a generic serializer that uses separator between all items and calls around for possibility to prepend/append strings to the final result

pub fn no_highlight(string: String) -> String

Default highlighter used for matching items

pub fn second_highlight_default(string: String) -> String

Default highlighter for the second string/list in the comparison

pub fn serialize(
  styling: Styling(a),
  serializer: fn(Part(a)) -> String,
) -> Styling(a)

Add a serializer to the Styling

The serializer is used to create string representation of the items in the segments of the Comparison See Part for details

NOTE: StringComparison will always use the default string serializer (concatenating the graphemes). If there is a need for custom serialization of StringComparison convert the string to a list of graphemes and treat it as a ListComparison

pub fn to_styled_comparison(
  styling: Styling(a),
) -> StyledComparison

Creates a styled comparison using either custom highlighters/serializer if they where added or default highlighters and/or serializer

Search Document