~grtcdr/citron

6f91aeec5b415310de1b454297df056b216ca79f — grtcdr 2 years ago 651fb97
Every subcommand has its own timeout

- The general timeout still remains, used only when a subcommand isn't
configured to have its own timeout.

- Refactored is_railway()

- the format directive has been renamed to body, which seems more
sensible.
4 files changed, 65 insertions(+), 35 deletions(-)

M src/config.rs
M src/main.rs
M src/notifier.rs
M src/read.rs
M src/config.rs => src/config.rs +15 -9
@@ 11,43 11,49 @@ pub struct Icon {
pub struct Network {
    pub title: Option<String>,
    pub interface: Option<String>,
    pub format: Option<String>,
    pub body: Option<String>,
    pub timeout: Option<u8>,
}

#[derive(Default, Debug, Serialize, Deserialize)]
pub struct Battery {
    pub title: Option<String>,
    pub format: Option<String>,
    pub body: Option<String>,
    pub timeout: Option<u8>,
}

#[derive(Default, Debug, Serialize, Deserialize)]
pub struct Backlight {
    pub title: Option<String>,
    pub timeout: Option<u8>,
}

#[derive(Default, Debug, Serialize, Deserialize)]
pub struct Uptime {
    pub title: Option<String>,
    pub timeout: Option<u8>,
}

#[derive(Default, Debug, Serialize, Deserialize)]
pub struct Memory {
    pub title: Option<String>,
    pub timeout: Option<u8>,
}

#[derive(Default, Debug, Serialize, Deserialize)]
pub struct Date {
    pub title: Option<String>,
    pub format: Option<String>,
    pub body: Option<String>,
    pub timeout: Option<u8>,
}

impl Date {
    pub fn is_railway(&self) -> bool {
        if let Some(format) = &self.format {
            !format.contains("%P")
        } else {
            false
        }
        !&self
            .body
            .as_ref()
            .map(|x| x.contains("%P"))
            .unwrap_or(false)
    }
}



@@ 60,7 66,7 @@ pub struct Config {
    pub memory: Option<Memory>,
    pub uptime: Option<Uptime>,
    pub icon: Option<Icon>,
    pub timeout: Option<u32>,
    pub timeout: Option<u8>,
}

impl Config {

M src/main.rs => src/main.rs +45 -21
@@ 36,6 36,10 @@ fn main() {
        }
    }

    if let Some(timeout) = config.timeout {
        notifier.timeout = timeout;
    }

    let use_icons = icon.theme.is_some();

    if let Some(cmd) = &opt.cmd {


@@ 44,49 48,73 @@ fn main() {
        match cmd {
            Subcommand::Backlight => {
                notifier.set_body(read::backlight(icon_opts));
                if let Some(bac) = &config.backlight {
                    if let Some(ti) = &bac.title {
                        notifier.title = Title::Custom(ti.to_string());
                if let Some(backlight) = &config.backlight {
                    if let Some(timeout) = &backlight.title {
                        notifier.title = Title::Custom(timeout.to_string());
                    }

                    if let Some(timeout) = &backlight.timeout {
                        notifier.timeout = *timeout;
                    }
                }
            }
            Subcommand::Battery => {
                notifier.set_body(read::battery(&config, icon_opts));
                if let Some(bat) = &config.battery {
                    if let Some(ti) = &bat.title {
                        notifier.title = Title::Custom(ti.to_string());
                if let Some(battery) = &config.battery {
                    if let Some(title) = &battery.title {
                        notifier.title = Title::Custom(title.to_string());
                    }

                    if let Some(timeout) = &battery.timeout {
                        notifier.timeout = *timeout;
                    }
                }
            }
            Subcommand::Uptime => {
                notifier.set_body(read::uptime(icon_opts));
                if let Some(up) = &config.uptime {
                    if let Some(ti) = &up.title {
                        notifier.title = Title::Custom(ti.to_string());
                if let Some(uptime) = &config.uptime {
                    if let Some(title) = &uptime.title {
                        notifier.title = Title::Custom(title.to_string());
                    }

                    if let Some(timeout) = &uptime.timeout {
                        notifier.timeout = *timeout;
                    }
                }
            }
            Subcommand::Network => {
                notifier.set_body(read::network(&config, icon_opts));
                if let Some(net) = &config.network {
                    if let Some(ti) = &net.title {
                        notifier.title = Title::Custom(ti.to_string());
                if let Some(network) = &config.network {
                    if let Some(title) = &network.title {
                        notifier.title = Title::Custom(title.to_string());
                    }

                    if let Some(timeout) = &network.timeout {
                        notifier.timeout = *timeout;
                    }
                }
            }
            Subcommand::Date => {
                notifier.set_body(read::date(&config, icon_opts));
                if let Some(date) = &config.date {
                    if let Some(ti) = &date.title {
                        notifier.title = Title::Custom(ti.to_string());
                    if let Some(title) = &date.title {
                        notifier.title = Title::Custom(title.to_string());
                    }

                    if let Some(timeout) = &date.timeout {
                        notifier.timeout = *timeout;
                    }
                }
            }
            Subcommand::Memory => {
                notifier.set_body(read::memory(icon_opts));
                if let Some(mem) = &config.memory {
                    if let Some(ti) = &mem.title {
                        notifier.title = Title::Custom(ti.to_string());
                if let Some(memory) = &config.memory {
                    if let Some(title) = &memory.title {
                        notifier.title = Title::Custom(title.to_string());
                    }

                    if let Some(timeout) = &memory.timeout {
                        notifier.timeout = *timeout;
                    }
                }
            }


@@ 98,10 126,6 @@ fn main() {
            _ => (),
        }

        if let Some(timeout) = config.timeout {
            notifier.timeout = timeout;
        }

        notifier.send();
    }
}

M src/notifier.rs => src/notifier.rs +2 -2
@@ 13,7 13,7 @@ pub struct Notifier {
    /// The path to an icon.
    pub icon: PathBuf,
    /// The timeout of a notification.
    pub timeout: u32,
    pub timeout: u8,
}

#[allow(clippy::new_without_default)]


@@ 38,7 38,7 @@ impl Notifier {
        Notification::new()
            .body(&self.body)
            .summary(&self.title.to_string())
            .timeout(Timeout::Milliseconds(self.timeout * 1000))
            .timeout(Timeout::Milliseconds(self.timeout as u32 * 1000))
            .icon(&self.icon.to_string_lossy())
            .show()
            .expect("No notification daemon running.");

M src/read.rs => src/read.rs +3 -3
@@ 61,7 61,7 @@ pub fn network(config: &Config, icon: (&mut String, bool)) -> Result<String, Rea
                fmt.set_tx_bytes(tx_bytes);
            }

            if let Some(format) = &network.format {
            if let Some(format) = &network.body {
                return Ok(fmt.with_format(format.to_string()));
            }
        }


@@ 104,7 104,7 @@ pub fn battery(config: &Config, icon: (&mut String, bool)) -> Result<String, Rea
    }

    if let Some(battery) = &config.battery {
        if let Some(format) = &battery.format {
        if let Some(format) = &battery.body {
            return Ok(fmt.with_format(format.to_owned()));
        }
    }


@@ 145,7 145,7 @@ pub fn date(config: &Config, icon: (&mut String, bool)) -> Result<String, Readou
        }

        if let Some(date) = &config.date {
            if let Some(format) = &date.format {
            if let Some(format) = &date.body {
                return Ok(fmt.with_format(format.to_owned()));
            }
        }