Skip to content

Better way of rendered length of unicode string #1

@jobjo

Description

@jobjo

Needed in order to determine the rendered length of a string (that is the string without markup sections for color/style). Current version:

let style_regexp = Str.regexp "\\[[0-9]+m"

let rendered_length : type a. (Format.formatter -> a -> unit) -> a -> int =
 fun f a ->
    Format.kasprintf Fun.id "%a" f a
    |> Str.global_replace style_regexp ""
    |> String.to_seq
    |> Seq.filter (fun c -> Char.code c <> 27)
    |> Seq.fold_left (fun n _ -> n + 1) 0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions