culr/lib/processing.ml
2024-12-03 01:32:51 +11:00

52 lines
1.4 KiB
OCaml

open Types
let ansi_filter s =
s |> function
| Ansi a ->
Types.Ansi
(List.filter
(function Types.Fg _ | Types.Bg _ -> false | _ -> true)
a)
| n -> n
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 }
module Culriser = struct
type filter = (colour -> bool) list
type sort = int array
let run_filters (f : filter) el =
List.fold_left (fun res filt -> if not (filt el) then false else res) true f
let create s f c =
let ( @ ) = Array.append in
let colours =
c |> permute s |> fun c ->
if List.length f > 0 then
Array.fold_left
(fun acc el -> if run_filters f el then acc @ [| el |] else acc)
[||] c
else c
in
{ colours; sz = Array.length colours; current = 0 }
let next t =
let ret = t.current and nv = t.current + 1 in
if nv >= t.sz then t.current <- 0 else t.current <- nv;
t.colours.(ret)
let reset t = t.current <- 0
let serialise_with_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) ])
| _ -> ());
Emitter.serialise serialiser chunk
end