improve behaviour around indented data

This commit is contained in:
atagen 2024-12-05 22:58:29 +11:00
parent 7146615e04
commit e440fda10c
2 changed files with 19 additions and 11 deletions

View File

@ -4,7 +4,7 @@ let read_all colours =
In_channel.set_binary_mode stdin true;
let open Types in
let t = Emitter.create and culr = Culriser.create colours in
Emitter.serialise t (Ansi [ Fg (Culriser.next culr) ]);
Emitter.serialise t (Ansi [ Fg (Culriser.current culr) ]);
Angstrom_unix.parse_many ~buf_size:4096 Parse.culr_parse
(fun c -> c |> ansi_filter culr |> Culriser.serialise_with_colour culr t)
stdin

View File

@ -2,7 +2,12 @@ open Types
open Array
(* TODO: implement this so cli can use a dsl to specify colourising patterns *)
type culr = { colours : colour array; sz : int; mutable current : int }
type culr = {
colours : colour array;
sz : int;
mutable current : int;
mutable first : bool;
}
module Culriser = struct
type filter = (colour -> bool) list
@ -20,25 +25,28 @@ module Culriser = struct
[||] c
else c
in
{ colours; sz = length colours; current = 0 }
{ colours; sz = length colours; current = 0; first = true }
let current t = t.colours.(t.current)
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)
t.current <- (t.current + 1) mod t.sz;
t.colours.(t.current)
let reset t = t.current <- 0
let reset t =
t.current <- 0;
t.first <- true
let serialise_with_colour t serialiser chunk =
Emitter.serialise serialiser chunk;
(match chunk with
| Separator _ -> Emitter.serialise serialiser (Ansi [ Fg (next t) ])
| Separator _ ->
if not t.first then Emitter.serialise serialiser (Ansi [ Fg (next t) ])
| Delimiter _ ->
reset t;
Emitter.serialise serialiser (Ansi [ Fg (next t) ])
| _ -> ());
Emitter.serialise serialiser chunk
Emitter.serialise serialiser (Ansi [ Fg (current t) ]);
| Text _ -> if t.first then t.first <- false
| _ -> ())
end
let ansi_filter t s =