Commit
Author: Kevin Schoon [kevinschoon@gmail.com]
Hash: 33d4e60ab08549baf1bef07590e012f86fa0d810
Timestamp: Sat, 06 Feb 2021 04:32:26 +0000 (3 years ago)

+125 -171 +/-4 browse
move display into a new file
1diff --git a/lib/cmd.ml b/lib/cmd.ml
2index 4c52c94..c2c167c 100644
3--- a/lib/cmd.ml
4+++ b/lib/cmd.ml
5 @@ -190,7 +190,6 @@ Select a note that matches the filter criteria and open it in your text editor.
6 | None -> failwith "not found"]
7
8 let list_notes =
9- let open Note.Display in
10 let open Command.Let_syntax in
11 Command.basic ~summary:"list existing notes"
12 ~readme:(fun () ->
13 @@ -219,8 +218,10 @@ is provided then all notes will be listed.
14 Note.Filter.find_many ?strategy:filter_kind ~args:filter_args
15 get_notes
16 in
17- let text_styles = cfg.styles in
18- to_stdout ~columns ~style ~text_styles notes]
19+ let styles = cfg.styles in
20+ let cells = Note.to_cells ~columns ~styles notes in
21+ Display.to_stdout ~style cells;
22+ ]
23
24 let sync =
25 Command.basic ~summary:"sync notes to a remote server"
26 diff --git a/lib/display.ml b/lib/display.ml
27new file mode 100644
28index 0000000..4036af7
29--- /dev/null
30+++ b/lib/display.ml
31 @@ -0,0 +1,70 @@
32+ open Core
33+ open ANSITerminal
34+
35+ type cell = string * int * int
36+
37+ type row = cell list
38+
39+ let fixed_spacing cells =
40+ (* find the maximum cell length per column *)
41+ let maximum_values =
42+ List.fold ~init:[]
43+ ~f:(fun accm row ->
44+ List.mapi
45+ ~f:(fun i col ->
46+ let col_length = snd3 col in
47+ let current_max =
48+ match List.nth accm i with Some len -> len | None -> 0
49+ in
50+ if col_length > current_max then col_length + 2 else current_max)
51+ row)
52+ cells
53+ in
54+ maximum_values
55+
56+ let fix_right cells =
57+ let widths = fixed_spacing cells in
58+ let term_width, _ = size () in
59+ let _, right = List.split_n widths 1 in
60+ let col_one = List.nth_exn widths 0 in
61+ [ col_one + (term_width - List.fold ~init:5 ~f:( + ) widths) ] @ right
62+
63+ let apply cells widths =
64+ (* let maximums = fixed_spacing cells in *)
65+ let cells =
66+ List.map
67+ ~f:(fun row ->
68+ List.mapi
69+ ~f:(fun i entry ->
70+ let max = List.nth_exn widths i in
71+ let text, length, padding = entry in
72+ let padding = padding + (max - length) in
73+ let padding = if padding > 0 then padding else 0 in
74+ (text, length, padding))
75+ row)
76+ cells
77+ in
78+ List.fold ~init:[]
79+ ~f:(fun accm row ->
80+ accm
81+ @ [
82+ List.fold ~init:""
83+ ~f:(fun accm cell ->
84+ let text, _, padding = cell in
85+ String.concat [ accm; text; String.make padding ' ' ])
86+ row;
87+ ])
88+ cells
89+
90+ let to_stdout ~style cells =
91+ match style with
92+ | `Simple ->
93+ List.iter
94+ ~f:(fun cell ->
95+ print_endline
96+ (let value = List.nth_exn cell 0 in
97+ let text = fst3 value in
98+ text))
99+ cells
100+ | `Fixed -> List.iter ~f:print_endline (apply cells (fixed_spacing cells))
101+ | `Wide -> List.iter ~f:print_endline (apply cells (fix_right cells))
102 diff --git a/lib/note.ml b/lib/note.ml
103index fe4328c..636a293 100644
104--- a/lib/note.ml
105+++ b/lib/note.ml
106 @@ -22,7 +22,8 @@ let get_title t =
107 in
108 match title with
109 | Some title -> title
110- (* Since we couldn't determine the title from frontmatter now we will infer the title by looking at the markdown *)
111+ (* Since we couldn't determine the title from frontmatter now we will
112+ infer the title by looking at the markdown *)
113 | None -> (
114 let title =
115 List.find
116 @@ -66,7 +67,6 @@ let get_data t =
117 | "JSON" | "Json" | "json" ->
118 Some (Ezjsonm.value (Ezjsonm.from_string data))
119 | "YAML" | "Yaml" | "yaml" -> Some (Yaml.of_string_exn data)
120- (* TODO Sexp, ...?? *)
121 | _ -> None )
122 | _ -> None)
123 t.markdown
124 @@ -202,131 +202,58 @@ module Filter = struct
125 notes
126 end
127
128- module Display = struct
129- open ANSITerminal
130+ open ANSITerminal
131
132- type cell = string * int * int
133+ let paint_tag (styles : Config.StylePair.t list) text : string =
134+ match List.find ~f:(fun entry -> String.equal entry.pattern text) styles with
135+ | Some entry -> sprintf entry.styles "%s" text
136+ | None -> sprintf [ Foreground Default ] "%s" text
137
138- type row = cell list
139-
140- let paint_tag (styles : Config.StylePair.t list) text : string =
141- match
142- List.find ~f:(fun entry -> String.equal entry.pattern text) styles
143- with
144- | Some entry -> sprintf entry.styles "%s" text
145- | None -> sprintf [ Foreground Default ] "%s" text
146-
147- let to_cells columns styles notes =
148- let header =
149- List.map
150- ~f:(fun column ->
151- let text_value = Config.Column.to_string column in
152- let text_length = String.length text_value in
153- let text_value = sprintf [ Bold; Underlined ] "%s" text_value in
154- (text_value, text_length, 1))
155- columns
156- in
157- let note_cells =
158- let default_padding = 1 in
159- List.fold ~init:[]
160- ~f:(fun accm note ->
161- accm
162- @ [
163- List.map
164- ~f:(fun column ->
165- match column with
166- | `Title ->
167- let text_value = get_title note in
168- (text_value, String.length text_value, default_padding)
169- | `Description ->
170- let text_value = get_description note in
171- (text_value, String.length text_value, default_padding)
172- | `Tags ->
173- let text_value = String.concat ~sep:"|" (get_tags note) in
174- let text_length = String.length text_value in
175- let tags = get_tags note in
176- let tags =
177- List.map ~f:(fun tag -> paint_tag styles tag) tags
178- in
179- let text_value = String.concat ~sep:"|" tags in
180- (text_value, text_length, default_padding)
181- | `WordCount ->
182- let text_value =
183- Core.sprintf "%d"
184- (List.length (Util.to_words note.markdown))
185- in
186- (text_value, String.length text_value, default_padding)
187- | `Slug ->
188- let text_value = Slug.to_string note.slug in
189- (text_value, String.length text_value, default_padding))
190- columns;
191- ])
192- notes
193- in
194- [ header ] @ note_cells
195-
196- let fixed_spacing cells =
197- (* find the maximum cell length per column *)
198- let maximum_values =
199- List.fold ~init:[]
200- ~f:(fun accm row ->
201- List.mapi
202- ~f:(fun i col ->
203- let col_length = snd3 col in
204- let current_max =
205- match List.nth accm i with Some len -> len | None -> 0
206- in
207- if col_length > current_max then col_length + 2 else current_max)
208- row)
209- cells
210- in
211- maximum_values
212-
213- let fix_right cells =
214- let widths = fixed_spacing cells in
215- let term_width, _ = size () in
216- let _, right = List.split_n widths 1 in
217- let col_one = List.nth_exn widths 0 in
218- [ col_one + (term_width - List.fold ~init:5 ~f:( + ) widths) ] @ right
219-
220- let apply cells widths =
221- (* let maximums = fixed_spacing cells in *)
222- let cells =
223- List.map
224- ~f:(fun row ->
225- List.mapi
226- ~f:(fun i entry ->
227- let max = List.nth_exn widths i in
228- let text, length, padding = entry in
229- let padding = padding + (max - length) in
230- let padding = if padding > 0 then padding else 0 in
231- (text, length, padding))
232- row)
233- cells
234- in
235+ let to_cells ~columns ~styles notes =
236+ let header =
237+ List.map
238+ ~f:(fun column ->
239+ let text_value = Config.Column.to_string column in
240+ let text_length = String.length text_value in
241+ let text_value = sprintf [ Bold; Underlined ] "%s" text_value in
242+ (text_value, text_length, 1))
243+ columns
244+ in
245+ let note_cells =
246+ let default_padding = 1 in
247 List.fold ~init:[]
248- ~f:(fun accm row ->
249+ ~f:(fun accm note ->
250 accm
251 @ [
252- List.fold ~init:""
253- ~f:(fun accm cell ->
254- let text, _, padding = cell in
255- String.concat [ accm; text; String.make padding ' ' ])
256- row;
257+ List.map
258+ ~f:(fun column ->
259+ match column with
260+ | `Title ->
261+ let text_value = get_title note in
262+ (text_value, String.length text_value, default_padding)
263+ | `Description ->
264+ let text_value = get_description note in
265+ (text_value, String.length text_value, default_padding)
266+ | `Tags ->
267+ let text_value = String.concat ~sep:"|" (get_tags note) in
268+ let text_length = String.length text_value in
269+ let tags = get_tags note in
270+ let tags =
271+ List.map ~f:(fun tag -> paint_tag styles tag) tags
272+ in
273+ let text_value = String.concat ~sep:"|" tags in
274+ (text_value, text_length, default_padding)
275+ | `WordCount ->
276+ let text_value =
277+ Core.sprintf "%d"
278+ (List.length (Util.to_words note.markdown))
279+ in
280+ (text_value, String.length text_value, default_padding)
281+ | `Slug ->
282+ let text_value = Slug.to_string note.slug in
283+ (text_value, String.length text_value, default_padding))
284+ columns;
285 ])
286- cells
287-
288- let to_stdout ~columns ~style ~text_styles notes =
289- let cells = to_cells columns text_styles notes in
290- match style with
291- | `Simple ->
292- List.iter
293- ~f:(fun cell ->
294- print_endline
295- (let value = List.nth_exn cell 0 in
296- let text = fst3 value in
297- text))
298- cells
299- | `Fixed -> List.iter ~f:print_endline (apply cells (fixed_spacing cells))
300- | `Wide -> List.iter ~f:print_endline (apply cells (fix_right cells))
301- end
302+ notes
303+ in
304+ [ header ] @ note_cells
305 diff --git a/lib/note.mli b/lib/note.mli
306deleted file mode 100644
307index b5a17e1..0000000
308--- a/lib/note.mli
309+++ /dev/null
310 @@ -1,44 +0,0 @@
311- type t
312-
313- val build : ?tags:string list -> ?content:string -> title:string -> Slug.t -> t
314- (** build a new note *)
315-
316- val get_title : t -> string
317- (** access the title of a note *)
318-
319- val get_description : t -> string
320- (** access the description of a note *)
321-
322- val get_path : t -> string
323- (** access the absolute path of a note *)
324-
325- val to_string : t -> string
326- (** convert a note into a string *)
327-
328- val of_string : data:string -> Slug.t -> t
329- (** decode a note from a string *)
330-
331- module Encoding : sig
332- val to_string : style:[< `Raw | `Json | `Yaml ] -> t -> string
333- end
334-
335- module Filter : sig
336- type strategy = Keys | Fulltext
337-
338- val find_one : ?strategy:strategy -> args:string list -> t list -> t option
339-
340- val find_many : ?strategy:strategy -> args:string list -> t list -> t list
341- end
342-
343- module Display : sig
344- type cell = string * int * int
345-
346- type row = cell list
347-
348- val to_stdout :
349- columns:Config.Column.t list ->
350- style:Config.ListStyle.t ->
351- text_styles:Config.StylePair.t list ->
352- t list ->
353- unit
354- end