stitch

Note taking for messy minimalists

git clone git://mccd.space/stitch

stitch.ml (2598B)

      1 module Grep = Grep
      2 module Common = Common
      3 module Todos = Todos
      4 module Headlines = Headlines
      5 
      6 let start (tag : string) () =
      7   (* This is a rather funky state management.
      8      What we do is store a function for each view that restores it's state. Since the render function is
      9      void -> void
     10 
     11      This allows us to remember the state of the view and restore it as we travel between different views.
     12 
     13      It does create a cyclical state though.
     14   *)
     15   if String.equal String.empty (String.trim Grep.execution_directory)
     16   then
     17     print_endline
     18       "Execution directory is not set. You will need to set environment variable \
     19        STITCH_DIRECTORY to the absolute path of your notes. For example: \
     20        STITCH_DIRECTORY='/home/you/notes'."
     21   else (
     22     let term = Common.Term.create () in
     23     let restore_headline_state = ref (fun () -> ()) in
     24     let restore_done_state = ref (fun () -> ()) in
     25     let restore_todo_state = ref (fun () -> ()) in
     26     (* INIT DONE VIEW *)
     27     print_endline "DONE INIT";
     28     let goto_todo_from_done new_done_state =
     29       restore_done_state := new_done_state;
     30       !restore_todo_state ()
     31     in
     32     let goto_headlines_from_done new_done_state =
     33       restore_done_state := new_done_state;
     34       !restore_headline_state ()
     35     in
     36     (restore_done_state
     37      := fun () ->
     38           let done_state =
     39             Done.init
     40               ~goto_headlines:goto_headlines_from_done
     41               ~goto_todo:goto_todo_from_done
     42           in
     43           Done.render term done_state);
     44     (* INIT TODO VIEW *)
     45     print_endline "TODO INIT";
     46     let goto_done_from_todo new_todo_state =
     47       restore_todo_state := new_todo_state;
     48       !restore_done_state ()
     49     in
     50     let goto_headline_from_todo new_todo_state =
     51       restore_todo_state := new_todo_state;
     52       !restore_headline_state ()
     53     in
     54     (restore_todo_state
     55      := fun () ->
     56           let todo =
     57             Todos.init
     58               ~goto_headlines:goto_headline_from_todo
     59               ~goto_done:goto_done_from_todo
     60           in
     61           Todos.render term todo);
     62     (* INIT HEADLINE VIEW *)
     63     print_endline "HEADLINE INIT";
     64     let headline =
     65       Headlines.init
     66         ~goto_done_view:(fun new_state ->
     67           restore_headline_state := new_state;
     68           !restore_done_state ())
     69         ~goto_todos_view:(fun new_state ->
     70           restore_headline_state := new_state;
     71           !restore_todo_state ())
     72         ~regexp:tag
     73     in
     74     print_endline "Got render without error";
     75     match headline with
     76     | Ok headline -> Headlines.render term headline
     77     | Error msg -> print_endline msg)