From 53916fc3f7dcbefd90e3d0340a2a8f32bf331d1d Mon Sep 17 00:00:00 2001 From: Federico Igne Date: Sun, 28 Jan 2024 01:22:52 +0100 Subject: feat: add plain search functionality (with history) --- lib/control.ml | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 lib/control.ml (limited to 'lib/control.ml') diff --git a/lib/control.ml b/lib/control.ml new file mode 100644 index 0000000..670175c --- /dev/null +++ b/lib/control.ml @@ -0,0 +1,32 @@ +open Base + +type t = { prompt : Key.t; content : char Zipper.t } +type result = Search of bool * char Sequence.t | No_result + +let create prompt = { prompt; content = Zipper.empty } + +let render c = + let prompt = Key.to_string c.prompt |> String.to_list + and content = c.content |> Zipper.to_seq in + Sequence.shift_right_with_list content prompt + +let is_search c = match c.prompt with Key '/' | Key '?' -> true | _ -> false + +let cursor c = + String.length (Key.to_string c.prompt) + Zipper.left_length c.content + 1 + +let get_result c = + let open Zipper in + match c.prompt with + | Key '/' -> Search (true, c.content |> to_seq) + | Key '?' -> Search (false, c.content |> to_seq) + | _ -> No_result + +let set_content s c = { c with content = Zipper.(of_seq s |> far_right) } +let move_left c = { c with content = Zipper.left c.content } +let move_right c = { c with content = Zipper.right c.content } +let delete_before c = { c with content = Zipper.pop_before c.content |> snd } +let delete_after c = { c with content = Zipper.pop c.content |> snd } +let bol c = { c with content = Zipper.far_left c.content } +let eol c = { c with content = Zipper.far_right c.content } +let insert k c = { c with content = Zipper.push_before k c.content } -- cgit v1.2.3