stitch

Note taking for messy minimalists

git clone git://mccd.space/stitch
commit 5aec082b915416107cec9e8c811bbbedafb70b59
parent 0bc408515cfc9c8feff686f8c758dfaf6c997bb4
Author: Marc Coquand <marc@mccd.space>
Date:   Mon, 20 May 2024 09:07:45 -0500

Enable hiding the file name using 'h'

Diffstat:
Mbin/main.ml | 2+-
Mlib/done.ml | 29+++++++++++++++++++++++------
Mlib/grep.ml | 14++++++++++----
Mlib/headlines.ml | 22++++++++++++++++++----
Mlib/help_screen.ml | 1+
Mlib/todos.ml | 29+++++++++++++++++++++++------
6 files changed, 76 insertions(+), 21 deletions(-)
diff --git a/bin/main.ml b/bin/main.ml
@@ -164,7 +164,7 @@ let headlines_cmd =
     Cmd.info
       ~envs
       "stitch"
-      ~version:"0.0.3 ALPHA"
+      ~version:"0.0.7 ALPHA"
       ~doc
       ~man:
         (List.concat
diff --git a/lib/done.ml b/lib/done.ml
@@ -12,6 +12,7 @@ type state =
   ; goto_todo : (unit -> unit) -> unit
   ; tag : string
   ; output : string option
+  ; hide_file_name : bool
   }
 
 let title = I.strf ~attr:A.(st bold) "%s" "Done" |> I.pad ~l:0 ~t:0
@@ -28,18 +29,28 @@ let init ~goto_todo ~goto_headlines =
   ; goto_todo
   ; tag = ""
   ; output = None
+  ; hide_file_name = false
   }
 
 
-let load_done () =
+let load_done ?(hide_file_name = false) () =
   let done_content = Grep.get_done () |> Grep.parse_todo_string in
-  let done_pretty = Grep.pretty_format_todo done_content in
+  let done_pretty = Grep.pretty_format_todo ~hide_file_name done_content in
   done_content, done_pretty
 
 
 let rec render
   t
-  ({ pos; scroll; content; content_pretty; goto_headlines; goto_todo; output; _ } as state)
+  ({ pos
+   ; scroll
+   ; content
+   ; content_pretty
+   ; goto_headlines
+   ; goto_todo
+   ; output
+   ; hide_file_name
+   ; _
+   } as state)
   =
   let x, y = pos in
   let size_x, size_y = Common.Term.size t in
@@ -112,8 +123,14 @@ 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 'h', []) ->
+    let hide_file_name = not hide_file_name in
+    let content_pretty =
+      Grep.pretty_format_todo ~hide_file_name (content |> Array.to_list) |> Array.of_list
+    in
+    render t { state with hide_file_name; content_pretty }
   | `Key (`ASCII 'g', []) ->
-    let content, content_pretty = load_done () in
+    let content, content_pretty = load_done ~hide_file_name () in
     let y = min (List.length content_pretty + content_start) y in
     render
       t
