stitch

Note taking for messy minimalists

git clone git://mccd.space/stitch
commit fde592dbbb97a89a498feb95f97bee674bd571e8
parent 2b47b301cbf234eabfcebd28f069e5d154441354
Author: Marc Coquand <marc@mccd.space>
Date:   Mon, 13 May 2024 13:35:36 -0500

Refactor

Diffstat:
MREADME.org | 3++-
Mbin/main.ml | 78++----------------------------------------------------------------------------
Rbin/common.ml -> lib/common.ml | 0
Mlib/dune | 5++++-
Rlib/stitch.ml -> lib/grep.ml | 0
Alib/headlines.ml | 79+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mstitch.opam | 7++++---
7 files changed, 91 insertions(+), 81 deletions(-)
diff --git a/README.org b/README.org
@@ -56,12 +56,13 @@ dune exec -- stitch
 You can build a basic capture command using $EDITOR and date command:
 
 alias capture="JRNL=\"$STITCH_DIRECTORY/$(date +'%Y-%m-%d
-%H:%M').org\" echo '* :journal:' > $JRNL_FILE && $EDITOR $JRNL_FILE
+%H:%M').org\" echo '* :journal:' > $JRNL_FILE && $EDITOR $JRNL"
 
 and then you can find your journal entries, automatically sorted by
 creation date with stitch and the journal tag:
 
 alias jrnl="stitch -t journal"
+
 * KNOWN ISSUES
 
 - Resizing the screen when editor is open causes panic
diff --git a/bin/main.ml b/bin/main.ml
@@ -1,77 +1,3 @@
-open Notty
-open Common
+open Stitch
 
