From e440fda10c7265512f7f249c8f9628e8ed56ec93 Mon Sep 17 00:00:00 2001 From: atagen Date: Thu, 5 Dec 2024 22:58:29 +1100 Subject: [PATCH] improve behaviour around indented data --- lib/pipes.ml | 2 +- lib/processing.ml | 28 ++++++++++++++++++---------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/lib/pipes.ml b/lib/pipes.ml index 710de94..3865070 100644 --- a/lib/pipes.ml +++ b/lib/pipes.ml @@ -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 diff --git a/lib/processing.ml b/lib/processing.ml index d9cf55f..e7ab80a 100644 --- a/lib/processing.ml +++ b/lib/processing.ml @@ -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 =