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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
use chrono::format::{Item, StrftimeItems};
use chrono::{Local, Locale};
use once_cell::sync::Lazy;

use std::fmt::Debug;
use std::iter::repeat;
use std::time::{Duration, Instant};

use super::parse::Arg;
use super::prefix::Prefix;
use super::unit::Unit;
use super::value::ValueInner as Value;
use crate::errors::*;
use crate::escape::CollectEscaped;

const DEFAULT_STR_MIN_WIDTH: usize = 0;
const DEFAULT_STR_MAX_WIDTH: usize = usize::MAX;
const DEFAULT_STR_ROT_INTERVAL: Option<f64> = None;

const DEFAULT_BAR_WIDTH: usize = 5;
const DEFAULT_BAR_MAX_VAL: f64 = 100.0;

const DEFAULT_NUMBER_WIDTH: usize = 2;
const DEFAULT_NUMBER_PAD_WITH: char = ' ';

const DEFAULT_DATETIME_FORMAT: &str = "%a %d/%m %R";

pub const DEFAULT_STRING_FORMATTER: StrFormatter = StrFormatter {
    min_width: DEFAULT_STR_MIN_WIDTH,
    max_width: DEFAULT_STR_MAX_WIDTH,
    rot_interval_ms: None,
    init_time: None,
};

// TODO: split those defaults
pub const DEFAULT_NUMBER_FORMATTER: EngFormatter = EngFormatter(EngFixConfig {
    width: DEFAULT_NUMBER_WIDTH,
    unit: None,
    unit_has_space: false,
    unit_hidden: false,
    prefix: None,
    prefix_has_space: false,
    prefix_hidden: false,
    prefix_forced: false,
    pad_with: DEFAULT_NUMBER_PAD_WITH,
});

pub static DEFAULT_DATETIME_FORMATTER: Lazy<DatetimeFormatter> =
    Lazy::new(|| DatetimeFormatter::new(DEFAULT_DATETIME_FORMAT, None).unwrap());

pub const DEFAULT_FLAG_FORMATTER: FlagFormatter = FlagFormatter;

pub trait Formatter: Debug + Send + Sync {
    fn format(&self, val: &Value) -> Result<String>;

    fn interval(&self) -> Option<Duration> {
        None
    }
}

pub fn new_formatter(name: &str, args: &[Arg]) -> Result<Box<dyn Formatter>> {
    match name {
        "str" => {
            let mut min_width = DEFAULT_STR_MIN_WIDTH;
            let mut max_width = DEFAULT_STR_MAX_WIDTH;
            let mut rot_interval = DEFAULT_STR_ROT_INTERVAL;
            for arg in args {
                match arg.key {
                    "min_width" | "min_w" => {
                        min_width = arg.val.parse().error("Width must be a positive integer")?;
                    }
                    "max_width" | "max_w" => {
                        max_width = arg.val.parse().error("Width must be a positive integer")?;
                    }
                    "width" | "w" => {
                        min_width = arg.val.parse().error("Width must be a positive integer")?;
                        max_width = min_width;
                    }
                    "rot_interval" => {
                        rot_interval = Some(
                            arg.val
                                .parse()
                                .error("Interval must be a positive number")?,
                        );
                    }
                    other => {
                        return Err(Error::new(format!("Unknown argument for 'str': '{other}'")));
                    }
                }
            }
            if max_width < min_width {
                return Err(Error::new(
                    "Max width must be greater of equal to min width",
                ));
            }
            if let Some(rot_interval) = rot_interval {
                if rot_interval < 0.1 {
                    return Err(Error::new("Interval must be greater than 0.1"));
                }
            }
            Ok(Box::new(StrFormatter {
                min_width,
                max_width,
                rot_interval_ms: rot_interval.map(|x| (x * 1e3) as u64),
                init_time: Some(Instant::now()),
            }))
        }
        "pango-str" => {
            #[allow(clippy::never_loop)]
            for arg in args {
                return Err(Error::new(format!(
                    "Unknown argument for 'pango-str': '{}'",
                    arg.key
                )));
            }
            Ok(Box::new(PangoStrFormatter))
        }
        "bar" => {
            let mut width = DEFAULT_BAR_WIDTH;
            let mut max_value = DEFAULT_BAR_MAX_VAL;
            for arg in args {
                match arg.key {
                    "width" | "w" => {
                        width = arg.val.parse().error("Width must be a positive integer")?;
                    }
                    "max_value" => {
                        max_value = arg.val.parse().error("Max value must be a number")?;
                    }
                    other => {
                        return Err(Error::new(format!("Unknown argument for 'bar': '{other}'")));
                    }
                }
            }
            Ok(Box::new(BarFormatter { width, max_value }))
        }
        "eng" => Ok(Box::new(EngFormatter(EngFixConfig::from_args(args)?))),
        "fix" => Ok(Box::new(FixFormatter(EngFixConfig::from_args(args)?))),
        "datetime" => {
            let mut format = None;
            let mut locale = None;
            for arg in args {
                match arg.key {
                    "format" | "f" => {
                        format = Some(arg.val);
                    }
                    "locale" | "l" => {
                        locale = Some(arg.val);
                    }
                    other => {
                        return Err(Error::new(format!(
                            "Unknown argument for 'datetime': '{other}'"
                        )));
                    }
                }
            }

            Ok(Box::new(DatetimeFormatter::new(
                format.unwrap_or(DEFAULT_DATETIME_FORMAT),
                locale,
            )?))
        }
        _ => Err(Error::new(format!("Unknown formatter: '{name}'"))),
    }
}

