wallshift/
cli.rs

1use clap_derive::Parser;
2
3use crate::configuration::{get_configuration, Settings};
4
5pub enum Actions {
6    Launch,
7    Toggle,
8    Get,
9}
10
11#[derive(Parser)]
12pub struct Cli {
13    /// Toggle wallpaper
14    #[clap(short, long, conflicts_with_all = &["get", "seconds", "minutes"])]
15    toggle: bool,
16
17    /// Time between toggles in seconds. If not it defaults to 1800 seconds
18    #[clap(short, long, group = "sleep", conflicts_with = "minutes")]
19    seconds: Option<u64>,
20
21    /// Time between toggles in minutes. If not it defaults to 30 minutes
22    #[clap(short, long, group = "sleep", conflicts_with = "seconds")]
23    minutes: Option<u64>,
24
25    /// Get current wallpaper
26    #[clap(short, long, conflicts_with_all = &["toggle", "seconds", "minutes", "betterlockscreen"])]
27    get: bool,
28
29    /// Updates the betterlockscreen wallpaper
30    #[clap(long, group = "input")]
31    betterlockscreen: Option<bool>,
32}
33
34impl Cli {
35    fn get_seconds(&self) -> Option<u64> {
36        if let Some(seconds) = self.seconds {
37            return Some(seconds);
38        }
39        if let Some(minutes) = self.minutes {
40            return Some(minutes * 60);
41        }
42        None
43    }
44
45    pub fn get_settings(&self) -> Settings {
46        let mut settings = get_configuration().unwrap_or_else(|_| Settings::default());
47
48        if let Some(seconds) = self.get_seconds() {
49            settings.sleep_time = seconds;
50        }
51
52        if let Some(betterlockscreen) = self.betterlockscreen {
53            settings.betterlockscreen = betterlockscreen;
54        }
55
56        settings
57    }
58
59    pub fn get_action(&self) -> Actions {
60        if self.toggle {
61            return Actions::Toggle;
62        }
63        if self.get {
64            return Actions::Get;
65        }
66
67        Actions::Launch
68    }
69}