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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
module Grep = Grep
module Common = Common
open Notty
module Input_screen = Input_screen
module Stitched_article = Stitched_article
module Help_screen = Help_screen
module Todos = Todos
type state =
{ pos : int * int
; scroll : int
; content : (string * string) array
; content_pretty : string array
; todo_state : Todos.state option
}
(* TODO: Add page title *)
let rec render t ({ pos; scroll; content; content_pretty; todo_state } as state) =
let x, y = pos in
let img =
let dot = I.string A.(st bold) ">" |> I.pad ~l:0 ~t:(y - scroll)
and elements =
Array.mapi
(fun i el ->
if i == y - scroll
then I.strf ~attr:A.(st underline) "%s" el |> I.pad ~l:2 ~t:i
else I.strf "%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
render t @@ { state with pos = x, max (y - 1) 0; scroll }
in
let scroll_down () =
let scroll = if y - scroll >= size_y - 1 then scroll + 1 else scroll in
render t @@ { state with pos = x, min (y + 1) (content_length - 1); 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 _ -> render t state
| `Mouse ((`Press _ | `Drag), (_, y), _) ->
render t { state with pos = 0, min y content_length }
| `Key (`ASCII '?', []) -> Help_screen.render t { go_back = (fun () -> render t state) }
| `Key (`ASCII '2', []) ->
(match todo_state with
| Some todo_state ->
Todos.render
t
{ todo_state with
goto_headlines =
(fun new_state -> render t { state with todo_state = Some new_state })
}
| None ->
let todo_content = Grep.get_todos () |> Grep.parse_todo_string in
let todo_pretty = Grep.pretty_format_todo todo_content in
let todo_state =
Todos.init
~goto_headlines:(fun new_state ->
render t { state with todo_state = Some new_state })
~content:(todo_content |> Array.of_list)
~content_pretty:(todo_pretty |> Array.of_list)
in
Todos.render t todo_state)
| `Key (`ASCII 's', []) ->
let content =
Array.map
(fun (file_name, _) -> Grep.get_full_file_content_content file_name)
content
|> Array.to_list
in
let full_content = Grep.parse_full_content content in
let full_content_pretty =
Grep.pretty_print_parsed_content full_content |> Array.of_list
in
Stitched_article.render
t
{ pos = 0, 0
; content = full_content |> Array.of_list
; content_pretty = full_content_pretty
; scroll = 0
; go_back = (fun () -> render t state)
; todo_state
}
| `Key (`ASCII 'r', []) ->
let (input_state : Input_screen.state) =
{ screen = img
; user_input = ""
; prompt = "REGEXP: "
; on_enter =
(fun tag ->
let content = Grep.get_tagged_headlines tag () |> Grep.parse_headlines in
let content_pretty = Grep.pretty_format content in
Common.Term.cursor t None;
render t { state with content; content_pretty; pos = 0, 0; scroll = 0 })
; on_cancel =
(fun _ ->
Common.Term.cursor t None;
render t state)
}
in
Input_screen.render t input_state
| `Key (`ASCII 'j', []) | `Key (`ASCII 'N', [ `Ctrl ]) -> scroll_down ()
| `Key (`ASCII 'k', []) | `Key (`ASCII 'P', [ `Ctrl ]) -> scroll_up ()
| `Key (`Arrow d, _) ->
(match d with
| `Up -> scroll_up ()
| `Down -> scroll_down ()
| _ -> render t state)
| `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;
render t state
(* Capture resizing events *)
| exception Unix.Unix_error (Unix.EINTR, _, _) -> run_editor ()
| exception Unix.Unix_error (_, _, _) -> failwith "ERROR"
in
run_editor ()
| _ -> render t state
let start (tag : string) () =
let tag = if String.equal tag "" then None else Some tag in
let content =
match tag with
| None -> Grep.get_headlines () |> Grep.parse_headlines
| Some tag -> Grep.get_tagged_headlines tag () |> Grep.parse_headlines
in
if Array.length content == 0
then (
print_endline "No entry for tag";
exit 0)
else (
let content_pretty = content |> Grep.pretty_format in
render
(Common.Term.create ())
{ pos = 0, 0; scroll = 0; content; content_pretty; todo_state = None })
|