stitch

Note taking for messy minimalists

git clone git://mccd.space/stitch
commit 87dcfa6edd43316068347ff5b8f23c76bdc7b472
parent 2c0f6e026acb2ecfc1c39d617f47d024e404bb9d
Author: Marc Coquand <marc@mccd.space>
Date:   Thu, 30 May 2024 10:04:25 -0500

Add half-screen down/up scrolling

Diffstat:
Mlib/basic.ml | 1-
Mlib/done.ml | 18++++++++++++------
Mlib/headlines.ml | 18++++++++++++------
Mlib/help_screen.ml | 2++
Mlib/stitched_article.ml | 21++++++++++++++-------
Mlib/todos.ml | 18++++++++++++------
6 files changed, 52 insertions(+), 26 deletions(-)
diff --git a/lib/basic.ml b/lib/basic.ml
@@ -1,3 +1,2 @@
 let array_drop n arr =
   if Array.length arr < n then [||] else Array.sub arr n (max (Array.length arr - n) 0)
-
diff --git a/lib/done.ml b/lib/done.ml
@@ -87,13 +87,15 @@ let rec render
   in
   Common.Term.image t img;
   let content_end = Array.length content_pretty + (content_start - 1) in
-  let scroll_up () =
-    let scroll = if y - content_start - scroll = 0 then max (scroll - 1) 0 else scroll in
-    render t { state with pos = x, max (y - 1) content_start; scroll; output = None }
+  let scroll_up ?(amount = 1) () =
+    let scroll =
+      if y - content_start - scroll - amount < 0 then max (scroll - amount) 0 else scroll
+    in
+    render t { state with pos = x, max (y - amount) content_start; scroll; output = None }
   in
