summaryrefslogtreecommitdiff
path: root/lib/control.ml
blob: 670175c9f1842cd8eedc588910e6166e0385f0f1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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 }