culr/lib/processing.ml

51 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 el =
List.fold_left (fun res filt -> if not (filt el) then false else res) true f
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 |> 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 }
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 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