serialise colour directly, use arrays throughout

This commit is contained in:
atagen 2024-12-01 23:58:34 +11:00
parent 9ae2e4e91e
commit 54397204cf

View File

@ -12,12 +12,7 @@ let ansi_filter s =
let permute order a = Array.mapi (fun i _ -> a.(order.(i))) a
(* TODO: implement this so cli can use a dsl to specify colourising patterns *)
type culr = {
colours : colour array;
sz : int;
mutable current : int;
mutable first : bool;
}
type culr = { colours : colour array; sz : int; mutable current : int }
module Culriser = struct
type filter = (colour -> bool) list
@ -28,32 +23,28 @@ module Culriser = struct
let create ?(s = [| 0; 1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15 |])
?(f = []) c =
let ( @ ) = Array.append in
let colours =
c
|> List.fold_left
(fun acc el -> if run_filters f el then acc @ [ el ] else acc)
[]
|> Array.of_list |> permute s
c |> permute s
|> Array.fold_left
(fun acc el -> if run_filters f el then acc @ [| el |] else acc)
[||]
in
{ colours; sz = Array.length colours; current = 0; first = true }
{ colours; sz = Array.length colours; current = 0 }
let next t =
let nv = t.current + 1 in
let ret = t.current and nv = t.current + 1 in
if nv >= t.sz then t.current <- 0 else t.current <- nv;
t.colours.(t.current)
t.colours.(ret)
let reset t = t.current <- 0
let add_colour t chunk =
if t.first then (
t.first <- false;
Ansi [ Fg (next t) ] :: [ chunk ])
else
match chunk with
| Separator _ -> chunk :: [ Ansi [ Fg (next t) ] ]
| Delimiter _ ->
reset t;
chunk :: [ Ansi [ Fg (next t) ] ]
| Ansi _ -> [ chunk ]
| Text _ -> [ chunk ]
let add_colour t serialiser chunk =
(match chunk with
| Separator _ -> Emitter.serialise serialiser (Ansi [ Fg (next t) ])
| Delimiter _ ->
reset t;
Emitter.serialise serialiser (Ansi [ Fg (next t) ])
| _ -> ());
chunk
end