#[derive(Debug)]
pub struct StrFormatter {
    min_width: usize,
    max_width: usize,
    rot_interval_ms: Option<u64>,
    init_time: Option<Instant>,
}

impl Formatter for StrFormatter {
    fn format(&self, val: &Value) -> Result<String> {
        match val {
            Value::Text(text) => {
                let width = text.chars().count();
                Ok(match (self.rot_interval_ms, self.init_time) {
                    (Some(rot_interval_ms), Some(init_time)) if width > self.max_width => {
                        let width = width + 1; // Now we include '|' at the end
                        let step = (init_time.elapsed().as_millis() as u64 / rot_interval_ms)
                            as usize
                            % width;
                        let w1 = self.max_width.min(width - step);
                        text.chars()
                            .chain(Some('|'))
                            .skip(step)
                            .take(w1)
                            .chain(text.chars())
                            .take(self.max_width)
                            .collect_pango_escaped()
                    }
                    _ => text
                        .chars()
                        .chain(repeat(' ').take(self.min_width.saturating_sub(width)))
                        .take(self.max_width)
                        .collect_pango_escaped(),
                })
            }
            Value::Icon(icon) => Ok(icon.clone()), // No escaping
            other => Err(Error::new_format(format!(
                "{} cannot be formatted with 'str' formatter",
                other.type_name(),
            ))),
        }
    }

    fn interval(&self) -> Option<Duration> {
        self.rot_interval_ms.map(Duration::from_millis)
    }
}

#[derive(Debug)]
pub struct PangoStrFormatter;

impl Formatter for PangoStrFormatter {
    fn format(&self, val: &Value) -> Result<String> {
        match val {
            Value::Text(x) | Value::Icon(x) => Ok(x.clone()), // No escaping
            other => Err(Error::new_format(format!(
                "{} cannot be formatted with 'str' formatter",
                other.type_name(),
            ))),
        }
    }
}

#[derive(Debug)]
pub struct BarFormatter {
    width: usize,
    max_value: f64,
}

const VERTICAL_BAR_CHARS: [char; 9] = [
    ' ', '\u{258f}', '\u{258e}', '\u{258d}', '\u{258c}', '\u{258b}', '\u{258a}', '\u{2589}',
    '\u{2588}',
];

