From 54397204cfd66660cdbf16d566d5bb2323a33581 Mon Sep 17 00:00:00 2001 From: atagen Date: Sun, 1 Dec 2024 23:58:34 +1100 Subject: [PATCH] serialise colour directly, use arrays throughout --- lib/processing.ml | 43 +++++++++++++++++-------------------------- 1 file changed, 17 insertions(+), 26 deletions(-) diff --git a/lib/processing.ml b/lib/processing.ml index a7622b2..a42fb1b 100644 --- a/lib/processing.ml +++ b/lib/processing.ml @@ -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