1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
|
//! Terminal and JSON presentation.
//!
//! Color turns itself off when the output is not a terminal (a pipe, a file)
//! or when `NO_COLOR` is set, following <https://no-color.org>.
use std::io::IsTerminal;
use crate::model::{Conjugation, Entry, Locution, Sense};
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum ColorChoice {
Auto,
Always,
Never,
}
#[derive(Clone, Copy)]
pub struct Style {
color: bool,
pub width: usize,
}
impl Style {
pub fn new(choice: ColorChoice, width: Option<usize>) -> Self {
let color = match choice {
ColorChoice::Always => true,
ColorChoice::Never => false,
ColorChoice::Auto => {
std::env::var_os("NO_COLOR").is_none() && std::io::stdout().is_terminal()
}
};
Style {
color,
width: width.unwrap_or_else(detect_width).clamp(40, 100),
}
}
fn paint(&self, code: &str, s: &str) -> String {
if self.color && !s.is_empty() {
format!("\x1b[{code}m{s}\x1b[0m")
} else {
s.to_string()
}
}
pub fn bold(&self, s: &str) -> String {
self.paint("1", s)
}
pub fn dim(&self, s: &str) -> String {
self.paint("2", s)
}
pub fn italic(&self, s: &str) -> String {
self.paint("3", s)
}
pub fn cyan(&self, s: &str) -> String {
self.paint("36", s)
}
pub fn green(&self, s: &str) -> String {
self.paint("32", s)
}
pub fn red(&self, s: &str) -> String {
self.paint("31", s)
}
pub fn yellow(&self, s: &str) -> String {
self.paint("33", s)
}
pub fn heading(&self, s: &str) -> String {
self.paint("1;36", s)
}
}
fn detect_width() -> usize {
if let Ok(cols) = std::env::var("COLUMNS") {
if let Ok(n) = cols.parse::<usize>() {
return n;
}
}
terminal_size::terminal_size()
.map(|(terminal_size::Width(w), _)| w as usize)
.unwrap_or(80)
}
/// Wraps a paragraph to the given width. `first` indents the first line and
/// `rest` the following ones, so the text stays aligned under the sense
/// number instead of going back to the margin.
pub fn wrap(text: &str, width: usize, first: &str, rest: &str) -> String {
let usable = width.saturating_sub(rest.chars().count()).max(20);
let mut out = String::new();
let mut line = String::new();
let mut line_len = 0usize;
let mut started = false;
for word in text.split_whitespace() {
let wlen = word.chars().count();
if line_len > 0 && line_len + 1 + wlen > usable {
out.push_str(if started { rest } else { first });
out.push_str(&line);
out.push('\n');
started = true;
line.clear();
line_len = 0;
}
if line_len > 0 {
line.push(' ');
line_len += 1;
}
line.push_str(word);
line_len += wlen;
}
if line_len > 0 {
out.push_str(if started { rest } else { first });
out.push_str(&line);
}
out
}
/// The full entry, as shown in the terminal.
pub fn entry(e: &Entry, st: &Style, show_abbr: bool) -> String {
let mut out = String::new();
out.push_str(&st.heading(&e.word));
out.push('\n');
if let Some(ety) = &e.etymology {
out.push_str(&st.dim(&wrap(ety, st.width, "", "")));
out.push('\n');
}
out.push('\n');
for s in &e.senses {
out.push_str(&sense(s, st, show_abbr));
out.push('\n');
}
if !e.locutions.is_empty() {
out.push('\n');
out.push_str(&st.heading("Locuciones y expresiones"));
out.push('\n');
for l in &e.locutions {
out.push_str(&locution(l, st, show_abbr));
}
}
if let Some(c) = &e.conjugation {
out.push('\n');
out.push_str(&conjugation(c, st));
}
out
}
/// A sense: number, definition, examples and synonyms.
pub fn sense(s: &Sense, st: &Style, show_abbr: bool) -> String {
let num = s.number.clone().unwrap_or_default();
let label = if num.is_empty() {
" ".to_string()
} else {
format!("{num}.")
};
let pad = " ".repeat(label.chars().count().max(2) + 1);
let first = format!("{} ", st.yellow(&label));
let mut out = wrap(&superscript_homographs(&s.text), st.width, &first, &pad);
out.push('\n');
for ex in &s.examples {
out.push_str(&st.italic(&st.dim(&wrap(ex, st.width, &pad, &pad))));
out.push('\n');
}
if !s.synonyms.is_empty() {
let joined = superscript_homographs(&s.synonyms.join(", "));
let line = format!("{} {joined}", st.green("Sin.:"));
out.push_str(&wrap(&line, st.width, &pad, &pad));
out.push('\n');
}
if !s.antonyms.is_empty() {
let joined = superscript_homographs(&s.antonyms.join(", "));
let line = format!("{} {joined}", st.red("Ant.:"));
out.push_str(&wrap(&line, st.width, &pad, &pad));
out.push('\n');
}
if show_abbr && !s.grammar.is_empty() {
let expanded: Vec<String> = s
.grammar
.iter()
.map(|a| format!("{} = {}", a.short, a.long))
.collect();
let line = expanded.join("; ");
out.push_str(&st.dim(&wrap(&line, st.width, &pad, &pad)));
out.push('\n');
}
out
}
fn locution(l: &Locution, st: &Style, show_abbr: bool) -> String {
let mut out = format!("\n{}\n", st.bold(&l.phrase));
for s in &l.senses {
out.push_str(&sense(s, st, show_abbr));
}
out
}
/// The conjugation, one mood per block and one tense per paragraph.
///
/// A seven-person grid per tense does not fit an 80-column terminal once
/// compound tenses show up («yo habría hablado»), so the forms flow separated
/// by «·». Each form already carries its pronoun, so no column header is needed.
pub fn conjugation(c: &
|