impl Formatter for BarFormatter {
    fn format(&self, val: &Value) -> Result<String> {
        match val {
            Value::Number { mut val, .. } => {
                val = (val / self.max_value).clamp(0., 1.);
                let chars_to_fill = val * self.width as f64;
                Ok((0..self.width)
                    .map(|i| {
                        VERTICAL_BAR_CHARS[((chars_to_fill - i as f64).clamp(0., 1.) * 8.) as usize]
                    })
                    .collect())
            }
            other => Err(Error::new_format(format!(
                "{} cannot be formatted with 'bar' formatter",
                other.type_name(),
            ))),
        }
    }
}

#[derive(Debug)]
struct EngFixConfig {
    width: usize,
    unit: Option<Unit>,
    unit_has_space: bool,
    unit_hidden: bool,
    prefix: Option<Prefix>,
    prefix_has_space: bool,
    prefix_hidden: bool,
    prefix_forced: bool,
    pad_with: char,
}

impl EngFixConfig {
    fn from_args(args: &[Arg]) -> Result<Self> {
        let mut width = DEFAULT_NUMBER_WIDTH;
        let mut unit = None;
        let mut unit_has_space = false;
        let mut unit_hidden = false;
        let mut prefix = None;
        let mut prefix_has_space = false;
        let mut prefix_hidden = false;
        let mut prefix_forced = false;
        let mut pad_with = DEFAULT_NUMBER_PAD_WITH;

        for arg in args {
            match arg.key {
                "width" | "w" => {
                    width = arg.val.parse().error("Width must be a positive integer")?;
                }
                "unit" | "u" => {
                    unit = Some(arg.val.parse()?);
                }
                "hide_unit" => {
                    unit_hidden = arg
                        .val
                        .parse()
                        .ok()
                        .error("hide_unit must be true or false")?;
                }
                "unit_space" => {
                    unit_has_space = arg
                        .val
                        .parse()
                        .ok()
                        .error("unit_space must be true or false")?;
                }
                "prefix" | "p" => {
                    prefix = Some(arg.val.parse()?);
                }
                "hide_prefix" => {
                    prefix_hidden = arg
                        .val
                        .parse()
                        .ok()
                        .error("hide_prefix must be true or false")?;
                }
                "prefix_space" => {
                    prefix_has_space = arg
                        .val
                        .parse()
                        .ok()
                        .error("prefix_space must be true or false")?;
                }
                "force_prefix" => {
                    prefix_forced = arg
                        .val
                        .parse()
                        .ok()
                        .error("force_prefix must be true or false")?;
                }
                "pad_with" => {
                    pad_with = arg
                        .val
                        .parse()
                        .error("pad_with must be a single character")?;
                }
                other => {
                    return Err(Error::new(format!(
                        "Unknown argument for 'fix'/'eng': '{other}'"
                    )));
                }
            }
        }

        Ok(Self {
            width,
            unit,
            unit_has_space,
            unit_hidden,
            prefix,
            prefix_has_space,
            prefix_hidden,
            prefix_forced,
            pad_with,
        })
    }
}

#[derive(Debug)]
pub struct EngFormatter(EngFixConfig);

