stitch

Note taking for messy minimalists

git clone git://mccd.space/stitch
commit 527e025e88fc01c7ed4fc3f951484b051c264d83
parent eed9e36b661009e5bcf1c4f5ec3a9279bbb7cda5
Author: Marc Coquand <marc@mccd.space>
Date:   Thu, 23 May 2024 14:04:23 -0500

Fix export with relative line

Diffstat:
Mlib/export.ml | 16+++++++++++++---
Mlib/headlines.ml | 4++--
Mlib/stitched_article.ml | 6+++---
3 files changed, 18 insertions(+), 8 deletions(-)
diff --git a/lib/export.ml b/lib/export.ml
@@ -1,4 +1,14 @@
 let to_path path ~content =
-  let oc = open_out path in
-  Printf.fprintf oc "%s" content;
-  close_out oc
+  try
+    let home = Sys.getenv_opt "HOME" in
+    let path =
+      match home with
+      | Some home -> Str.replace_first (Str.regexp "~") home path
+      | None -> path
+    in
+    let oc = open_out path in
+    Printf.fprintf oc "%s\n" content;
+    close_out oc;
+    "Successfully wrote to " ^ path
+  with
+  | exn -> "Failed to write to path: " ^ Printexc.to_string exn
diff --git a/lib/headlines.ml b/lib/headlines.ml
@@ -118,8 +118,8 @@ let rec render
             if String.equal (String.trim path) String.empty |> not
             then (
               let export = String.concat "\n" (Array.to_list content_pretty) in
-              Export.to_path path ~content:export;
-              render t { state with output = Some ("Export written to: " ^ path) })
+              let result = Export.to_path path ~content:export in
+              render t { state with output = Some result })
             else render t { state with output = Some "Export Blank; skipping" })
       ; on_cancel =
           (fun _ ->
diff --git a/lib/stitched_article.ml b/lib/stitched_article.ml
@@ -95,13 +95,13 @@ let rec render
                   (Array.to_list content_pretty
                    |> List.map (fun (_, b) -> Grep.display_type_line b))
               in
-              Export.to_path path ~content:export;
-              render t { state with output = Some ("Export written to: " ^ path ) })
+              let result = Export.to_path path ~content:export in
+              render t { state with output = Some result })
             else render t { state with output = Some "Export Blank; skipping" })
       ; on_cancel =
           (fun _ ->
             Common.Term.cursor t None;
-            render t {state with output = Some "Export Cancelled"})
+            render t { state with output = Some "Export Cancelled" })
       }
     in
     Input_prompt.render t input_state