Skip to content

Commit

Permalink
merge Options with cli
Browse files Browse the repository at this point in the history
  • Loading branch information
o2sh committed Oct 12, 2020
1 parent 5e595a5 commit 8419edd
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 114 deletions.
78 changes: 5 additions & 73 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
// `error_chain!` can recurse deeply
#![recursion_limit = "1024"]

use onefetch::{
clap_app, error::*, image_backends, info, info_fields, language::Language, options,
};
use onefetch::{cli::Options, error::*, info};

use {
process::{Command, Stdio},
std::{convert::From, env, process, str::FromStr},
strum::IntoEnumIterator,
std::process,
};

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

let matches = clap_app::build_app().get_matches_from(env::args_os());

if matches.is_present("languages") {
return list_languages();
}

let fields_to_hide: Vec<String> = if let Some(values) = matches.values_of("disable-fields") {
values.map(String::from).collect()
} else {
Vec::new()
};

let image = if let Some(image_path) = matches.value_of("image") {
Some(image::open(image_path).chain_err(|| "Could not load the specified image")?)
} else {
None
};

let image_backend = if image.is_some() {
if let Some(backend_name) = matches.value_of("image-backend") {
image_backends::get_image_backend(backend_name)
} else {
image_backends::get_best_backend()
}
} else {
None
};

let config = options::Options {
path: String::from(matches.value_of("input").unwrap()),
ascii_language: if let Some(ascii_language) = matches.value_of("ascii-language") {
Language::from_str(&ascii_language.to_lowercase()).unwrap()
} else {
Language::Unknown
},
ascii_colors: if let Some(values) = matches.values_of("ascii-colors") {
values.map(String::from).collect()
} else {
Vec::new()
},
disabled_fields: info_fields::get_disabled_fields(fields_to_hide)?,
no_bold: !matches.is_present("no-bold"),
image,
image_backend,
no_merges: matches.is_present("no-merge-commits"),
no_color_blocks: matches.is_present("no-color-blocks"),
number_of_authors: if let Some(value) = matches.value_of("authors-number") {
usize::from_str(value).unwrap()
} else {
3
},
excluded: if let Some(user_ignored) = matches.values_of("exclude") {
user_ignored.map(String::from).collect()
} else {
Vec::new()
},
};
// Load command line options.
let options = Options::new()?;

let info = info::Info::new(config)?;
let info = info::Info::new(options)?;

print!("{}", info);
Ok(())
}

pub fn list_languages() -> Result<()> {
let iterator = Language::iter().filter(|x| *x != Language::Unknown);

for l in iterator {
println!("{}", l);
}

Ok(())
}

fn main() {
let result = run();
match result {
Expand Down
90 changes: 82 additions & 8 deletions src/onefetch/clap_app.rs → src/onefetch/cli.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,36 @@
use {
crate::onefetch::{info_fields::InfoFields, language::Language},
crate::onefetch::{
error::*, image_backends, info_fields, info_fields::InfoFields, language::Language,
},
clap::{crate_description, crate_name, crate_version, App, AppSettings, Arg},
image::DynamicImage,
std::{convert::From, env, str::FromStr},
strum::{EnumCount, IntoEnumIterator},
};

pub fn build_app() -> App<'static, 'static> {
#[cfg(target_os = "linux")]
let possible_backends = ["kitty", "sixel"];
#[cfg(not(target_os = "linux"))]
let possible_backends = [];
pub struct Options {
pub path: String,
pub ascii_language: Language,
pub ascii_colors: Vec<String>,
pub disabled_fields: info_fields::InfoFieldOn,
pub no_bold: bool,
pub image: Option<DynamicImage>,
pub image_backend: Option<Box<dyn image_backends::ImageBackend>>,
pub no_merges: bool,
pub no_color_blocks: bool,
pub number_of_authors: usize,
pub excluded: Vec<String>,
}

impl Options {
/// Build `Options` from command line arguments.
pub fn new() -> Result<Self> {
#[cfg(target_os = "linux")]
let possible_backends = ["kitty", "sixel"];
#[cfg(not(target_os = "linux"))]
let possible_backends = [];

App::new(crate_name!())
let matches = App::new(crate_name!())
.version(crate_version!())
.about(crate_description!())
.setting(AppSettings::ColoredHelp)
Expand Down Expand Up @@ -122,5 +142,59 @@ pub fn build_app() -> App<'static, 'static> {
.multiple(true)
.takes_value(true)
.help("Ignore all files & directories matching EXCLUDE."),
)
).get_matches();

let fields_to_hide: Vec<String> = if let Some(values) = matches.values_of("disable-fields")
{
values.map(String::from).collect()
} else {
Vec::new()
};

let image = if let Some(image_path) = matches.value_of("image") {
Some(image::open(image_path).chain_err(|| "Could not load the specified image")?)
} else {
None
};

let image_backend = if image.is_some() {
if let Some(backend_name) = matches.value_of("image-backend") {
image_backends::get_image_backend(backend_name)
} else {
image_backends::get_best_backend()
}
} else {
None
};

Ok(Options {
path: String::from(matches.value_of("input").unwrap()),
ascii_language: if let Some(ascii_language) = matches.value_of("ascii-language") {
Language::from_str(&ascii_language.to_lowercase()).unwrap()
} else {
Language::Unknown
},
ascii_colors: if let Some(values) = matches.values_of("ascii-colors") {
values.map(String::from).collect()
} else {
Vec::new()
},
disabled_fields: info_fields::get_disabled_fields(fields_to_hide)?,
no_bold: !matches.is_present("no-bold"),
image,
image_backend,
no_merges: matches.is_present("no-merge-commits"),
no_color_blocks: matches.is_present("no-color-blocks"),
number_of_authors: if let Some(value) = matches.value_of("authors-number") {
usize::from_str(value).unwrap()
} else {
3
},
excluded: if let Some(user_ignored) = matches.values_of("exclude") {
user_ignored.map(String::from).collect()
} else {
Vec::new()
},
})
}
}
2 changes: 1 addition & 1 deletion src/onefetch/info.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use {
crate::onefetch::{
ascii_art::AsciiArt,
cli::Options,
commit_info::CommitInfo,
error::*,
language::Language,
license::{Detector, LICENSE_FILES},
options::Options,
},
colored::{Color, ColoredString, Colorize},
git2::Repository,
Expand Down
3 changes: 1 addition & 2 deletions src/onefetch/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
pub mod ascii_art;
pub mod clap_app;
pub mod cli;
pub mod commit_info;
pub mod error;
pub mod image_backends;
pub mod info;
pub mod info_fields;
pub mod language;
pub mod license;
pub mod options;
30 changes: 0 additions & 30 deletions src/onefetch/options.rs

This file was deleted.

0 comments on commit 8419edd

Please sign in to comment.