Skip to content

Commit 8419edd

Browse files
committed
merge Options with cli
1 parent 5e595a5 commit 8419edd

File tree

5 files changed

+89
-114
lines changed

5 files changed

+89
-114
lines changed

src/main.rs

+5-73
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
// `error_chain!` can recurse deeply
22
#![recursion_limit = "1024"]
33

4-
use onefetch::{
5-
clap_app, error::*, image_backends, info, info_fields, language::Language, options,
6-
};
4+
use onefetch::{cli::Options, error::*, info};
75

86
use {
97
process::{Command, Stdio},
10-
std::{convert::From, env, process, str::FromStr},
11-
strum::IntoEnumIterator,
8+
std::process,
129
};
1310

1411
mod onefetch;
@@ -21,80 +18,15 @@ fn run() -> Result<()> {
2118
return Err("Git failed to execute!".into());
2219
}
2320

24-
let matches = clap_app::build_app().get_matches_from(env::args_os());
25-
26-
if matches.is_present("languages") {
27-
return list_languages();
28-
}
29-
30-
let fields_to_hide: Vec<String> = if let Some(values) = matches.values_of("disable-fields") {
31-
values.map(String::from).collect()
32-
} else {
33-
Vec::new()
34-
};
35-
36-
let image = if let Some(image_path) = matches.value_of("image") {
37-
Some(image::open(image_path).chain_err(|| "Could not load the specified image")?)
38-
} else {
39-
None
40-
};
41-
42-
let image_backend = if image.is_some() {
43-
if let Some(backend_name) = matches.value_of("image-backend") {
44-
image_backends::get_image_backend(backend_name)
45-
} else {
46-
image_backends::get_best_backend()
47-
}
48-
} else {
49-
None
50-
};
51-
52-
let config = options::Options {
53-
path: String::from(matches.value_of("input").unwrap()),
54-
ascii_language: if let Some(ascii_language) = matches.value_of("ascii-language") {
55-
Language::from_str(&ascii_language.to_lowercase()).unwrap()
56-
} else {
57-
Language::Unknown
58-
},
59-
ascii_colors: if let Some(values) = matches.values_of("ascii-colors") {
60-
values.map(String::from).collect()
61-
} else {
62-
Vec::new()
63-
},
64-
disabled_fields: info_fields::get_disabled_fields(fields_to_hide)?,
65-
no_bold: !matches.is_present("no-bold"),
66-
image,
67-
image_backend,
68-
no_merges: matches.is_present("no-merge-commits"),
69-
no_color_blocks: matches.is_present("no-color-blocks"),
70-
number_of_authors: if let Some(value) = matches.value_of("authors-number") {
71-
usize::from_str(value).unwrap()
72-
} else {
73-
3
74-
},
75-
excluded: if let Some(user_ignored) = matches.values_of("exclude") {
76-
user_ignored.map(String::from).collect()
77-
} else {
78-
Vec::new()
79-
},
80-
};
21+
// Load command line options.
22+
let options = Options::new()?;
8123

82-
let info = info::Info::new(config)?;
24+
let info = info::Info::new(options)?;
8325

8426
print!("{}", info);
8527
Ok(())
8628
}
8729

88-
pub fn list_languages() -> Result<()> {
89-
let iterator = Language::iter().filter(|x| *x != Language::Unknown);
90-
91-
for l in iterator {
92-
println!("{}", l);
93-
}
94-
95-
Ok(())
96-
}
97-
9830
fn main() {
9931
let result = run();
10032
match result {

src/onefetch/clap_app.rs renamed to src/onefetch/cli.rs

+82-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,36 @@
11
use {
2-
crate::onefetch::{info_fields::InfoFields, language::Language},
2+
crate::onefetch::{
3+
error::*, image_backends, info_fields, info_fields::InfoFields, language::Language,
4+
},
35
clap::{crate_description, crate_name, crate_version, App, AppSettings, Arg},
6+
image::DynamicImage,
7+
std::{convert::From, env, str::FromStr},
48
strum::{EnumCount, IntoEnumIterator},
59
};
610

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 = [];
1232

13-
App::new(crate_name!())
33+
let matches = App::new(crate_name!())
1434
.version(crate_version!())
1535
.about(crate_description!())
1636
.setting(AppSettings::ColoredHelp)
@@ -122,5 +142,59 @@ pub fn build_app() -> App<'static, 'static> {
122142
.multiple(true)
123143
.takes_value(true)
124144
.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+
}
126200
}

src/onefetch/info.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use {
22
crate::onefetch::{
33
ascii_art::AsciiArt,
4+
cli::Options,
45
commit_info::CommitInfo,
56
error::*,
67
language::Language,
78
license::{Detector, LICENSE_FILES},
8-
options::Options,
99
},
1010
colored::{Color, ColoredString, Colorize},
1111
git2::Repository,

src/onefetch/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
pub mod ascii_art;
2-
pub mod clap_app;
2+
pub mod cli;
33
pub mod commit_info;
44
pub mod error;
55
pub mod image_backends;
66
pub mod info;
77
pub mod info_fields;
88
pub mod language;
99
pub mod license;
10-
pub mod options;

src/onefetch/options.rs

-30
This file was deleted.

0 commit comments

Comments
 (0)