diff options
Diffstat (limited to 'lib/control.ml')
| -rw-r--r-- | lib/control.ml | 32 |
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 @@ | |||
| 1 | open Base | ||
| 2 | |||
| 3 | type t = { prompt : Key.t; content : char Zipper.t } | ||
| 4 | type result = Search of bool * char Sequence.t | No_result | ||
| 5 | |||
| 6 | let create prompt = { prompt; content = Zipper.empty } | ||
| 7 | |||
| 8 | let 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 | |||
| 13 | let is_search c = match c.prompt with Key '/' | Key '?' -> true | _ -> false | ||
| 14 | |||
| 15 | let cursor c = | ||
| 16 | String.length (Key.to_string c.prompt) + Zipper.left_length c.content + 1 | ||
| 17 | |||
| 18 | let 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 | |||
| 25 | let set_content s c = { c with content = Zipper.(of_seq s |> far_right) } | ||
| 26 | let move_left c = { c with content = Zipper.left c.content } | ||
| 27 | let move_right c = { c with content = Zipper.right c.content } | ||
| 28 | let delete_before c = { c with content = Zipper.pop_before c.content |> snd } | ||
| 29 | let delete_after c = { c with content = Zipper.pop c.content |> snd } | ||
| 30 | let bol c = { c with content = Zipper.far_left c.content } | ||
| 31 | let eol c = { c with content = Zipper.far_right c.content } | ||
| 32 | let insert k c = { c with content = Zipper.push_before k c.content } | ||