@@ -142,7 +159,7 @@ let rec render
                 ~content
                 ~selected_file
                 ~on_return:(fun result ->
-                  let content, content_pretty = load_done () in
+                  let content, content_pretty = load_done ~hide_file_name () in
                   let y = min (List.length content_pretty + content_start) y in
                   Common.Term.cursor t None;
                   render
@@ -166,7 +183,7 @@ let rec render
   | `Key (`ASCII 'T', [ `Ctrl ]) ->
     let selected_file, _ = Array.get content (y - content_start) in
     let _ = Grep.toggle_todo selected_file in
-    let content, content_pretty = load_done () in
+    let content, content_pretty = load_done ~hide_file_name () in
     let y = min (List.length content_pretty + content_start) y in
     render
       t
diff --git a/lib/grep.ml b/lib/grep.ml
@@ -94,10 +94,13 @@ let parse_todo_string s =
       | _ -> raise (Not_A_Tuple (String.concat " SPLIT " split, message))))
 
 
-let pretty_format_todo parsed_headlines =
+let pretty_format_todo ?(hide_file_name = false) parsed_headlines =
   let padding = get_padding_list parsed_headlines in
   List.map
-    (fun (file_name, content) -> String.concat " | " [ pad file_name padding; content ])
+    (fun (file_name, content) ->
+      if not hide_file_name
+      then String.concat " | " [ pad file_name padding; content ]
+      else content)
     parsed_headlines
 
 
@@ -236,10 +239,13 @@ let parse_headlines s =
 
 
 (** Turns "2024-03-05.org:* Hello world" into "2024-03-05    | * Hello world" *)
-let pretty_format parsed_headlines =
+let pretty_format ?(hide_file_name = false) parsed_headlines =
   let padding = get_padding_arr parsed_headlines in
   Array.map
-    (fun (file_name, content) -> String.concat " | " [ pad file_name padding; content ])
+    (fun (file_name, content) ->
+      if not hide_file_name
+      then String.concat " | " [ pad file_name padding; content ]
+      else content)
     parsed_headlines
 
 
diff --git a/lib/headlines.ml b/lib/headlines.ml
@@ -14,6 +14,7 @@ type state =
   ; goto_done_view : (unit -> unit) -> unit
   ; output : string option
   ; tag : string
+  ; hide_file_name : bool
   }
 
 let init ~goto_done_view ~goto_todos_view ~regexp =
@@ -37,6 +38,7 @@ let init ~goto_done_view ~goto_todos_view ~regexp =
     ; goto_todos_view
     ; output = None
     ; tag = ""
+    ; hide_file_name = false
     })
 
 
@@ -45,8 +47,16 @@ let title = I.strf ~attr:A.(st bold) "%s" "Notes" |> I.pad ~l:0 ~t:0
 (* TODO: Add page title *)
 let rec render
   t
-  ({ pos; scroll; content; content_pretty; goto_todos_view; goto_done_view; tag; output }
-   as state)
+  ({ pos
+   ; scroll
+   ; content
+   ; content_pretty
+   ; goto_todos_view
+   ; goto_done_view
+   ; tag
+   ; output
+   ; hide_file_name
+   } as state)
   =
   let content_start = 2 in
   let size_x, size_y = Common.Term.size t in
@@ -96,6 +106,10 @@ let rec render
     render t { state with pos = 0, min y content_end; output = None }
   | `Key (`ASCII '?', []) -> Help_screen.render t { go_back = (fun () -> render t state) }
   | `Key (`ASCII '2', []) -> goto_todos_view (fun () -> render t state)
+  | `Key (`ASCII 'h', []) ->
+    let hide_file_name = not hide_file_name in
+    let content_pretty = content |> Grep.pretty_format ~hide_file_name in
+    render t { state with hide_file_name; content_pretty }
   | `Key (`ASCII '3', []) -> goto_done_view (fun () -> render t state)
   | `Key (`ASCII 'f', []) ->
     let content =
@@ -128,7 +142,7 @@ let rec render
       ; on_enter =
           (fun tag ->
             let content = Grep.get_tagged_headlines tag () |> Grep.parse_headlines in
-            let content_pretty = Grep.pretty_format content in
+            let content_pretty = Grep.pretty_format ~hide_file_name content in
             Common.Term.cursor t None;
             render
               t
@@ -176,7 +190,7 @@ let rec render
                   let content =
                     Grep.get_tagged_headlines tag () |> Grep.parse_headlines
                   in
-                  let content_pretty = Grep.pretty_format content in
+                  let content_pretty = Grep.pretty_format ~hide_file_name content in
                   Common.Term.cursor t None;
                   render
                     t
diff --git a/lib/help_screen.ml b/lib/help_screen.ml
@@ -31,6 +31,7 @@ let general_help_menu =
   ; "Done", "3"
   ; "Run Shell Command", "!"
   ; "Edit File in $EDITOR", "Enter, e"
+  ; "Toggle Hide File Name", "h"
   ]
 
 
diff --git a/lib/todos.ml b/lib/todos.ml
@@ -12,6 +12,7 @@ type state =
   ; goto_done : (unit -> unit) -> unit
   ; output : string option
   ; tag : string
+  ; hide_file_name : bool
   }
 
 let title = I.strf ~attr:A.(st bold) "%s" "Todo" |> I.pad ~l:0 ~t:0
@@ -28,18 +29,28 @@ let init ~goto_done ~goto_headlines =
   ; goto_done
   ; output = None
   ; tag = ""
+  ; hide_file_name = false
   }
 
 
-let load_todos () =
+let load_todos ?(hide_file_name = false) () =
   let todo_content = Grep.get_todos () |> Grep.parse_todo_string in
-  let todo_pretty = Grep.pretty_format_todo todo_content in
+  let todo_pretty = Grep.pretty_format_todo ~hide_file_name todo_content in
   todo_content, todo_pretty
 
 
 let rec render
   t
-  ({ pos; scroll; content; content_pretty; goto_headlines; goto_done; output; _ } as state)
+  ({ pos
+   ; scroll
+   ; content
+   ; content_pretty
+   ; goto_headlines
+   ; goto_done
+   ; output
+   ; hide_file_name
+   ; _
+   } as state)
   =
   let x, y = pos in
   let size_x, size_y = Common.Term.size t in
@@ -90,7 +101,7 @@ let rec render
   | `Key (`ASCII '1', []) -> goto_headlines (fun () -> render t state)
   | `Key (`ASCII '3', []) -> goto_done (fun () -> render t state)
   | `Key (`ASCII 'g', []) ->
-    let content, content_pretty = load_todos () in
+    let content, content_pretty = load_todos ~hide_file_name () in
     let y = min (List.length content_pretty + content_start) y in
     render
       t
@@ -145,7 +156,7 @@ let rec render
                 ~content
                 ~selected_file
                 ~on_return:(fun result ->
-                  let content, content_pretty = load_todos () in
+                  let content, content_pretty = load_todos ~hide_file_name () in
                   let y = min (List.length content_pretty + content_start) y in
                   Common.Term.cursor t None;
                   render
@@ -164,10 +175,16 @@ let rec render
       }
     in
     Input_prompt.render t input_state
+  | `Key (`ASCII 'h', []) ->
+    let hide_file_name = not hide_file_name in
+    let content_pretty =
+      Grep.pretty_format_todo ~hide_file_name (content |> Array.to_list) |> Array.of_list
+    in
+    render t { state with hide_file_name; content_pretty }
   | `Key (`ASCII 'T', [ `Ctrl ]) ->
     let selected_file, _ = Array.get content (y - content_start) in
     let _ = Grep.toggle_done selected_file in
-    let content, content_pretty = load_todos () in
+    let content, content_pretty = load_todos ~hide_file_name () in
     let y = min (List.length content_pretty + content_start) y in
     render
       t