Commit
Author: Kevin Schoon [kevinschoon@gmail.com]
Hash: 88847b22ea35b2355c58be1e8e8ca0f9005cc80f
Timestamp: Thu, 17 Jun 2021 19:02:25 +0000 (3 years ago)

+118 -118 +/-3 browse
refactor display output
1diff --git a/bin/note.ml b/bin/note.ml
2index adbc0cf..01f0a50 100644
3--- a/bin/note.ml
4+++ b/bin/note.ml
5 @@ -238,21 +238,14 @@ is provided then all notes will be listed.
6 ~doc:"columns to include in output"
7 in
8 fun () ->
9- let notes = cfg.state_dir |> Note.load ~context |> Note.to_list in
10+ let notes = cfg.state_dir |> Note.load ~context in
11 let styles = cfg.styles in
12- let cells = notes |> Display.to_cells ~paint:true ~columns ~styles in
13- cells |> Display.Tabular.to_string ~style |> print_endline]
14+ notes |> Display.to_string ~style ~columns ~styles |> print_endline]
15
16 let sync =
17 Command.basic ~summary:"sync notes to a remote server"
18 (Command.Param.return (fun () -> Sync.sync cfg.on_sync))
19
20- let tree =
21- Command.basic ~summary:"tree debug command"
22- (Command.Param.return (fun () ->
23- cfg.state_dir |> Note.load ~context |> convert_tree
24- |> Display.Hierarchical.to_string |> print_endline))
25-
26 let version =
27 match Build_info.V1.version () with
28 | None -> "n/a"
29 @@ -272,5 +265,4 @@ let run =
30 ("edit", edit_note);
31 ("ls", list_notes);
32 ("sync", sync);
33- ("tree", tree);
34 ])
35 diff --git a/lib/display.ml b/lib/display.ml
36index 03d63a1..9360d9d 100644
37--- a/lib/display.ml
38+++ b/lib/display.ml
39 @@ -5,84 +5,76 @@ type cell = string * int * int
40
41 and row = cell list
42
43- let rec to_words (accm : string list) (doc : Omd.doc) : string list =
44- let split_words inline =
45- match inline with Omd.Text text -> String.split ~on:' ' text | _ -> []
46- in
47- match doc with
48- | [] -> accm
49- | hd :: tl -> (
50- (* TODO: account for headings, lists, etc *)
51- match hd.bl_desc with
52- | Paragraph inline ->
53- let accm = accm @ split_words inline.il_desc in
54- to_words accm tl
55- | _ -> to_words accm tl)
56-
57- let paint_tag (styles : Config.StylePair.t list) text : string =
58- match List.find ~f:(fun entry -> String.equal entry.pattern text) styles with
59- | Some entry -> sprintf entry.styles "%s" text
60- | None -> sprintf [ Foreground Default ] "%s" text
61-
62- let to_cells ?(paint = false) ~columns ~styles (notes : Note.note list) =
63- let header =
64- List.map
65- ~f:(fun column ->
66- let text_value = Config.Column.to_string column in
67- let text_length = String.length text_value in
68- let text_value =
69- if paint then sprintf [ Bold; Underlined ] "%s" text_value
70- else text_value
71- in
72- (text_value, text_length, 1))
73- columns
74- in
75- let note_cells =
76- let default_padding = 1 in
77- List.fold ~init:[]
78- ~f:(fun accm note ->
79- accm
80- @ [
81- List.map
82- ~f:(fun column ->
83- match column with
84- | `Title ->
85- let text_value = note.frontmatter.title in
86- (text_value, String.length text_value, default_padding)
87- | `Description ->
88- let text_value = note.frontmatter.description in
89- (text_value, String.length text_value, default_padding)
90- | `Slug ->
91- let text_value =
92- match note.slug with
93- | Some slug -> slug |> Slug.shortname
94- | None -> "??"
95- in
96- (text_value, String.length text_value, default_padding)
97- | `Tags ->
98- let text_value =
99- String.concat ~sep:"|" note.frontmatter.tags
100- in
101- let text_length = String.length text_value in
102- let tags = note.frontmatter.tags in
103- let tags =
104- if paint then
105- List.map ~f:(fun tag -> paint_tag styles tag) tags
106- else tags
107- in
108- let text_value = String.concat ~sep:"|" tags in
109- (text_value, text_length, default_padding)
110- | `LineCount ->
111- let count = note.content |> String.split_lines |> List.length in
112- let text_value = count |> Core.sprintf "%d" in
113- (text_value, String.length text_value, default_padding))
114- columns;
115- ])
116- notes
117- in
118- [ header ] @ note_cells
119+ type cells = (string * int * int) list list
120
121 module Tabular = struct
122+ let paint_tag (styles : Config.StylePair.t list) text : string =
123+ match
124+ List.find ~f:(fun entry -> String.equal entry.pattern text) styles
125+ with
126+ | Some entry -> sprintf entry.styles "%s" text
127+ | None -> sprintf [ Foreground Default ] "%s" text
128+
129+ let to_cells ?(paint = false) ~columns ~styles (notes : Note.note list) =
130+ let header =
131+ List.map
132+ ~f:(fun column ->
133+ let text_value = Config.Column.to_string column in
134+ let text_length = String.length text_value in
135+ let text_value =
136+ if paint then sprintf [ Bold; Underlined ] "%s" text_value
137+ else text_value
138+ in
139+ (text_value, text_length, 1))
140+ columns
141+ in
142+ let note_cells =
143+ let default_padding = 1 in
144+ List.fold ~init:[]
145+ ~f:(fun accm note ->
146+ accm
147+ @ [
148+ List.map
149+ ~f:(fun column ->
150+ match column with
151+ | `Title ->
152+ let text_value = note.frontmatter.title in
153+ (text_value, String.length text_value, default_padding)
154+ | `Description ->
155+ let text_value = note.frontmatter.description in
156+ (text_value, String.length text_value, default_padding)
157+ | `Slug ->
158+ let text_value =
159+ match note.slug with
160+ | Some slug -> slug |> Slug.shortname
161+ | None -> "??"
162+ in
163+ (text_value, String.length text_value, default_padding)
164+ | `Tags ->
165+ let text_value =
166+ String.concat ~sep:"|" note.frontmatter.tags
167+ in
168+ let text_length = String.length text_value in
169+ let tags = note.frontmatter.tags in
170+ let tags =
171+ if paint then
172+ List.map ~f:(fun tag -> paint_tag styles tag) tags
173+ else tags
174+ in
175+ let text_value = String.concat ~sep:"|" tags in
176+ (text_value, text_length, default_padding)
177+ | `LineCount ->
178+ let count =
179+ note.content |> String.split_lines |> List.length
180+ in
181+ let text_value = count |> Core.sprintf "%d" in
182+ (text_value, String.length text_value, default_padding))
183+ columns;
184+ ])
185+ notes
186+ in
187+ [ header ] @ note_cells
188+
189 let fixed cells =
190 (* find the maximum cell length per column *)
191 let maximum_values =
192 @@ -134,27 +126,26 @@ module Tabular = struct
193 ])
194 cells
195
196- let to_string ~style (cells : row list) =
197- match style with
198- | `Simple ->
199- let lines =
200- List.slice
201- (cells |> List.map ~f:(fun row -> row |> List.hd_exn |> fst3))
202- 1 0
203- |> String.concat ~sep:"\n"
204- in
205- "\n" ^ lines ^ "\n"
206- | `Fixed ->
207- let lines =
208- apply ~widths:(cells |> fixed) cells |> String.concat ~sep:"\n"
209- in
210- "\n" ^ lines ^ "\n"
211- | `Wide ->
212- let lines =
213- apply ~widths:(cells |> fixed_right) cells |> String.concat ~sep:"\n"
214- in
215- "\n" ^ lines ^ "\n"
216- | `Tree -> failwith "not implemented"
217+ let simple cells =
218+ let lines =
219+ List.slice
220+ (cells |> List.map ~f:(fun row -> row |> List.hd_exn |> fst3))
221+ 1 0
222+ |> String.concat ~sep:"\n"
223+ in
224+ "\n" ^ lines ^ "\n"
225+
226+ let fixed cells =
227+ let lines =
228+ apply ~widths:(cells |> fixed) cells |> String.concat ~sep:"\n"
229+ in
230+ "\n" ^ lines ^ "\n"
231+
232+ let wide cells =
233+ let lines =
234+ apply ~widths:(cells |> fixed_right) cells |> String.concat ~sep:"\n"
235+ in
236+ "\n" ^ lines ^ "\n"
237 end
238
239 module Hierarchical = struct
240 @@ -194,3 +185,22 @@ module Hierarchical = struct
241
242 let to_string tree = "\n" ^ (tree |> to_lines |> String.concat)
243 end
244+
245+ let rec convert_tree tree =
246+ let (Note.Tree (note, others)) = tree in
247+ let title = note.frontmatter.title in
248+ let title = "[" ^ title ^ "]" in
249+ Hierarchical.Tree (title, List.map ~f:convert_tree others)
250+
251+ let to_string ?(style = `Tree) ?(columns = []) ?(styles = []) notes =
252+ match style with
253+ | `Tree -> notes |> convert_tree |> Hierarchical.to_string
254+ | `Simple ->
255+ notes |> Note.flatten ~accm:[] |> Tabular.to_cells ~columns ~styles
256+ |> Tabular.simple
257+ | `Fixed ->
258+ notes |> Note.flatten ~accm:[] |> Tabular.to_cells ~columns ~styles
259+ |> Tabular.fixed
260+ | `Wide ->
261+ notes |> Note.flatten ~accm:[] |> Tabular.to_cells ~columns ~styles
262+ |> Tabular.wide
263 diff --git a/test/display_test.ml b/test/display_test.ml
264index a1165c8..828581b 100644
265--- a/test/display_test.ml
266+++ b/test/display_test.ml
267 @@ -43,26 +43,25 @@ baz
268 qux
269 |} in
270 let result =
271- notes
272- |> to_cells ~columns:[ `Title ] ~styles:[]
273- |> Tabular.to_string ~style:`Simple
274+ notes |> Tabular.to_cells ~columns:[ `Title ] ~styles:[] |> Tabular.simple
275 in
276 Alcotest.(check string) "tabular_simple" expected result
277
278-
279 let test_tabular_display_fixed () =
280 let open Display in
281- let expected = {|
282+ let expected =
283+ {|
284 title description tags
285 fuu fuu note a|b|c
286 bar bar note with a very long description d|e|f
287 baz baz note h|i|j
288 qux qux note k|l|m
289- |} in
290+ |}
291+ in
292 let result =
293 notes
294- |> to_cells ~columns:[ `Title ; `Description ; `Tags] ~styles:[]
295- |> Tabular.to_string ~style:`Fixed
296+ |> Tabular.to_cells ~columns:[ `Title; `Description; `Tags ] ~styles:[]
297+ |> Tabular.fixed
298 in
299 print_endline (String.Hexdump.to_string_hum expected);
300 print_endline (String.Hexdump.to_string_hum result);
301 @@ -95,7 +94,6 @@ let () =
302 Alcotest.test_case "tabular fixed" `Quick test_tabular_display_simple;
303 ] );
304 ( "tabular-fixed",
305- [
306- Alcotest.test_case "tabular fixed" `Quick test_tabular_display_fixed;
307- ] );
308+ [ Alcotest.test_case "tabular fixed" `Quick test_tabular_display_fixed ]
309+ );
310 ]