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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
module Grep = Grep
module Common = Common
open Notty
module Input_prompt = Input_prompt
module Stitched_article = Stitched_article
module Help_screen = Help_screen
type state =
{ pos : int * int
; scroll : int
; content : (string * string) array
; content_pretty : string array
; goto_todos_view : (unit -> unit) -> unit
; goto_done_view : (unit -> unit) -> unit
; output : string option
; tag : string
; hide_file_name : bool
}
let init ~goto_done_view ~goto_todos_view ~regexp =
let content = Grep.get_tagged_headlines regexp () |> Grep.parse_headlines in
if Array.length content == 0
then (
print_endline "Regexp not found";
exit 0)
else (
let content_pretty = content |> Grep.pretty_format in
{ pos = 0, 2
; scroll = 0
; content
; content_pretty
; goto_done_view
; goto_todos_view
; output = None
; tag = regexp
; hide_file_name = false
})
let title ~tag =
match tag with
| "" -> I.strf ~attr:A.(st bold) "%s" "Notes" |> I.pad ~l:0 ~t:0
| a -> I.strf ~attr:A.(st bold) "%s > %s" "Notes" a |> I.pad ~l:0 ~t:0
let refresh regexp =
let content = Grep.get_tagged_headlines regexp () |> Grep.parse_headlines in
content, content |> Grep.pretty_format
let rec render
t
({ pos
; scroll
; content
; content_pretty
; goto_todos_view
; goto_done_view
; tag
; output
; hide_file_name
} as state)
=
let content_start = 2 in
let size_x, size_y = Common.Term.size t in
let x, y = pos in
let content_position = y - content_start in
let img =
let output_info =
match output with
| Some line ->
I.strf "%s%s" (String.escaped line) (String.make size_x ' ')
|> I.pad ~t:(size_y - 1)
| None -> I.empty
in
let dot =
if Array.length content_pretty = 0
then I.empty
else I.string A.(st bold) ">" |> I.pad ~l:0 ~t:(y - scroll)
and elements =
Array.mapi
(fun i el ->
if i + 1 == y - scroll
then I.strf "%s" el |> I.pad ~l:2 ~t:(i + content_start)
else I.strf "%s" el |> I.pad ~l:2 ~t:(i + content_start))
(Basic.array_drop scroll content_pretty)
in
let open I in
Array.fold_left
(fun sum el -> sum </> el)
(title ~tag </> dot </> output_info)
elements
in
Common.Term.image t img;
let content_end = Array.length content_pretty + (content_start - 1) in
let scroll_up ?(amount = 1) () =
let scroll =
if y - content_start - scroll - amount < 0 then max (scroll - amount) 0 else scroll
in
render t { state with pos = x, max (y - amount) content_start; scroll; output = None }
in
let scroll_down ?(amount = 1) () =
let scroll = if y - scroll >= size_y - amount then scroll + amount else scroll in
render t { state with pos = x, min (y + amount) content_end; scroll; output = None }
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_end; output = None }
| `Key (`ASCII '?', []) -> Help_screen.render t { go_back = (fun () -> render t state) }
| `Key (`ASCII '2', []) -> goto_todos_view (fun () -> render t state)
| `Key (`ASCII 'o', []) ->
let (input_state : Input_prompt.state) =
{ screen = img
; user_input_pre = ""
; user_input_post = ""
; prompt = "EXPORT TO"
; on_enter =
(fun path ->
Common.Term.cursor t None;
if String.equal (String.trim path) String.empty |> not
then (
let export = String.concat "\n" (Array.to_list content_pretty) in
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" })
}
in
Input_prompt.render t input_state
| `Key (`ASCII 'h', []) ->
let hide_file_name = not hide_file_name in
let content_pretty = content |> Grep.pretty_format ~hide_file_name in
render t { state with hide_file_name; content_pretty }
| `Key (`ASCII '3', []) -> goto_done_view (fun () -> render t state)
| `Key (`ASCII 'f', []) ->
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, Stitched_article.content_start
; content = full_content |> Array.of_list
; content_pretty = full_content_pretty
; scroll = 0
; tag
; output = None
; go_back = (fun () -> render t state)
; goto_todos_view
; goto_done_view
}
| `Key (`ASCII 's', []) ->
let (input_state : Input_prompt.state) =
{ screen = img
; user_input_post = ""
; user_input_pre = ""
; prompt = "REGEXP"
; on_enter =
(fun tag ->
let content = Grep.get_tagged_headlines tag () |> Grep.parse_headlines in
let content_pretty = Grep.pretty_format ~hide_file_name content in
Common.Term.cursor t None;
render
t
{ state with
content
; content_pretty
; pos = 0, content_start
; scroll = 0
; tag
})
; on_cancel =
(fun _ ->
Common.Term.cursor t None;
render t state)
}
in
Input_prompt.render t input_state
| `Key (`ASCII 'D', [ `Ctrl ]) | `Key (`Page `Down, []) ->
scroll_down ~amount:(size_y / 2) ()
| `Key (`ASCII 'U', [ `Ctrl ]) | `Key (`Page `Up, []) ->
scroll_up ~amount:(size_y / 2) ()
| `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 '!', []) ->
let selected_file, content = Array.get content content_position in
let selected_file = Grep.execution_directory ^ "/" ^ selected_file in
let (input_state : Input_prompt.state) =
{ screen = img
; user_input_post = ""
; user_input_pre = ""
; prompt = "COMMAND"
; on_enter =
(fun command ->
if String.equal (String.trim command) String.empty
then (
Common.Term.cursor t None;
render t state)
else
Arbitrary_command.run
t
~command:(Scanf.unescaped command)
~content
~selected_file
~on_return:(fun result ->
let content =
Grep.get_tagged_headlines tag () |> Grep.parse_headlines
in
let content_pretty = Grep.pretty_format ~hide_file_name content in
Common.Term.cursor t None;
render
t
{ state with
content
; content_pretty
; pos = 0, content_start
; scroll = 0
; output = Some result
}))
; on_cancel =
(fun _ ->
Common.Term.cursor t None;
render t state)
}
in
Input_prompt.render t input_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 content_position in
let full_path_file = Grep.execution_directory ^ "/" ^ selected_file in
let full_args = Array.append (Array.of_list args) [| "+1"; 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;
let content, content_pretty = refresh tag in
render t { state with content; content_pretty }
(* Capture resizing events *)
| exception Unix.Unix_error (Unix.EINTR, _, _) -> run_editor ()
| exception Unix.Unix_error (_, _, _) -> failwith "ERROR"
in
run_editor ()
| _ -> render t state
|