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
use crate::{get_config_path, read_config_file, MyResult};
use anstyle::{
    AnsiColor::{Cyan, Green, Yellow},
    Color::Ansi,
    Style,
};
use clap::Parser; // command-line arguments

// https://stackoverflow.com/questions/74068168/clap-rs-not-printing-colors-during-help
fn get_styles() -> clap::builder::Styles {
    clap::builder::Styles::styled()
        .placeholder(Style::new().fg_color(Some(Ansi(Yellow))))
        .usage(Style::new().fg_color(Some(Ansi(Cyan))).bold())
        .header(Style::new().fg_color(Some(Ansi(Cyan))).bold().underline())
        .literal(Style::new().fg_color(Some(Ansi(Green))))
}

#[derive(Parser, Debug, Clone)]
#[command(
    // Read from `Cargo.toml`
    author, version, about,
    long_about = None,
    next_line_help = true,
    styles=get_styles(),
)]
pub struct Arguments {
    /// Read the configuration file and exit the program.
    #[arg(short('c'), long("config"), default_value_t = false)]
    pub config: bool,

    /// Set the minimum dimension that the height and width must satisfy.
    ///
    /// dimension: width x length of an image
    ///
    /// Default value = 800
    /*
    #[arg(
        short('d'), long("dimension"),
        required = false,
        default_value_t = 800,
        value_parser = clap::value_parser!(u32).range(10..)
    )]
    */
    #[arg(short('d'), long("dimension"))]
    pub dimension: Option<u32>,

    /// Set the interval (in seconds) between each wallpaper displayed.
    ///
    /// Default value = 30 * 60 = 1800 seconds (30 minutes).
    /*
    #[arg(
        short('i'), long("interval"),
        required = false,
        default_value_t = 1800,
        value_parser = clap::value_parser!(u64).range(5..)
    )]
    */
    #[arg(short('i'), long("interval"))]
    pub interval: Option<u64>,

    /// Set the number of monitors.
    #[arg(short('n'), long("monitor_number"), default_value_t = 2)]
    pub monitor_number: usize,

    /// Sort the images found.
    #[arg(short('s'), long("sort"), default_value_t = false)]
    pub sort: bool,

    /// Show intermediate runtime messages.
    ///
    /// Show found images.
    ///
    /// Show pid numbers of previous running program.
    #[arg(short('v'), long("verbose"), default_value_t = false)]
    pub verbose: bool,
}

impl Arguments {
    /// Build Arguments struct
    pub fn build() -> MyResult<Arguments> {
        let args: Arguments = Arguments::parse();

        if args.config {
            let config_path = get_config_path()?;
            let config = read_config_file(&config_path)?;
            let json: String = serde_json::to_string_pretty(&config)?;
            println!("{json}");
            std::process::exit(0);
        }

        Ok(args)
    }
}