-let content = Stitch.get_headlines () |> Stitch.parse_headlines
-let content_pretty = content |> Stitch.pretty_format
-
-let rec main t (((x, y) as pos), scroll) =
-  let img =
-    let dot = I.string A.(fg black) ">" |> I.pad ~l:0 ~t:(y - scroll)
-    and elements =
-      Array.mapi
-        (fun i el -> I.strf ~attr:A.(fg black) "%s" el |> I.pad ~l:2 ~t:i)
-        (Array.to_seq content_pretty |> Seq.drop scroll |> Array.of_seq)
-    in
-    let open I in
-    Array.fold_left (fun sum el -> el </> sum) dot elements
-  in
-  let _, size_y = Term.size t in
-  Term.image t img;
-  let content_length = Array.length content_pretty in
-  let scroll_up () =
-    let scroll = if y - scroll = 0 then max (scroll - 1) 0 else scroll in
-    main t @@ ((x, max (y - 1) 0), scroll)
-  in
-  let scroll_down () =
-    let scroll = if y - scroll >= size_y - 1 then scroll + 1 else scroll in
-    main t @@ ((x, min (y + 1) content_length), scroll)
-  in
-  match Term.event t with
-  | `End | `Key (`Escape, []) | `Key (`ASCII 'q', []) | `Key (`ASCII 'C', [ `Ctrl ]) -> ()
-  | `Mouse (`Press (`Scroll s), _, _) ->
-    (match s with
-     | `Down -> scroll_down ()
-     | `Up -> scroll_up ())
-  | `Resize _ -> main t (pos, scroll)
-  | `Mouse ((`Press _ | `Drag), (_, y), _) -> main t ((0, min y content_length), scroll)
-  | `Key (`ASCII 'j', []) | `Key (`ASCII 'N', [ `Ctrl ]) -> scroll_down ()
-  | `Key (`ASCII 'k', []) | `Key (`ASCII 'P', [ `Ctrl ]) ->
-    let scroll = if y - scroll = 0 then max (scroll - 1) 0 else scroll in
-    main t @@ ((x, max (y - 1) 0), scroll)
-  | `Key (`Arrow d, _) ->
-    (match d with
-     | `Up -> scroll_up ()
-     | `Down -> scroll_down ()
-     | _ -> main t ((x, y), scroll))
-  | `Key (`ASCII 'e', []) | `Key (`Enter, []) ->
-    (* Editor might be set with extra args, in that case we need to separate these *)
-    let[@warning "-8"] (editor :: args) =
-      String.split_on_char ' ' (Sys.getenv "EDITOR")
-    in
-    let selected_file, _ = Array.get content y in
-    let full_path_file = Stitch.execution_directory ^ "/" ^ selected_file in
-    let full_args = Array.append (Array.of_list args) [| full_path_file |] in
-    Term.cursor t (Some (0, 0));
-    let _ =
-      Unix.create_process_env
-        editor
-        full_args
-        (Unix.environment ())
-        Unix.stdin
-        Unix.stdout
-        Unix.stderr
-    in
-    let rec run_editor () =
-      match Unix.wait () with
-      | _, _ ->
-        Term.cursor t None;
-        main t ((x, y), scroll)
-      (* Capture resizing events *)
-      | exception Unix.Unix_error (Unix.EINTR, _, _) -> run_editor ()
-      | exception Unix.Unix_error (_, _, _) -> failwith "ERROR"
-    in
-    run_editor ()
-  | _ -> main t (pos, scroll)
-
-
-let () = main (Term.create ()) ((0, 0), 0)
+let () = Headlines.start ()
diff --git a/bin/common.ml b/lib/common.ml
diff --git a/lib/dune b/lib/dune
@@ -1,7 +1,10 @@
 (library
  (name stitch)
  (libraries
+  unix
+  cmdliner
   str
+  notty
+  notty.unix
   shexp.process
-  cmdliner
   lambda-term))
diff --git a/lib/stitch.ml b/lib/grep.ml
diff --git a/lib/headlines.ml b/lib/headlines.ml
@@ -0,0 +1,79 @@
+module Grep = Grep
+module Common = Common
+open Notty
+
+let content = Grep.get_headlines () |> Grep.parse_headlines
+let content_pretty = content |> Grep.pretty_format
+
+let rec headline_screen t (((x, y) as pos), scroll) =
+  let img =
+    let dot = I.string A.(fg black) ">" |> I.pad ~l:0 ~t:(y - scroll)
+    and elements =
+      Array.mapi
+        (fun i el -> I.strf ~attr:A.(fg black) "%s" el |> I.pad ~l:2 ~t:i)
+        (Array.to_seq content_pretty |> Seq.drop scroll |> Array.of_seq)
+    in
+    let open I in
+    Array.fold_left (fun sum el -> el </> sum) dot elements
+  in
+  let _, size_y = Common.Term.size t in
+  Common.Term.image t img;
+  let content_length = Array.length content_pretty in
+  let scroll_up () =
+    let scroll = if y - scroll = 0 then max (scroll - 1) 0 else scroll in
+    headline_screen t @@ ((x, max (y - 1) 0), scroll)
+  in
+  let scroll_down () =
+    let scroll = if y - scroll >= size_y - 1 then scroll + 1 else scroll in
+    headline_screen t @@ ((x, min (y + 1) content_length), scroll)
+  in
+  match Common.Term.event t with
+  | `End | `Key (`Escape, []) | `Key (`ASCII 'q', []) | `Key (`ASCII 'C', [ `Ctrl ]) -> ()
+  | `Mouse (`Press (`Scroll s), _, _) ->
+    (match s with
+     | `Down -> scroll_down ()
+     | `Up -> scroll_up ())
+  | `Resize _ -> headline_screen t (pos, scroll)
+  | `Mouse ((`Press _ | `Drag), (_, y), _) ->
+    headline_screen t ((0, min y content_length), scroll)
+  | `Key (`ASCII 'j', []) | `Key (`ASCII 'N', [ `Ctrl ]) -> scroll_down ()
+  | `Key (`ASCII 'k', []) | `Key (`ASCII 'P', [ `Ctrl ]) ->
+    let scroll = if y - scroll = 0 then max (scroll - 1) 0 else scroll in
+    headline_screen t @@ ((x, max (y - 1) 0), scroll)
+  | `Key (`Arrow d, _) ->
+    (match d with
+     | `Up -> scroll_up ()
+     | `Down -> scroll_down ()
+     | _ -> headline_screen t ((x, y), scroll))
+  | `Key (`ASCII 'e', []) | `Key (`Enter, []) ->
+    (* Editor might be set with extra args, in that case we need to separate these *)
+    let[@warning "-8"] (editor :: args) =
+      String.split_on_char ' ' (Sys.getenv "EDITOR")
+    in
+    let selected_file, _ = Array.get content y in
+    let full_path_file = Grep.execution_directory ^ "/" ^ selected_file in
+    let full_args = Array.append (Array.of_list args) [| full_path_file |] in
+    Common.Term.cursor t (Some (0, 0));
+    let _ =
+      Unix.create_process_env
+        editor
+        full_args
+        (Unix.environment ())
+        Unix.stdin
+        Unix.stdout
+        Unix.stderr
+    in
+    let rec run_editor () =
+      match Unix.wait () with
+      | _, _ ->
+        Common.Term.cursor t None;
+        headline_screen t ((x, y), scroll)
+      (* Capture resizing events *)
+      | exception Unix.Unix_error (Unix.EINTR, _, _) -> run_editor ()
+      | exception Unix.Unix_error (_, _, _) -> failwith "ERROR"
+    in
+    run_editor ()
+  | _ -> headline_screen t (pos, scroll)
+
+
+let start () = headline_screen (Common.Term.create ()) ((0, 0), 0)
diff --git a/stitch.opam b/stitch.opam
@@ -1,10 +1,11 @@
 # This file is generated by dune, edit dune-project instead
 opam-version: "2.0"
-synopsis: "A Terminal Note Taking App"
-description: ""
+synopsis: "A Note Composer"
+description:
+  "A minimal CLI tool that allows you to compose notes together. Useful as part of a bigger system for building a PKM."
 maintainer: ["Marc Coquand"]
 authors: ["Marc Coquand"]
-license: "GPL-3.0-only"
+license: "BSD-3-Clause"
 tags: ["productivity" "minimal"]
 doc: "https://url/to/documentation"
 depends: [