50 lines
1.3 KiB
OCaml
50 lines
1.3 KiB
OCaml
open Types
|
|
open Array
|
|
|
|
let ansi_filter s =
|
|
s |> function
|
|
| Ansi a ->
|
|
Types.Ansi
|
|
(List.filter
|
|
(function Fg _ | Bg _ -> false | _ -> true)
|
|
a)
|
|
| n -> n
|
|
|
|
(* 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 ?(filters = []) (c : colour array) =
|
|
let ( @ ) = append in
|
|
let colours =
|
|
if List.length filters > 0 then
|
|
fold_left
|
|
(fun acc el -> if run_filters filters el then acc @ [| el |] else acc)
|
|
[||] c
|
|
else c
|
|
in
|
|
{ colours; sz = 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
|