-  let scroll_down () =
-    let scroll = if y - scroll >= size_y - 1 then scroll + 1 else scroll in
-    render t { state with pos = x, min (y + 1) content_end; scroll; output = None }
+  let scroll_down ?(amount = 1) () =
+    let scroll = if y - scroll >= size_y - amount then scroll + amount else scroll in
+    render t { state with pos = x, min (y + amount) content_end; scroll; output = None }
   in
   match Common.Term.event t with
   | `End | `Key (`Escape, []) | `Key (`ASCII 'q', []) | `Key (`ASCII 'C', [ `Ctrl ]) -> ()
@@ -132,6 +134,10 @@ let rec render
     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 'D', [ `Ctrl ]) | `Key (`Page `Down, []) ->
+    scroll_down ~amount:(size_y / 2) ()
+  | `Key (`ASCII 'U', [ `Ctrl ]) | `Key (`Page `Up, []) ->
+    scroll_up ~amount:(size_y / 2) ()
   | `Key (`ASCII 'h', []) ->
     let hide_file_name = not hide_file_name in
     let content_pretty =
diff --git a/lib/headlines.ml b/lib/headlines.ml
@@ -93,13 +93,15 @@ let rec render
   in
   Common.Term.image t img;
   let content_end = Array.length content_pretty + (content_start - 1) in
-  let scroll_up () =
-    let scroll = if y - content_start - scroll = 0 then max (scroll - 1) 0 else scroll in
-    render t { state with pos = x, max (y - 1) content_start; scroll; output = None }
+  let scroll_up ?(amount = 1) () =
+    let scroll =
+      if y - content_start - scroll - amount < 0 then max (scroll - amount) 0 else scroll
+    in
+    render t { state with pos = x, max (y - amount) content_start; scroll; output = None }
   in
-  let scroll_down () =
-    let scroll = if y - scroll >= size_y - 1 then scroll + 1 else scroll in
-    render t { state with pos = x, min (y + 1) content_end; scroll; output = None }
+  let scroll_down ?(amount = 1) () =
+    let scroll = if y - scroll >= size_y - amount then scroll + amount else scroll in
+    render t { state with pos = x, min (y + amount) content_end; scroll; output = None }
   in
   match Common.Term.event t with
   | `End | `Key (`Escape, []) | `Key (`ASCII 'q', []) | `Key (`ASCII 'C', [ `Ctrl ]) -> ()
@@ -189,6 +191,10 @@ let rec render
       }
     in
     Input_prompt.render t input_state
+  | `Key (`ASCII 'D', [ `Ctrl ]) | `Key (`Page `Down, []) ->
+    scroll_down ~amount:(size_y / 2) ()
+  | `Key (`ASCII 'U', [ `Ctrl ]) | `Key (`Page `Up, []) ->
+    scroll_up ~amount:(size_y / 2) ()
   | `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/help_screen.ml b/lib/help_screen.ml
@@ -25,6 +25,8 @@ let general_help_menu =
   ; "Exit", "Ctrl-c, q, Esc"
   ; "Down", "Ctrl-n, j"
   ; "Up", "Ctrl-p, k"
+  ; "Half-screen down", "Ctrl-d, PgDn"
+  ; "Half-screen up", "Ctrl-u, PgUp"
   ; "Regexp Search", "s"
   ; "Notes", "1"
   ; "Todo", "2"
diff --git a/lib/stitched_article.ml b/lib/stitched_article.ml
@@ -72,14 +72,17 @@ let rec render
   in
   Common.Term.image t img;
   let content_length = Array.length content_pretty in
-  let scroll_up () =
-    let scroll = if y - content_start - scroll = 0 then max (scroll - 1) 0 else scroll in
-    render t @@ { state with pos = x, max (y - 1) content_start; scroll; output = None }
+  let scroll_up ?(amount = 1) () =
+    let scroll =
+      if y - content_start - scroll - amount < 0 then max (scroll - amount) 0 else scroll
+    in
+    render t { state with pos = x, max (y - amount) content_start; scroll; output = None }
   in
-  let scroll_down () =
-    let scroll = if y - scroll >= size_y - 1 then scroll + 1 else scroll in
-    render t
-    @@ { state with pos = x, min (y + 1) (content_length - 1); scroll; output = None }
+  let scroll_down ?(amount = 1) () =
+    let scroll = if y - scroll >= size_y - amount then scroll + amount else scroll in
+    render
+      t
+      { state with pos = x, min (y + amount) content_length; scroll; output = None }
   in
   match Common.Term.event t with
   | `End | `Key (`Escape, []) | `Key (`ASCII 'q', []) | `Key (`ASCII 'C', [ `Ctrl ]) -> ()
@@ -120,6 +123,10 @@ let rec render
       }
     in
     Input_prompt.render t input_state
+  | `Key (`ASCII 'D', [ `Ctrl ]) | `Key (`Page `Down, []) ->
+    scroll_down ~amount:(size_y / 2) ()
+  | `Key (`ASCII 'U', [ `Ctrl ]) | `Key (`Page `Up, []) ->
+    scroll_up ~amount:(size_y / 2) ()
   | `Key (`ASCII '!', []) ->
     let file_number_offset, content_line = Array.get content_pretty (y - content_start) in
     let content_line = Grep.display_type_line content_line in
diff --git a/lib/todos.ml b/lib/todos.ml
@@ -94,13 +94,15 @@ let rec render
   in
   Common.Term.image t img;
   let content_end = Array.length content_pretty + (content_start - 1) in
-  let scroll_up () =
-    let scroll = if y - content_start - scroll = 0 then max (scroll - 1) 0 else scroll in
-    render t { state with pos = x, max (y - 1) content_start; scroll; output = None }
+  let scroll_up ?(amount = 1) () =
+    let scroll =
+      if y - content_start - scroll - amount < 0 then max (scroll - amount) 0 else scroll
+    in
+    render t { state with pos = x, max (y - amount) content_start; scroll; output = None }
   in
-  let scroll_down () =
-    let scroll = if y - scroll >= size_y - 1 then scroll + 1 else scroll in
-    render t { state with pos = x, min (y + 1) content_end; scroll; output = None }
+  let scroll_down ?(amount = 1) () =
+    let scroll = if y - scroll >= size_y - amount then scroll + amount else scroll in
+    render t { state with pos = x, min (y + amount) content_end; scroll; output = None }
   in
   match Common.Term.event t with
   | `End | `Key (`Escape, []) | `Key (`ASCII 'q', []) | `Key (`ASCII 'C', [ `Ctrl ]) -> ()
@@ -114,6 +116,10 @@ let rec render
   | `Key (`ASCII '?', []) -> Help_screen.render t { go_back = (fun () -> render t state) }
   | `Key (`ASCII '1', []) -> goto_headlines (fun () -> render t state)
   | `Key (`ASCII '3', []) -> goto_done (fun () -> render t state)
+  | `Key (`ASCII 'D', [ `Ctrl ]) | `Key (`Page `Down, []) ->
+    scroll_down ~amount:(size_y / 2) ()
+  | `Key (`ASCII 'U', [ `Ctrl ]) | `Key (`Page `Up, []) ->
+    scroll_up ~amount:(size_y / 2) ()
   | `Key (`ASCII 'g', []) ->
     let content, content_pretty = load_todos ~hide_file_name () in
     let y = min (List.length content_pretty + content_start) y in