From 5c3864986f844a31e0b6dbeec48543dc6376cff6 Mon Sep 17 00:00:00 2001 From: Federico Igne Date: Tue, 23 Jan 2024 18:11:22 +0100 Subject: feat: add partial chords to statusbar --- lib/command.ml | 47 +++++++++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 24 deletions(-) (limited to 'lib/command.ml') diff --git a/lib/command.ml b/lib/command.ml index 31e2ca2..74b616a 100644 --- a/lib/command.ml +++ b/lib/command.ml @@ -18,6 +18,7 @@ type operation = type scope = Line | To_bol | To_eol | Down | Left | Right | Up type command = + | Reset | Type of char | Simple of Key.t | Partial of Key.t @@ -89,59 +90,57 @@ let is_instant_operation k = List.mem ~equal:Poly.equal instant_operation k let n_stream = let step s k = - let open Sequence.Step in match (s, k) with - | `start, Key '"' -> Yield { value = Partial k; state = `chord_reg_pre } + | `start, Key '"' -> (`chord_reg_pre, Partial k) (* Register *) - | `chord_reg_pre, Key c -> Yield { value = Partial k; state = `chord_reg c } + | `chord_reg_pre, Key c -> (`chord_reg c, Partial k) (* Count (first) *) | `start, Key n when Char.('1' <= n && n <= '9') -> let n = Char.to_int n - 48 in - Yield { value = Partial k; state = `chord_fst_n (None, n) } + (`chord_fst_n (None, n), Partial k) | `chord_reg r, Key n when Char.('1' <= n && n <= '9') -> let n = Char.to_int n - 48 in - Yield { value = Partial k; state = `chord_fst_n (Some r, n) } + (`chord_fst_n (Some r, n), Partial k) | `chord_fst_n (r, m), Key n when Char.('0' <= n && n <= '9') -> let n = (10 * m) + Char.to_int n - 48 in - Yield { value = Partial k; state = `chord_fst_n (r, n) } + (`chord_fst_n (r, n), Partial k) (* Instant operations *) - | `start, k when is_instant_operation k -> - Yield { value = shortcut (to_op k); state = `start } + | `start, k when is_instant_operation k -> (`start, shortcut (to_op k)) | `chord_reg r, k when is_instant_operation k -> - Yield { value = shortcut ~r (to_op k); state = `start } + (`start, shortcut ~r (to_op k)) | `chord_fst_n (r, n), k when is_instant_operation k -> - Yield { value = shortcut ?r ~n (to_op k); state = `start } + (`start, shortcut ?r ~n (to_op k)) (* Chord operation (first) *) | `start, k when is_chord_operation k -> - Yield { value = Partial k; state = `chord_cmd (None, None, to_op k) } + (`chord_cmd (None, None, to_op k), Partial k) | `chord_reg r, k when is_chord_operation k -> - Yield { value = Partial k; state = `chord_cmd (Some r, None, to_op k) } + (`chord_cmd (Some r, None, to_op k), Partial k) | `chord_fst_n (r, n), k when is_chord_operation k -> - Yield { value = Partial k; state = `chord_cmd (r, Some n, to_op k) } + (`chord_cmd (r, Some n, to_op k), Partial k) (* Count (second) *) | `chord_cmd (r, n1, c), Key n when Char.('1' <= n && n <= '9') -> let n = Char.to_int n - 48 in - Yield { value = Partial k; state = `chord_snd_n (r, n1, c, n) } + (`chord_snd_n (r, n1, c, n), Partial k) | `chord_snd_n (r, n1, c, n2), Key n when Char.('0' <= n && n <= '9') -> let n2 = (10 * n2) + Char.to_int n - 48 in - Yield { value = Partial k; state = `chord_snd_n (r, n1, c, n2) } + (`chord_snd_n (r, n1, c, n2), Partial k) (* Chord operation (second) *) | `chord_cmd (r, n, c), k when is_chord_operation k && Poly.(c = to_op k) -> - Yield { value = chord ?r ?n1:n c Line; state = `start } + (`start, chord ?r ?n1:n c Line) | `chord_snd_n (r, n1, c, n2), k when is_chord_operation k && Poly.(c = to_op k) -> - Yield { value = chord ?r ?n1 c ~n2 Line; state = `start } + (`start, chord ?r ?n1 c ~n2 Line) (* Movement *) | (`start | `chord_reg _), k when is_simple_movement k -> - Yield { value = chord Noop (to_scope k); state = `start } + (`start, chord Noop (to_scope k)) | `chord_fst_n (_, n), k when is_simple_movement k -> - Yield { value = chord ~n1:n Noop (to_scope k); state = `start } + (`start, chord ~n1:n Noop (to_scope k)) | `chord_cmd (r, n, c), k when is_simple_movement k -> - Yield { value = chord ?r ?n1:n c (to_scope k); state = `start } + (`start, chord ?r ?n1:n c (to_scope k)) | `chord_snd_n (r, n1, c, n2), k when is_simple_movement k -> - Yield { value = chord ?r ?n1 c ~n2 (to_scope k); state = `start } + (`start, chord ?r ?n1 c ~n2 (to_scope k)) (* Catch-all rules *) - | `start, _ -> Yield { value = Simple k; state = `start } - | _, _ -> Skip { state = `start } + | `start, _ -> (`start, Simple k) + | _, _ -> (`start, Reset) in - Sequence.unfold_with ~init:`start ~f:step Key.stream + Sequence.folding_map ~init:`start ~f:step Key.stream -- cgit v1.2.3