Commit
+106 -113 +/-6 browse
1 | diff --git a/bin/note.ml b/bin/note.ml |
2 | index 9c3dc9b..c91323e 100644 |
3 | --- a/bin/note.ml |
4 | +++ b/bin/note.ml |
5 | @@ -16,7 +16,7 @@ let rec convert_tree tree = |
6 | let (Note.Tree (note, others)) = tree in |
7 | let title = note.frontmatter.title in |
8 | let title = "[" ^ title ^ "]" in |
9 | - Display.Tree.Tree (title, List.map ~f:convert_tree others) |
10 | + Display.Tree (title, List.map ~f:convert_tree others) |
11 | |
12 | let get_notes = |
13 | let notes = cfg.state_dir |> Note.load |> Note.flatten ~accm:[] in |
14 | @@ -234,7 +234,7 @@ is provided then all notes will be listed. |
15 | in |
16 | let styles = cfg.styles in |
17 | let cells = Note.Util.to_cells ~columns ~styles notes in |
18 | - Display.Cell.to_stdout ~style cells] |
19 | + Display.to_stdout ~style cells] |
20 | |
21 | let sync = |
22 | Command.basic ~summary:"sync notes to a remote server" |
23 | @@ -243,7 +243,7 @@ let sync = |
24 | let tree = |
25 | Command.basic ~summary:"tree debug command" |
26 | (Command.Param.return (fun () -> |
27 | - cfg.state_dir |> Note.load |> convert_tree |> Display.Tree.to_string |> print_endline)) |
28 | + cfg.state_dir |> Note.load |> convert_tree |> Display.to_string |> print_endline)) |
29 | |
30 | let version = |
31 | match Build_info.V1.version () with |
32 | diff --git a/lib/config.ml b/lib/config.ml |
33 | index 2c25555..8ab9691 100644 |
34 | --- a/lib/config.ml |
35 | +++ b/lib/config.ml |
36 | @@ -14,19 +14,21 @@ let config_path = |
37 | | None -> Filename.concat base_xdg_config_path "/note/config.yaml" |
38 | |
39 | module ListStyle = struct |
40 | - type t = [ `Fixed | `Wide | `Simple ] |
41 | + type t = [ `Fixed | `Wide | `Simple | `Tree ] |
42 | |
43 | - let all = [ `Fixed; `Wide; `Simple ] |
44 | + let all = [ `Fixed; `Wide; `Simple; `Tree ] |
45 | |
46 | let to_string = function |
47 | | `Fixed -> "fixed" |
48 | | `Wide -> "wide" |
49 | | `Simple -> "simple" |
50 | + | `Tree -> "tree" |
51 | |
52 | let of_string = function |
53 | | "fixed" -> `Fixed |
54 | | "wide" -> `Wide |
55 | | "simple" -> `Simple |
56 | + | "tree" -> `Tree |
57 | | key -> failwith key |
58 | end |
59 | |
60 | diff --git a/lib/display.ml b/lib/display.ml |
61 | index 99d4821..948afb4 100644 |
62 | --- a/lib/display.ml |
63 | +++ b/lib/display.ml |
64 | @@ -1,110 +1,107 @@ |
65 | open Core |
66 | open ANSITerminal |
67 | |
68 | + type tree = Tree of (string * tree list) |
69 | + |
70 | type cell = string * int * int |
71 | |
72 | type row = cell list |
73 | |
74 | - module Tree = struct |
75 | - type t = Tree of (string * t list) |
76 | - |
77 | - let fill ~last ~state = |
78 | - let get_padding = function true -> "│ " | false -> " " in |
79 | - let get_edge = function true -> "└──" | false -> "├──" in |
80 | - match List.length state with |
81 | - | 0 -> [] |
82 | - | 1 -> [ last |> get_edge ] |
83 | - | len -> |
84 | - let state = List.slice state 0 (len - 1) in |
85 | - let padding = List.map ~f:get_padding state in |
86 | - List.append padding [ last |> get_edge ] |
87 | - |
88 | - let rec to_lines ?(state = []) ?(last = false) next = |
89 | - let (Tree next) = next in |
90 | - let title, children = next in |
91 | - match List.length children with |
92 | - | 0 -> |
93 | - (* leaf *) |
94 | - List.append (fill ~last ~state) [ title; "\n" ] |
95 | - | n_children -> |
96 | - (* node *) |
97 | - List.foldi |
98 | - ~init: |
99 | - [ List.append (fill ~last ~state) [ title; "\n" ] |> String.concat ] |
100 | - ~f:(fun i accm node -> |
101 | - let is_last = Int.equal i (n_children - 1) in |
102 | - let state = List.append state [ phys_equal is_last false ] in |
103 | - let lines = to_lines ~state ~last:is_last node in |
104 | - List.append accm lines) |
105 | - children |
106 | + let fill ~last ~state = |
107 | + let get_padding = function true -> "│ " | false -> " " in |
108 | + let get_edge = function true -> "└──" | false -> "├──" in |
109 | + match List.length state with |
110 | + | 0 -> [] |
111 | + | 1 -> [ last |> get_edge ] |
112 | + | len -> |
113 | + let state = List.slice state 0 (len - 1) in |
114 | + let padding = List.map ~f:get_padding state in |
115 | + List.append padding [ last |> get_edge ] |
116 | |
117 | - let to_string t = |
118 | - let result = t |> to_lines |> String.concat in |
119 | - "\n" ^ result |
120 | - end |
121 | - |
122 | - module Cell = struct |
123 | - let fixed_spacing cells = |
124 | - (* find the maximum cell length per column *) |
125 | - let maximum_values = |
126 | - List.fold ~init:[] |
127 | - ~f:(fun accm row -> |
128 | - List.mapi |
129 | - ~f:(fun i col -> |
130 | - let col_length = snd3 col in |
131 | - let current_max = |
132 | - match List.nth accm i with Some len -> len | None -> 0 |
133 | - in |
134 | - if col_length > current_max then col_length + 2 else current_max) |
135 | - row) |
136 | - cells |
137 | - in |
138 | - maximum_values |
139 | + let rec to_lines ?(state = []) ?(last = false) next = |
140 | + let (Tree next) = next in |
141 | + let title, children = next in |
142 | + match List.length children with |
143 | + | 0 -> |
144 | + (* leaf *) |
145 | + List.append (fill ~last ~state) [ title; "\n" ] |
146 | + | n_children -> |
147 | + (* node *) |
148 | + List.foldi |
149 | + ~init: |
150 | + [ List.append (fill ~last ~state) [ title; "\n" ] |> String.concat ] |
151 | + ~f:(fun i accm node -> |
152 | + let is_last = Int.equal i (n_children - 1) in |
153 | + let state = List.append state [ phys_equal is_last false ] in |
154 | + let lines = to_lines ~state ~last:is_last node in |
155 | + List.append accm lines) |
156 | + children |
157 | |
158 | - let fix_right cells = |
159 | - let widths = fixed_spacing cells in |
160 | - let term_width, _ = size () in |
161 | - let _, right = List.split_n widths 1 in |
162 | - let col_one = List.nth_exn widths 0 in |
163 | - [ col_one + (term_width - List.fold ~init:5 ~f:( + ) widths) ] @ right |
164 | + let to_string t = |
165 | + let result = t |> to_lines |> String.concat in |
166 | + "\n" ^ result |
167 | |
168 | - let apply cells widths = |
169 | - (* let maximums = fixed_spacing cells in *) |
170 | - let cells = |
171 | - List.map |
172 | - ~f:(fun row -> |
173 | - List.mapi |
174 | - ~f:(fun i entry -> |
175 | - let max = List.nth_exn widths i in |
176 | - let text, length, padding = entry in |
177 | - let padding = padding + (max - length) in |
178 | - let padding = if padding > 0 then padding else 0 in |
179 | - (text, length, padding)) |
180 | - row) |
181 | - cells |
182 | - in |
183 | + let fixed_spacing cells = |
184 | + (* find the maximum cell length per column *) |
185 | + let maximum_values = |
186 | List.fold ~init:[] |
187 | ~f:(fun accm row -> |
188 | - accm |
189 | - @ [ |
190 | - List.fold ~init:"" |
191 | - ~f:(fun accm cell -> |
192 | - let text, _, padding = cell in |
193 | - String.concat [ accm; text; String.make padding ' ' ]) |
194 | - row; |
195 | - ]) |
196 | + List.mapi |
197 | + ~f:(fun i col -> |
198 | + let col_length = snd3 col in |
199 | + let current_max = |
200 | + match List.nth accm i with Some len -> len | None -> 0 |
201 | + in |
202 | + if col_length > current_max then col_length + 2 else current_max) |
203 | + row) |
204 | cells |
205 | + in |
206 | + maximum_values |
207 | + |
208 | + let fix_right cells = |
209 | + let widths = fixed_spacing cells in |
210 | + let term_width, _ = size () in |
211 | + let _, right = List.split_n widths 1 in |
212 | + let col_one = List.nth_exn widths 0 in |
213 | + [ col_one + (term_width - List.fold ~init:5 ~f:( + ) widths) ] @ right |
214 | |
215 | - let to_stdout ~style cells = |
216 | - match style with |
217 | - | `Simple -> |
218 | - List.iter |
219 | - ~f:(fun cell -> |
220 | - print_endline |
221 | - (let value = List.nth_exn cell 0 in |
222 | - let text = fst3 value in |
223 | - text)) |
224 | - cells |
225 | - | `Fixed -> List.iter ~f:print_endline (apply cells (fixed_spacing cells)) |
226 | - | `Wide -> List.iter ~f:print_endline (apply cells (fix_right cells)) |
227 | - end |
228 | + let apply cells widths = |
229 | + (* let maximums = fixed_spacing cells in *) |
230 | + let cells = |
231 | + List.map |
232 | + ~f:(fun row -> |
233 | + List.mapi |
234 | + ~f:(fun i entry -> |
235 | + let max = List.nth_exn widths i in |
236 | + let text, length, padding = entry in |
237 | + let padding = padding + (max - length) in |
238 | + let padding = if padding > 0 then padding else 0 in |
239 | + (text, length, padding)) |
240 | + row) |
241 | + cells |
242 | + in |
243 | + List.fold ~init:[] |
244 | + ~f:(fun accm row -> |
245 | + accm |
246 | + @ [ |
247 | + List.fold ~init:"" |
248 | + ~f:(fun accm cell -> |
249 | + let text, _, padding = cell in |
250 | + String.concat [ accm; text; String.make padding ' ' ]) |
251 | + row; |
252 | + ]) |
253 | + cells |
254 | + |
255 | + let to_stdout ~style cells = |
256 | + match style with |
257 | + | `Simple -> |
258 | + List.iter |
259 | + ~f:(fun cell -> |
260 | + print_endline |
261 | + (let value = List.nth_exn cell 0 in |
262 | + let text = fst3 value in |
263 | + text)) |
264 | + cells |
265 | + | `Fixed -> List.iter ~f:print_endline (apply cells (fixed_spacing cells)) |
266 | + | `Wide -> List.iter ~f:print_endline (apply cells (fix_right cells)) |
267 | + | `Tree -> failwith "unimplemented" |
268 | diff --git a/lib/note.ml b/lib/note.ml |
269 | index 912e77c..cca5672 100644 |
270 | --- a/lib/note.ml |
271 | +++ b/lib/note.ml |
272 | @@ -267,17 +267,11 @@ let load path = |
273 | | Some root -> notes |> resolve ~root:(Tree (root, [])) |
274 | | None -> notes |> resolve ~root:(Tree (of_string root_template, [])) |
275 | |
276 | + (* fancy output *) |
277 | + |
278 | module Util = struct |
279 | open ANSITerminal |
280 | |
281 | - let split_words str = |
282 | - List.filter_map |
283 | - ~f:(fun x -> |
284 | - match String.strip ~drop:(fun x -> Char.equal x ' ') x with |
285 | - | "" -> None |
286 | - | _ -> Some x) |
287 | - (String.split ~on:' ' str) |
288 | - |
289 | let rec to_words (accm : string list) (doc : Omd.doc) : string list = |
290 | let split_words inline = |
291 | match inline with Omd.Text text -> String.split ~on:' ' text | _ -> [] |
292 | diff --git a/test/display_test.ml b/test/display_test.ml |
293 | index 667a66b..7e980e6 100644 |
294 | --- a/test/display_test.ml |
295 | +++ b/test/display_test.ml |
296 | @@ -1,7 +1,7 @@ |
297 | open Note_lib |
298 | |
299 | let test_tree () = |
300 | - let open Display.Tree in |
301 | + let open Display in |
302 | let expected = {| |
303 | A |
304 | ├──B |
305 | diff --git a/test/note_test.ml b/test/note_test.ml |
306 | index fc612ce..6039b73 100644 |
307 | --- a/test/note_test.ml |
308 | +++ b/test/note_test.ml |
309 | @@ -8,7 +8,7 @@ let rec convert_tree tree = |
310 | let (Note.Tree (note, others)) = tree in |
311 | let title = note.frontmatter.title in |
312 | let title = "[" ^ title ^ "]" in |
313 | - Display.Tree.Tree (title, List.map ~f:convert_tree others) |
314 | + Display.Tree (title, List.map ~f:convert_tree others) |
315 | |
316 | let make_a_note () = |
317 | let note = |
318 | @@ -124,7 +124,7 @@ let insert_at () = |
319 | Alcotest.(check bool) "inserted" true (Option.is_some result) |
320 | |
321 | let test_structure () = |
322 | - let open Display.Tree in |
323 | + let open Display in |
324 | let expected = |
325 | {| |
326 | [root] |
327 | @@ -244,7 +244,7 @@ let test_resolve () = |
328 | in |
329 | let tree_as_string = |
330 | [ n3; n2; n1; n0 ; n4] |> Note.resolve ~root |> convert_tree |
331 | - |> Display.Tree.to_string |
332 | + |> Display.to_string |
333 | in |
334 | Alcotest.(check string) "resolve" expected tree_as_string |
335 |