summaryrefslogtreecommitdiff
path: root/lib/control.ml
diff options
context:
space:
mode:
authorFederico Igne <undyamon@disroot.org>2024-01-28 01:22:52 +0100
committerFederico Igne <undyamon@disroot.org>2024-01-28 01:22:52 +0100
commit53916fc3f7dcbefd90e3d0340a2a8f32bf331d1d (patch)
treebaf9b5d6bdfdc77d0338e230adf64f7f52aefa4e /lib/control.ml
parent722a3d4d2d0d0630f57cfbfdddc229dee341505b (diff)
downloadsandy-53916fc3f7dcbefd90e3d0340a2a8f32bf331d1d.tar.gz
sandy-53916fc3f7dcbefd90e3d0340a2a8f32bf331d1d.zip
feat: add plain search functionality (with history)
Diffstat (limited to 'lib/control.ml')
-rw-r--r--lib/control.ml32
1 files changed, 32 insertions, 0 deletions
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 @@
1open Base
2
3type t = { prompt : Key.t; content : char Zipper.t }
4type result = Search of bool * char Sequence.t | No_result
5
6let create prompt = { prompt; content = Zipper.empty }
7
8let render c =
9 let prompt = Key.to_string c.prompt |> String.to_list
10 and content = c.content |> Zipper.to_seq in
11 Sequence.shift_right_with_list content prompt
12
13let is_search c = match c.prompt with Key '/' | Key '?' -> true | _ -> false
14
15let cursor c =
16 String.length (Key.to_string c.prompt) + Zipper.left_length c.content + 1
17
18let get_result c =
19 let open Zipper in
20 match c.prompt with
21 | Key '/' -> Search (true, c.content |> to_seq)
22 | Key '?' -> Search (false, c.content |> to_seq)
23 | _ -> No_result
24
25let set_content s c = { c with content = Zipper.(of_seq s |> far_right) }
26let move_left c = { c with content = Zipper.left c.content }
27let move_right c = { c with content = Zipper.right c.content }
28let delete_before c = { c with content = Zipper.pop_before c.content |> snd }
29let delete_after c = { c with content = Zipper.pop c.content |> snd }
30let bol c = { c with content = Zipper.far_left c.content }
31let eol c = { c with content = Zipper.far_right c.content }
32let insert k c = { c with content = Zipper.push_before k c.content }