impl Formatter for EngFormatter {
    fn format(&self, val: &Value) -> Result<String> {
        match val {
            Value::Number { mut val, mut unit } => {
                if let Some(new_unit) = self.0.unit {
                    val = unit.convert(val, new_unit)?;
                    unit = new_unit;
                }

                let (min_prefix, max_prefix) = match (self.0.prefix, self.0.prefix_forced) {
                    (Some(prefix), true) => (prefix, prefix),
                    (Some(prefix), false) => (prefix, Prefix::max_available()),
                    (None, _) => (Prefix::min_available(), Prefix::max_available()),
                };

                let prefix = unit
                    .clamp_prefix(if min_prefix.is_binary() {
                        Prefix::eng_binary(val)
                    } else {
                        Prefix::eng(val)
                    })
                    .clamp(min_prefix, max_prefix);
                val = prefix.apply(val);

                let mut digits = (val.max(1.).log10().floor() + 1.0) as isize;
                if val < 0. {
                    digits += 1;
                }

                let mut retval = match self.0.width as isize - digits {
                    isize::MIN..=0 => format!("{}", val.floor()),
                    1 => format!("{}{}", self.0.pad_with, val.floor() as i64),
                    rest => format!("{:.*}", rest as usize - 1, val),
                };

                let display_prefix = !self.0.prefix_hidden
                    && prefix != Prefix::One
                    && prefix != Prefix::OneButBinary;
                let display_unit = !self.0.unit_hidden && unit != Unit::None;

                if display_prefix {
                    if self.0.prefix_has_space {
                        retval.push(' ');
                    }
                    retval.push_str(&prefix.to_string());
                }
                if display_unit {
                    if self.0.unit_has_space || (self.0.prefix_has_space && !display_prefix) {
                        retval.push(' ');
                    }
                    retval.push_str(&unit.to_string());
                }

                Ok(retval)
            }
            other => Err(Error::new_format(format!(
                "{} cannot be formatted with 'eng' formatter",
                other.type_name(),
            ))),
        }
    }
}

#[derive(Debug)]
pub struct FixFormatter(EngFixConfig);

impl Formatter for FixFormatter {
    fn format(&self, val: &Value) -> Result<String> {
        match val {
            Value::Number {
                ..
                // mut val,
                // unit,
                // icon,
            } => Err(Error::new_format("'fix' formatter is not implemented yet")),
            other => Err(Error::new_format(format!(
                "{} cannot be formatted with 'fix' formatter",
                other.type_name(),
            )))
        }
    }
}

#[derive(Debug)]
pub struct DatetimeFormatter {
    items: Vec<Item<'static>>,
    locale: Option<Locale>,
}

fn make_static_item(item: Item<'_>) -> Item<'static> {
    match item {
        Item::Literal(str) => Item::OwnedLiteral(str.into()),
        Item::OwnedLiteral(boxed) => Item::OwnedLiteral(boxed),
        Item::Space(str) => Item::OwnedSpace(str.into()),
        Item::OwnedSpace(boxed) => Item::OwnedSpace(boxed),
        Item::Numeric(numeric, pad) => Item::Numeric(numeric, pad),
        Item::Fixed(fixed) => Item::Fixed(fixed),
        Item::Error => Item::Error,
    }
}

impl DatetimeFormatter {
    fn new(format: &str, locale: Option<&str>) -> Result<Self> {
        let (items, locale) = match locale {
            Some(locale) => {
                let locale = locale.try_into().ok().error("invalid locale")?;
                (StrftimeItems::new_with_locale(format, locale), Some(locale))
            }
            None => (StrftimeItems::new(format), None),
        };

        Ok(Self {
            items: items.map(make_static_item).collect(),
            locale,
        })
    }
}

impl Formatter for DatetimeFormatter {
    fn format(&self, val: &Value) -> Result<String> {
        match val {
            Value::Datetime(datetime, timezone) => Ok(match self.locale {
                Some(locale) => match timezone {
                    Some(tz) => datetime
                        .with_timezone(tz)
                        .format_localized_with_items(self.items.iter(), locale),
                    None => datetime
                        .with_timezone(&Local)
                        .format_localized_with_items(self.items.iter(), locale),
                },
                None => match timezone {
                    Some(tz) => datetime
                        .with_timezone(tz)
                        .format_with_items(self.items.iter()),
                    None => datetime
                        .with_timezone(&Local)
                        .format_with_items(self.items.iter()),
                },
            }
            .to_string()),
            other => Err(Error::new_format(format!(
                "{} cannot be formatted with 'datetime' formatter",
                other.type_name(),
            ))),
        }
    }
}

#[derive(Debug)]
pub struct FlagFormatter;

impl Formatter for FlagFormatter {
    fn format(&self, val: &Value) -> Result<String> {
        match val {
            Value::Flag => Ok(String::new()),
            _ => {
                unreachable!()
            }
        }
    }
}