support colour reinsert after resets, switch to intrinsics by default

This commit is contained in:
atagen 2024-12-05 22:19:43 +11:00
parent a4bf011cb0
commit 7146615e04
6 changed files with 54 additions and 33 deletions

View file

@ -1,15 +1,6 @@
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 }
@ -31,6 +22,8 @@ module Culriser = struct
in
{ colours; sz = length colours; current = 0 }
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;
@ -47,3 +40,26 @@ module Culriser = struct
| _ -> ());
Emitter.serialise serialiser chunk
end
let ansi_filter t s =
s |> function
| Ansi a -> (
let open List in
let ol = length a
and result =
a
|> filter (function Fg _ | Bg _ -> false | _ -> true)
|> fold_left
(fun acc el ->
if el = Reset then acc @ [ el; Fg (Culriser.current t) ]
else acc @ [ el ])
[]
in
let rl = length result in
match (rl, ol) with
(* empty ansi code is equivalent to a reset *)
| 0, 0 -> Ansi [ Reset; Fg (Culriser.current t) ]
(* if we filtered everything out of it produce no tokens *)
| 0, _ -> Empty
| _, _ -> Ansi result)
| n -> n