Commit
Author: Kevin Schoon [kevinschoon@gmail.com]
Hash: 366c67311b482191c00f4ce3c80e62278c1c3c72
Timestamp: Wed, 03 Feb 2021 15:45:25 +0000 (3 years ago)

+90 -40 +/-2 browse
refactor Display module for colorized tags
1diff --git a/lib/note.ml b/lib/note.ml
2index 4c96074..6a17476 100644
3--- a/lib/note.ml
4+++ b/lib/note.ml
5 @@ -203,22 +203,35 @@ module Filter = struct
6 end
7
8 module Display = struct
9- (* TODO: Colourized tags *)
10-
11 open ANSITerminal
12
13- type cell = string * ANSITerminal.style list
14+ type cell = string * int * int
15
16 type row = cell list
17
18+ type pair = string * color
19+
20+ let paint_tag pairs text =
21+ let color =
22+ match List.find ~f:(fun entry -> String.equal (fst entry) text) pairs with
23+ | Some color_text ->
24+ snd color_text
25+ | None -> Foreground Default
26+ in
27+ sprintf [ color ] "%s" text
28+
29 let to_cells columns notes =
30 let header =
31 List.map
32 ~f:(fun column ->
33- (Config.Column.to_string column, [ Bold; Underlined ]))
34+ let text_value = Config.Column.to_string column in
35+ let text_length = String.length text_value in
36+ let text_value = sprintf [ Bold; Underlined ] "%s" text_value in
37+ (text_value, text_length, 1))
38 columns
39 in
40 let note_cells =
41+ let default_padding = 1 in
42 List.fold ~init:[]
43 ~f:(fun accm note ->
44 accm
45 @@ -226,14 +239,33 @@ module Display = struct
46 List.map
47 ~f:(fun column ->
48 match column with
49- | `Title -> (get_title note, [ Reset ])
50- | `Description -> (get_description note, [ Reset ])
51- | `Tags -> (String.concat ~sep:"|" (get_tags note), [ Reset ])
52+ | `Title ->
53+ let text_value = get_title note in
54+ (text_value, String.length text_value, default_padding)
55+ | `Description ->
56+ let text_value = get_description note in
57+ (text_value, String.length text_value, default_padding)
58+ (* TODO: Colourized tags *)
59+ | `Tags ->
60+ let text_value = String.concat ~sep:"|" (get_tags note) in
61+ let text_length = String.length text_value in
62+ let tags = get_tags note in
63+ let tags =
64+ List.map
65+ ~f:(fun tag -> paint_tag [ ] tag)
66+ tags
67+ in
68+ let text_value = String.concat ~sep:"|" tags in
69+ (text_value, text_length, default_padding)
70 | `WordCount ->
71- ( Core.sprintf "%d"
72- (List.length (Util.to_words note.markdown)),
73- [ Reset ] )
74- | `Slug -> (Slug.to_string note.slug, [ Reset ]))
75+ let text_value =
76+ Core.sprintf "%d"
77+ (List.length (Util.to_words note.markdown))
78+ in
79+ (text_value, String.length text_value, default_padding)
80+ | `Slug ->
81+ let text_value = Slug.to_string note.slug in
82+ (text_value, String.length text_value, default_padding))
83 columns;
84 ])
85 notes
86 @@ -241,38 +273,52 @@ module Display = struct
87 [ header ] @ note_cells
88
89 let fixed_spacing cells =
90- (* find the maximum column length for all cells *)
91- List.fold ~init:[]
92- ~f:(fun accm row ->
93- List.mapi
94- ~f:(fun i col ->
95- let col_length = String.length (fst col) in
96- let current_max =
97- match List.nth accm i with Some len -> len | None -> 0
98- in
99- if col_length > current_max then col_length+2 else current_max)
100- row)
101- cells
102+ (* find the maximum cell length per column *)
103+ let maximum_values =
104+ List.fold ~init:[]
105+ ~f:(fun accm row ->
106+ List.mapi
107+ ~f:(fun i col ->
108+ let col_length = snd3 col in
109+ let current_max =
110+ match List.nth accm i with Some len -> len | None -> 0
111+ in
112+ if col_length > current_max then col_length + 2 else current_max)
113+ row)
114+ cells
115+ in
116+ maximum_values
117
118- let fix_right widths =
119+ let fix_right cells =
120+ let widths = fixed_spacing cells in
121 let term_width, _ = size () in
122 let _, right = List.split_n widths 1 in
123 let col_one = List.nth_exn widths 0 in
124- [ col_one + (term_width - List.fold ~init:0 ~f:( + ) widths) ] @ right
125+ [ col_one + (term_width - List.fold ~init:5 ~f:( + ) widths) ] @ right
126
127- let apply widths cells =
128+ let apply cells widths =
129+ (* let maximums = fixed_spacing cells in *)
130+ let cells =
131+ List.map
132+ ~f:(fun row ->
133+ List.mapi
134+ ~f:(fun i entry ->
135+ let max = List.nth_exn widths i in
136+ let text, length, padding = entry in
137+ let padding = padding + (max - length) in
138+ let padding = if padding > 0 then padding else 0 in
139+ (text, length, padding))
140+ row)
141+ cells
142+ in
143 List.fold ~init:[]
144 ~f:(fun accm row ->
145 accm
146 @ [
147- List.foldi ~init:""
148- ~f:(fun i accm cell ->
149- let text = fst cell in
150- let styles = snd cell in
151- let cell_width = List.nth_exn widths i in
152- let padding = cell_width - String.length text in
153- String.concat
154- [ accm; sprintf styles "%s" text; String.make padding ' ' ])
155+ List.fold ~init:""
156+ ~f:(fun accm cell ->
157+ let text, _, padding = cell in
158+ String.concat [ accm; text; String.make padding ' ' ])
159 row;
160 ])
161 cells
162 @@ -282,10 +328,12 @@ module Display = struct
163 match style with
164 | `Simple ->
165 List.iter
166- ~f:(fun cell -> print_endline (fst (List.nth_exn cell 0)))
167+ ~f:(fun cell ->
168+ print_endline
169+ (let value = List.nth_exn cell 0 in
170+ let text = fst3 value in
171+ text))
172 cells
173- | `Fixed -> List.iter ~f:print_endline (apply (fixed_spacing cells) cells)
174- | `Wide ->
175- List.iter ~f:print_endline
176- (apply (fix_right (fixed_spacing cells)) cells)
177+ | `Fixed -> List.iter ~f:print_endline (apply cells (fixed_spacing cells))
178+ | `Wide -> List.iter ~f:print_endline (apply cells (fix_right cells))
179 end
180 diff --git a/lib/note.mli b/lib/note.mli
181index fa8d118..85d6f3b 100644
182--- a/lib/note.mli
183+++ b/lib/note.mli
184 @@ -31,9 +31,11 @@ module Filter : sig
185 end
186
187 module Display : sig
188- type cell = string * ANSITerminal.style list
189+ type cell = string * int * int
190
191 type row = cell list
192
193+ type pair = string * ANSITerminal.color
194+
195 val to_stdout : columns: Config.Column.t list -> style:[< `Fixed | `Simple | `Wide ] -> t list -> unit
196 end