|
1 | 1 | use {
|
2 |
| - crate::onefetch::{info_fields::InfoFields, language::Language}, |
| 2 | + crate::onefetch::{ |
| 3 | + error::*, image_backends, info_fields, info_fields::InfoFields, language::Language, |
| 4 | + }, |
3 | 5 | clap::{crate_description, crate_name, crate_version, App, AppSettings, Arg},
|
| 6 | + image::DynamicImage, |
| 7 | + std::{convert::From, env, str::FromStr}, |
4 | 8 | strum::{EnumCount, IntoEnumIterator},
|
5 | 9 | };
|
6 | 10 |
|
7 |
| -pub fn build_app() -> App<'static, 'static> { |
8 |
| - #[cfg(target_os = "linux")] |
9 |
| - let possible_backends = ["kitty", "sixel"]; |
10 |
| - #[cfg(not(target_os = "linux"))] |
11 |
| - let possible_backends = []; |
| 11 | +pub struct Options { |
| 12 | + pub path: String, |
| 13 | + pub ascii_language: Language, |
| 14 | + pub ascii_colors: Vec<String>, |
| 15 | + pub disabled_fields: info_fields::InfoFieldOn, |
| 16 | + pub no_bold: bool, |
| 17 | + pub image: Option<DynamicImage>, |
| 18 | + pub image_backend: Option<Box<dyn image_backends::ImageBackend>>, |
| 19 | + pub no_merges: bool, |
| 20 | + pub no_color_blocks: bool, |
| 21 | + pub number_of_authors: usize, |
| 22 | + pub excluded: Vec<String>, |
| 23 | +} |
| 24 | + |
| 25 | +impl Options { |
| 26 | + /// Build `Options` from command line arguments. |
| 27 | + pub fn new() -> Result<Self> { |
| 28 | + #[cfg(target_os = "linux")] |
| 29 | + let possible_backends = ["kitty", "sixel"]; |
| 30 | + #[cfg(not(target_os = "linux"))] |
| 31 | + let possible_backends = []; |
12 | 32 |
|
13 |
| - App::new(crate_name!()) |
| 33 | + let matches = App::new(crate_name!()) |
14 | 34 | .version(crate_version!())
|
15 | 35 | .about(crate_description!())
|
16 | 36 | .setting(AppSettings::ColoredHelp)
|
@@ -122,5 +142,59 @@ pub fn build_app() -> App<'static, 'static> {
|
122 | 142 | .multiple(true)
|
123 | 143 | .takes_value(true)
|
124 | 144 | .help("Ignore all files & directories matching EXCLUDE."),
|
125 |
| - ) |
| 145 | + ).get_matches(); |
| 146 | + |
| 147 | + let fields_to_hide: Vec<String> = if let Some(values) = matches.values_of("disable-fields") |
| 148 | + { |
| 149 | + values.map(String::from).collect() |
| 150 | + } else { |
| 151 | + Vec::new() |
| 152 | + }; |
| 153 | + |
| 154 | + let image = if let Some(image_path) = matches.value_of("image") { |
| 155 | + Some(image::open(image_path).chain_err(|| "Could not load the specified image")?) |
| 156 | + } else { |
| 157 | + None |
| 158 | + }; |
| 159 | + |
| 160 | + let image_backend = if image.is_some() { |
| 161 | + if let Some(backend_name) = matches.value_of("image-backend") { |
| 162 | + image_backends::get_image_backend(backend_name) |
| 163 | + } else { |
| 164 | + image_backends::get_best_backend() |
| 165 | + } |
| 166 | + } else { |
| 167 | + None |
| 168 | + }; |
| 169 | + |
| 170 | + Ok(Options { |
| 171 | + path: String::from(matches.value_of("input").unwrap()), |
| 172 | + ascii_language: if let Some(ascii_language) = matches.value_of("ascii-language") { |
| 173 | + Language::from_str(&ascii_language.to_lowercase()).unwrap() |
| 174 | + } else { |
| 175 | + Language::Unknown |
| 176 | + }, |
| 177 | + ascii_colors: if let Some(values) = matches.values_of("ascii-colors") { |
| 178 | + values.map(String::from).collect() |
| 179 | + } else { |
| 180 | + Vec::new() |
| 181 | + }, |
| 182 | + disabled_fields: info_fields::get_disabled_fields(fields_to_hide)?, |
| 183 | + no_bold: !matches.is_present("no-bold"), |
| 184 | + image, |
| 185 | + image_backend, |
| 186 | + no_merges: matches.is_present("no-merge-commits"), |
| 187 | + no_color_blocks: matches.is_present("no-color-blocks"), |
| 188 | + number_of_authors: if let Some(value) = matches.value_of("authors-number") { |
| 189 | + usize::from_str(value).unwrap() |
| 190 | + } else { |
| 191 | + 3 |
| 192 | + }, |
| 193 | + excluded: if let Some(user_ignored) = matches.values_of("exclude") { |
| 194 | + user_ignored.map(String::from).collect() |
| 195 | + } else { |
| 196 | + Vec::new() |
| 197 | + }, |
| 198 | + }) |
| 199 | + } |
126 | 200 | }
|
0 commit comments