aboutsummaryrefslogtreecommitdiff
path: root/lib/input_prompt.ml
diff options
context:
space:
mode:
authorMarc Coquand <marc@mccd.space>2024-05-15 15:25:36 -0500
committerMarc Coquand <marc@mccd.space>2024-05-15 15:25:36 -0500
commitebb302498593f15330affa732c5206e31d882d8f (patch)
treeb874255b175974576a19789663f6532e4aa06239 /lib/input_prompt.ml
parentfa2582d562118dc1ceccd01ede8d0d7e80d71a29 (diff)
downloadstitch-ebb302498593f15330affa732c5206e31d882d8f.tar.gz
stitch-ebb302498593f15330affa732c5206e31d882d8f.tar.bz2
stitch-ebb302498593f15330affa732c5206e31d882d8f.zip
Rename screen -> Prompt
Diffstat (limited to 'lib/input_prompt.ml')
-rw-r--r--lib/input_prompt.ml38
1 files changed, 38 insertions, 0 deletions
diff --git a/lib/input_prompt.ml b/lib/input_prompt.ml
new file mode 100644
index 0000000..45979aa
--- /dev/null
+++ b/lib/input_prompt.ml
@@ -0,0 +1,38 @@
+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, []) ->
+ if String.equal "" user_input
+ then on_cancel ()
+ else (
+ 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