From ebb302498593f15330affa732c5206e31d882d8f Mon Sep 17 00:00:00 2001 From: Marc Coquand Date: Wed, 15 May 2024 15:25:36 -0500 Subject: Rename screen -> Prompt --- lib/done.ml | 4 ++-- lib/headlines.ml | 6 +++--- lib/input_prompt.ml | 38 ++++++++++++++++++++++++++++++++++++++ lib/input_screen.ml | 38 -------------------------------------- lib/stitched_article.ml | 6 +++--- lib/todos.ml | 4 ++-- 6 files changed, 48 insertions(+), 48 deletions(-) create mode 100644 lib/input_prompt.ml delete mode 100644 lib/input_screen.ml diff --git a/lib/done.ml b/lib/done.ml index 815f663..63e25a9 100644 --- a/lib/done.ml +++ b/lib/done.ml @@ -74,7 +74,7 @@ let rec render render t { state with pos = 0, min y content_end } | `Key (`ASCII '?', []) -> Help_screen.render t { go_back = (fun () -> render t state) } | `Key (`ASCII 'r', []) -> - let (input_state : Input_screen.state) = + let (input_state : Input_prompt.state) = { screen = img ; user_input = "" ; prompt = "REGEXP: " @@ -95,7 +95,7 @@ let rec render render t state) } in - Input_screen.render t input_state + Input_prompt.render t input_state | `Key (`ASCII '1', []) -> goto_headlines (fun () -> render t state) | `Key (`ASCII '2', []) -> goto_todo (fun () -> render t state) | `Key (`ASCII 'g', []) -> diff --git a/lib/headlines.ml b/lib/headlines.ml index 6b79262..36191bd 100644 --- a/lib/headlines.ml +++ b/lib/headlines.ml @@ -1,7 +1,7 @@ module Grep = Grep module Common = Common open Notty -module Input_screen = Input_screen +module Input_prompt = Input_prompt module Stitched_article = Stitched_article module Help_screen = Help_screen @@ -98,7 +98,7 @@ let rec render ; goto_done_view } | `Key (`ASCII 'r', []) -> - let (input_state : Input_screen.state) = + let (input_state : Input_prompt.state) = { screen = img ; user_input = "" ; prompt = "REGEXP: " @@ -116,7 +116,7 @@ let rec render render t state) } in - Input_screen.render t input_state + Input_prompt.render t input_state | `Key (`ASCII 'j', []) | `Key (`ASCII 'N', [ `Ctrl ]) -> scroll_down () | `Key (`ASCII 'k', []) | `Key (`ASCII 'P', [ `Ctrl ]) -> scroll_up () | `Key (`Arrow d, _) -> 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 diff --git a/lib/input_screen.ml b/lib/input_screen.ml deleted file mode 100644 index 45979aa..0000000 --- a/lib/input_screen.ml +++ /dev/null @@ -1,38 +0,0 @@ -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 diff --git a/lib/stitched_article.ml b/lib/stitched_article.ml index 981258b..1698e68 100644 --- a/lib/stitched_article.ml +++ b/lib/stitched_article.ml @@ -1,7 +1,7 @@ module Grep = Grep module Common = Common open Notty -module Input_screen = Input_screen +module Input_prompt = Input_prompt module Help_screen = Help_screen type state = @@ -58,7 +58,7 @@ let rec render | `Key (`ASCII 'j', []) | `Key (`ASCII 'N', [ `Ctrl ]) -> scroll_down () | `Key (`ASCII 'k', []) | `Key (`ASCII 'P', [ `Ctrl ]) -> scroll_up () | `Key (`ASCII 'r', []) -> - let (input_state : Input_screen.state) = + let (input_state : Input_prompt.state) = { screen = img ; user_input = "" ; prompt = "REGEXP: " @@ -100,7 +100,7 @@ let rec render render t state) } in - Input_screen.render t input_state + Input_prompt.render t input_state | `Key (`ASCII '?', []) -> Help_screen.render t { go_back = (fun () -> render t state) } | `Key (`ASCII '2', []) -> goto_todos_view (fun () -> render t state) | `Key (`ASCII '3', []) -> goto_done_view (fun () -> render t state) diff --git a/lib/todos.ml b/lib/todos.ml index bc8607f..57f03a8 100644 --- a/lib/todos.ml +++ b/lib/todos.ml @@ -86,7 +86,7 @@ let rec render ; content_pretty = Array.of_list content_pretty } | `Key (`ASCII 'r', []) -> - let (input_state : Input_screen.state) = + let (input_state : Input_prompt.state) = { screen = img ; user_input = "" ; prompt = "REGEXP: " @@ -107,7 +107,7 @@ let rec render render t state) } in - Input_screen.render t input_state + Input_prompt.render t input_state | `Key (`ASCII 'j', []) | `Key (`ASCII 'N', [ `Ctrl ]) -> scroll_down () | `Key (`ASCII 'k', []) | `Key (`ASCII 'P', [ `Ctrl ]) -> scroll_up () | `Key (`ASCII 't', []) -> -- cgit v1.2.3