(* open Ansi *) open Types open Angstrom let is_sep = function '\x20' | '\x09' -> true | _ -> false let is_delim = function '\x0a' | '\x0d' -> true | _ -> false let is_text c = not (is_sep c || is_delim c || c = '\x1b') let sep = take_while is_sep >>| fun s -> Separator s let delim = take_while is_delim >>| fun s -> Delimiter s let text = take_while is_text >>| fun s -> Text s let ansi = Ansi.ansi_parse >>| Ansi.parse_ansi_intermediate let culr_parse = peek_char_fail >>= function | n when is_sep n -> sep | n when is_delim n -> delim | n when n = '\x1b' -> ansi | _n -> text let debug_print = let print_colour = List.fold_left (fun _acc el -> match el with | Fg c -> ( match c with | Simple n | Intrinsic n -> print_endline ("fg simple/intrinsic " ^ string_of_int n) | RGB (r, g, b) -> print_endline ("fg rgb: " ^ List.fold_left (fun acc el -> acc ^ ", " ^ string_of_int el) "" [ r; g; b ])) | Bg c -> ( match c with | Simple n | Intrinsic n -> print_endline ("bg simple/intrinsic " ^ string_of_int n) | RGB (r, g, b) -> print_endline ("bg rgb: " ^ List.fold_left (fun acc el -> acc ^ ", " ^ string_of_int el) "" [ r; g; b ])) | Reset -> print_endline "ansi reset" | Other n -> print_endline ("other ansi: " ^ string_of_int n)) () in function | Text s | Separator s | Delimiter s -> print_endline ("parsed '" ^ s ^ "'") | Ansi a -> print_colour a let%test "sep_parse" = Angstrom.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 | Ok (Delimiter "\n\n\n") -> true | _ -> false let%test "text_parse" = Angstrom.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) "sometext \n\n\n \x1b[38;2;2m" |> function | Ok [ Text "sometext"; Separator " "; Delimiter "\n\n\n"; Separator " "; Ansi [ Fg (Simple 2) ]; ] -> true | Ok n -> n |> List.fold_left (fun _acc el -> el |> debug_print) (); false | _ -> false