git_hist/
args.rs

1use clap::{App, Arg};
2
3#[derive(Debug)]
4pub struct Args {
5    pub file_path: String,
6    pub should_use_full_commit_hash: bool,
7    pub beyond_last_line: bool,
8    pub should_emphasize_diff: bool,
9    pub user_for_name: UserType,
10    pub user_for_date: UserType,
11    pub date_format: String,
12    pub tab_spaces: String,
13}
14
15#[derive(Debug)]
16pub enum UserType {
17    Author,
18    Committer,
19}
20
21impl Args {
22    pub fn load() -> Args {
23        let matches = App::new(env!("CARGO_PKG_NAME"))
24            .version(env!("CARGO_PKG_VERSION"))
25            .about(env!("CARGO_PKG_DESCRIPTION"))
26            .setting(clap::AppSettings::ColoredHelp)
27            .arg(
28                Arg::new("help")
29                    .long("help")
30                    .short('h')
31                    .help("Print help information"),
32            )
33            .arg(
34                Arg::new("version")
35                    .long("version")
36                    .short('v')
37                    .help("Print version information"),
38            )
39            .arg(
40                Arg::new("full-hash")
41                    .long("full-hash")
42                    .help("Show full commit hashes instead of abbreviated commit hashes"),
43            )
44            .arg(
45                Arg::new("beyond-last-line")
46                    .long("beyond-last-line")
47                    .help("Set whether the view will scroll beyond the last line"),
48            )
49            .arg(
50                Arg::new("emphasize-diff")
51                    .long("emphasize-diff")
52                    .help("Set whether the view will emphasize different parts"),
53            )
54            .arg(
55                Arg::new("name-of")
56                    .long("name-of")
57                    .value_name("user")
58                    .possible_values(&["author", "committer"])
59                    .default_value("author")
60                    .help("Use whether authors or committers for names"),
61            )
62            .arg(
63                Arg::new("date-of")
64                    .long("date-of")
65                    .value_name("user")
66                    .possible_values(&["author", "committer"])
67                    .default_value("author")
68                    .help("Use whether authors or committers for dates"),
69            )
70            .arg(
71                Arg::new("date-format")
72                    .long("date-format")
73                    .value_name("format")
74                    .default_value("[%Y-%m-%d]")
75                    .help("Set date format: ref. https://docs.rs/chrono/0.4.19/chrono/format/strftime/index.html"),
76            )
77            .arg(
78                Arg::new("tab-size")
79                    .long("tab-size")
80                    .value_name("size")
81                    .default_value("4")
82                    .help("Set the number of spaces for a tab character (\\t)")
83            )
84            .arg(
85                Arg::new("file")
86                    .help("Set a target file path")
87                    .required(true),
88            )
89            .get_matches();
90
91        let file_path = String::from(matches.value_of("file").unwrap());
92
93        let should_use_full_commit_hash = matches.is_present("full-hash");
94        let beyond_last_line = matches.is_present("beyond-last-line");
95        let should_emphasize_diff = matches.is_present("emphasize-diff");
96        let user_for_name = if matches.value_of("name-of").unwrap() == "author" {
97            UserType::Author
98        } else {
99            UserType::Committer
100        };
101        let user_for_date = if matches.value_of("date-of").unwrap() == "author" {
102            UserType::Author
103        } else {
104            UserType::Committer
105        };
106        let date_format = String::from(matches.value_of("date-format").unwrap());
107
108        let tab_size = matches
109            .value_of_t::<usize>("tab-size")
110            .unwrap_or_else(|e| e.exit());
111        let tab_spaces = " ".repeat(tab_size);
112
113        Args {
114            file_path,
115            should_use_full_commit_hash,
116            beyond_last_line,
117            should_emphasize_diff,
118            user_for_name,
119            user_for_date,
120            date_format,
121            tab_spaces,
122        }
123    }
124}