aboutsummaryrefslogtreecommitdiff
path: root/lib/stitch.ml
blob: 453b7d13a12ac1ec0f8123bb8b689d4ef0a03e6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
module Grep = Grep
module Common = Common
module Todos = Todos
module Headlines = Headlines

let start (tag : string) () =
  (* This is a rather funky state management.
     What we do is store a function for each view that restores it's state. Since the render function is
     void -> void

     This allows us to remember the state of the view and restore it as we travel between different views.

     It does create a cyclical state though.
  *)
  if String.equal String.empty (String.trim Grep.execution_directory)
  then
    print_endline
      "Execution directory is not set. You will need to set environment variable \
       STITCH_DIRECTORY to the absolute path of your notes. For example: \
       STITCH_DIRECTORY='/home/you/notes'."
  else (
    let term = Common.Term.create () in
    let restore_headline_state = ref (fun () -> ()) in
    let restore_done_state = ref (fun () -> ()) in
    let restore_todo_state = ref (fun () -> ()) in
    (* INIT DONE VIEW *)
    print_endline "DONE INIT";
    let goto_todo_from_done new_done_state =
      restore_done_state := new_done_state;
      !restore_todo_state ()
    in
    let goto_headlines_from_done new_done_state =
      restore_done_state := new_done_state;
      !restore_headline_state ()
    in
    (restore_done_state
     := fun () ->
          let done_state =
            Done.init
              ~goto_headlines:goto_headlines_from_done
              ~goto_todo:goto_todo_from_done
          in
          Done.render term done_state);
    (* INIT TODO VIEW *)
    print_endline "TODO INIT";
    let goto_done_from_todo new_todo_state =
      restore_todo_state := new_todo_state;
      !restore_done_state ()
    in
    let goto_headline_from_todo new_todo_state =
      restore_todo_state := new_todo_state;
      !restore_headline_state ()
    in
    (restore_todo_state
     := fun () ->
          let todo =
            Todos.init
              ~goto_headlines:goto_headline_from_todo
              ~goto_done:goto_done_from_todo
          in
          Todos.render term todo);
    (* INIT HEADLINE VIEW *)
    print_endline "HEADLINE INIT";
    let headline =
      Headlines.init
        ~goto_done_view:(fun new_state ->
          restore_headline_state := new_state;
          !restore_done_state ())
        ~goto_todos_view:(fun new_state ->
          restore_headline_state := new_state;
          !restore_todo_state ())
        ~regexp:tag
    in
    print_endline "Got render without error";
    match headline with
    | Ok headline -> Headlines.render term headline
    | Error msg -> print_endline msg)