i3status_rs/formatting/
formatter.rs

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
use unicode_segmentation::UnicodeSegmentation;

use std::time::Duration;
use std::{borrow::Cow, fmt::Debug};

use super::parse::Arg;
use super::value::ValueInner as Value;
use super::FormatError;
use crate::config::SharedConfig;
use crate::errors::*;

// A helper macro for testing formatters
#[cfg(test)]
#[macro_export]
macro_rules! new_fmt {
    ($name:ident) => {{
        new_fmt!($name,)
    }};
    ($name:ident, $($key:ident : $value:tt),* $(,)?) => {
        new_formatter(stringify!($name), &[
            $( Arg { key: stringify!($key), val: stringify!($value) } ),*
        ])
    };
}

mod bar;
pub use bar::BarFormatter;
mod tally;
pub use tally::TallyFormatter;
mod datetime;
pub use datetime::{DatetimeFormatter, DEFAULT_DATETIME_FORMATTER};
mod duration;
pub use duration::{DurationFormatter, DEFAULT_DURATION_FORMATTER};
mod eng;
pub use eng::{EngFormatter, DEFAULT_NUMBER_FORMATTER};
mod flag;
pub use flag::{FlagFormatter, DEFAULT_FLAG_FORMATTER};
mod pango;
pub use pango::PangoStrFormatter;
mod str;
pub use str::{StrFormatter, DEFAULT_STRING_FORMATTER};

type PadWith = Cow<'static, str>;

const DEFAULT_NUMBER_PAD_WITH: PadWith = Cow::Borrowed(" ");

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

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

pub fn new_formatter(name: &str, args: &[Arg]) -> Result<Box<dyn Formatter>> {
    match name {
        "bar" => Ok(Box::new(BarFormatter::from_args(args)?)),
        "datetime" => Ok(Box::new(DatetimeFormatter::from_args(args)?)),
        "dur" | "duration" => Ok(Box::new(DurationFormatter::from_args(args)?)),
        "eng" => Ok(Box::new(EngFormatter::from_args(args)?)),
        "pango-str" => Ok(Box::new(PangoStrFormatter::from_args(args)?)),
        "str" => Ok(Box::new(StrFormatter::from_args(args)?)),
        "tally" => Ok(Box::new(TallyFormatter::from_args(args)?)),
        _ => Err(Error::new(format!("Unknown formatter: '{name}'"))),
    }
}