Commit
+67 -62 +/-2 browse
1 | diff --git a/lib/note.ml b/lib/note.ml |
2 | index a1bc130..0316ff5 100644 |
3 | --- a/lib/note.ml |
4 | +++ b/lib/note.ml |
5 | @@ -204,75 +204,76 @@ end |
6 | |
7 | module Display = struct |
8 | (* TODO: Colourized tags *) |
9 | + |
10 | open ANSITerminal |
11 | |
12 | type style = Fancy | Simple |
13 | |
14 | - (* three string columns where col 2 and 3 float to the right *) |
15 | - let three_col_string max1 max2 max3 col1 col2 col3 = |
16 | - let (s1, t1), (s2, t2), (s3, t3) = (col1, col2, col3) in |
17 | - let width, _ = size () in |
18 | - (* left side *) |
19 | - let l1 = String.concat [ t1; String.make (max1 - String.length t1) ' ' ] in |
20 | - (* right side *) |
21 | - let r1 = String.concat [ String.make (max2 - String.length t2) ' '; t2 ] in |
22 | - let r2 = String.concat [ String.make (max3 - String.length t3) ' '; t3 ] in |
23 | - let padding = |
24 | - String.make |
25 | - (width - (String.length l1 + String.length r1 + String.length r2 + 2)) |
26 | - ' ' |
27 | - in |
28 | - String.concat |
29 | + type cell = string * ANSITerminal.style list |
30 | + |
31 | + type row = cell list |
32 | + |
33 | + let to_cells notes = |
34 | + [ |
35 | [ |
36 | - sprintf s1 "%s" l1; |
37 | - padding; |
38 | - sprintf s2 "%s" r1; |
39 | - " "; |
40 | - sprintf s3 "%s" r2; |
41 | - ] |
42 | + ("title", [ Bold; Underlined ]); |
43 | + ("tags", [ Bold; Underlined ]); |
44 | + ("words", [ Bold; Underlined ]); |
45 | + ]; |
46 | + ] |
47 | + @ List.fold ~init:[] |
48 | + ~f:(fun accm note -> |
49 | + let title = (get_title note, [ Reset ]) in |
50 | + let tags = (String.concat ~sep:"|" (get_tags note), [ Reset ]) in |
51 | + let word_count = |
52 | + (Core.sprintf "%d" (List.length (tokenize note)), [ Reset ]) |
53 | + in |
54 | + accm @ [ [ title; tags; word_count ] ]) |
55 | + notes |
56 | + |
57 | + let fixed_spacing cells = |
58 | + (* find the maximum column length for all cells *) |
59 | + List.fold ~init:[] |
60 | + ~f:(fun accm row -> |
61 | + List.mapi |
62 | + ~f:(fun i col -> |
63 | + let col_length = String.length (fst col) in |
64 | + let current_max = |
65 | + match List.nth accm i with Some len -> len | None -> 0 |
66 | + in |
67 | + if col_length > current_max then col_length + 1 else current_max) |
68 | + row) |
69 | + cells |
70 | + |
71 | + let fix_right widths = |
72 | + let term_width, _ = size () in |
73 | + let _, right = List.split_n widths 1 in |
74 | + let col_one = List.nth_exn widths 0 in |
75 | + [ col_one + (term_width - List.fold ~init:0 ~f:( + ) widths) ] @ right |
76 | + |
77 | + let apply widths cells = |
78 | + List.fold ~init:[] |
79 | + ~f:(fun accm row -> |
80 | + accm |
81 | + @ [ |
82 | + List.foldi ~init:"" |
83 | + ~f:(fun i accm cell -> |
84 | + let text = fst cell in |
85 | + let styles = snd cell in |
86 | + let cell_width = List.nth_exn widths i in |
87 | + let padding = cell_width - String.length text in |
88 | + String.concat |
89 | + [ accm; sprintf styles "%s%s" text (String.make padding ' ') ]) |
90 | + row; |
91 | + ]) |
92 | + cells |
93 | |
94 | let print_short ~style notes = |
95 | - let columns = |
96 | - List.map |
97 | - ~f:(fun note -> |
98 | - let title = get_title note in |
99 | - let tags = String.concat ~sep:"|" (get_tags note) in |
100 | - let word_count = Core.sprintf "%d" (List.length (tokenize note)) in |
101 | - (([], title), ([], tags), ([], word_count))) |
102 | - notes |
103 | - in |
104 | match style with |
105 | - | Simple -> |
106 | - List.iter |
107 | - ~f:(fun (col1, col2, col3) -> |
108 | - let (_, text1), (_, _), (_, _) = (col1, col2, col3) in |
109 | - print_endline (Core.sprintf "%s" text1)) |
110 | - columns |
111 | + | Simple -> List.iter ~f:(fun note -> print_endline (get_title note)) notes |
112 | | Fancy -> |
113 | - let columns = |
114 | - [ (([ Bold ], "title"), ([ Bold ], "tags"), ([ Bold ], "words")) ] |
115 | - @ columns |
116 | - in |
117 | - let max1, max2, max3 = |
118 | - List.fold ~init:(0, 0, 0) |
119 | - ~f:(fun accm pair -> |
120 | - let (_, text1), (_, text2), (_, text3) = pair in |
121 | - let col1, col2, col3 = |
122 | - (String.length text1, String.length text2, String.length text3) |
123 | - in |
124 | - let max1, max2, max3 = accm in |
125 | - let max1 = if col1 > max1 then col1 else max1 in |
126 | - let max2 = if col2 > max2 then col2 else max2 in |
127 | - let max3 = if col3 > max3 then col3 else max3 in |
128 | - (max1, max2, max3)) |
129 | - columns |
130 | - in |
131 | - List.iter |
132 | - ~f:(fun pair -> |
133 | - let line = |
134 | - three_col_string max1 max2 max3 (fst3 pair) (snd3 pair) |
135 | - (trd3 pair) |
136 | - in |
137 | - print_endline line) |
138 | - columns |
139 | + let cells = to_cells notes in |
140 | + let widths = fix_right (fixed_spacing cells) in |
141 | + let spaced = apply widths cells in |
142 | + List.iter ~f:(fun spaced_row -> print_endline spaced_row) spaced |
143 | end |
144 | diff --git a/lib/note.mli b/lib/note.mli |
145 | index 66644f2..4700268 100644 |
146 | --- a/lib/note.mli |
147 | +++ b/lib/note.mli |
148 | @@ -62,5 +62,9 @@ end |
149 | module Display : sig |
150 | type style = Fancy | Simple |
151 | |
152 | + type cell = string * ANSITerminal.style list |
153 | + |
154 | + type row = cell list |
155 | + |
156 | val print_short : style:style -> t list -> unit |
157 | end |