aboutsummaryrefslogtreecommitdiff
path: root/lib/input_screen.ml
blob: 3fae53c571ceb0e234db650ff3bc7731430992af (plain)
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
33
34
35
module Grep = Grep
module Common = Common
open Notty

type state =
  { user_input : string
  ; on_enter : string -> unit
  ; on_cancel : unit -> unit
  ; prompt : string
  ; screen : I.t
  }

let rec render t ({ user_input; on_enter; on_cancel; screen; prompt } as state) =
  let _, size_y = Common.Term.size t in
  Common.Term.cursor t (Some (String.length user_input + String.length prompt, size_y));
  let img =
    let open I in
    I.strf "%s%s" prompt user_input |> I.pad ~l:0 ~t:(size_y - 1) </> screen
  in
  Common.Term.image t img;
  match Common.Term.event t with
  | `End | `Key (`ASCII 'G', [ `Ctrl ]) | `Key (`ASCII 'C', [ `Ctrl ]) -> on_cancel ()
  | `Key (`Enter, []) -> on_enter user_input
  | `Key (`Backspace, []) ->
    let state =
      { state with
        user_input = String.sub user_input 0 (max (String.length user_input - 1) 0)
      }
    in
    render t state
  | `Resize _ -> render t state
  | `Key (`ASCII c, []) ->
    let state = { state with user_input = user_input ^ String.make 1 c } in
    render t state
  | _ -> render t state