Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better way of rendered length of unicode string #1

Open
jobjo opened this issue Apr 1, 2021 · 0 comments
Open

Better way of rendered length of unicode string #1

jobjo opened this issue Apr 1, 2021 · 0 comments
Labels
good first issue Good for newcomers

Comments

@jobjo
Copy link
Owner

jobjo commented Apr 1, 2021

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
@jobjo jobjo added the good first issue Good for newcomers label Apr 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant