implement CULRS and CULR_ORDER env vars
This commit is contained in:
parent
8db7ba4de3
commit
3a2b319b2f
3 changed files with 55 additions and 22 deletions
39
lib/parse.ml
39
lib/parse.ml
|
@ -1,6 +1,6 @@
|
|||
(* open Ansi *)
|
||||
open Types
|
||||
open Angstrom
|
||||
open Parsers
|
||||
|
||||
let is_sep = function '\x20' | '\x09' -> true | _ -> false
|
||||
let is_delim = function '\x0a' | '\x0d' -> true | _ -> false
|
||||
|
@ -17,6 +17,25 @@ let culr_parse =
|
|||
| n when n = '\x1b' -> ansi
|
||||
| _n -> text
|
||||
|
||||
let conv_rgb = function [ r; g; b ] -> Types.RGB (r, g, b) | _ -> assert false
|
||||
let hex_digit = take 2 >>| fun s -> int_of_string ("0x" ^ s)
|
||||
|
||||
let rgb_triple =
|
||||
let digit_comma = digit <* char ',' in
|
||||
char '(' *> list [ digit_comma; digit_comma; digit ] <* char ')' >>| conv_rgb
|
||||
|
||||
let hex_colour = char '#' *> count 3 hex_digit >>| conv_rgb
|
||||
let simple_colour = digit >>| fun s -> Types.Simple s
|
||||
let env_colours = sep_by1 skip_semi (rgb_triple <|> hex_colour <|> simple_colour)
|
||||
let unpack = function Ok payload -> payload | _ -> []
|
||||
|
||||
let parse_env_colours s =
|
||||
parse_string ~consume:Consume.All env_colours s |> unpack |> Array.of_list
|
||||
|
||||
let parse_env_order s =
|
||||
parse_string ~consume:Consume.All Parsers.semi_digits s
|
||||
|> unpack |> Array.of_list
|
||||
|
||||
let debug_print =
|
||||
let print_colour =
|
||||
List.fold_left
|
||||
|
@ -51,22 +70,22 @@ let debug_print =
|
|||
| Ansi a -> print_colour a
|
||||
|
||||
let%test "sep_parse" =
|
||||
Angstrom.parse_string ~consume:Consume.All sep " " |> function
|
||||
parse_string ~consume:Consume.All sep " " |> function
|
||||
| Ok (Separator " ") -> true
|
||||
| _ -> false
|
||||
|
||||
let%test "delim_parse" =
|
||||
Angstrom.parse_string ~consume:Consume.All delim "\n\n\n" |> function
|
||||
parse_string ~consume:Consume.All delim "\n\n\n" |> function
|
||||
| Ok (Delimiter "\n\n\n") -> true
|
||||
| _ -> false
|
||||
|
||||
let%test "text_parse" =
|
||||
Angstrom.parse_string ~consume:Consume.All text "okokyeahok" |> function
|
||||
parse_string ~consume:Consume.All text "okokyeahok" |> function
|
||||
| Ok (Text "okokyeahok") -> true
|
||||
| _ -> false
|
||||
|
||||
let%test "culr_parse" =
|
||||
Angstrom.parse_string ~consume:Consume.Prefix (many1 culr_parse)
|
||||
parse_string ~consume:Consume.All (many1 culr_parse)
|
||||
"sometext \n\n\n \x1b[38;2;2m"
|
||||
|> function
|
||||
| Ok
|
||||
|
@ -82,3 +101,13 @@ let%test "culr_parse" =
|
|||
n |> List.fold_left (fun _acc el -> el |> debug_print) ();
|
||||
false
|
||||
| _ -> false
|
||||
|
||||
let%test "rgb" =
|
||||
parse_string ~consume:Consume.All rgb_triple "(255,0,127)" |> function
|
||||
| Ok s -> s = Types.RGB (255, 0, 127)
|
||||
| _ -> false
|
||||
|
||||
let%test "hex" =
|
||||
parse_string ~consume:Consume.All hex_colour "#FF007F" |> function
|
||||
| Ok s -> s = Types.RGB (255, 0, 127)
|
||||
| _ -> false
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue