Skip to content

Commit

Permalink
auto, always, never options for hide-logo with always as default
Browse files Browse the repository at this point in the history
  • Loading branch information
o2sh committed Nov 29, 2020
1 parent c72dbbe commit 51a7a4b
Showing 1 changed file with 26 additions and 44 deletions.
70 changes: 26 additions & 44 deletions src/onefetch/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use {
term_size,
};

const MAX_TERM_WIDTH: usize = 95;

pub struct Cli {
pub repo_path: String,
pub ascii_input: Option<String>,
Expand Down Expand Up @@ -95,7 +97,7 @@ impl Cli {
.help("Takes a non-empty STRING as input to replace the ASCII logo.")
.long_help(
"Takes a non-empty STRING as input to replace the ASCII logo. \
It is possible to pass a generated STRING by command substitution. \
It is possible to pass a generated STRING by command substitution. \n\
For example:\n \
'--ascii-input \"$(fortune | cowsay -W 25)\"'"
)
Expand Down Expand Up @@ -130,7 +132,7 @@ impl Cli {
.help("Changes the text colors (X X X...).")
.long_help(
"Changes the text colors (X X X...). \
Goes in order of title, ~, underline, subtitle, colon, and info. \
Goes in order of title, ~, underline, subtitle, colon, and info. \n\
For example:\n \
'--text-colors 9 10 11 12 13 14'"
)
Expand Down Expand Up @@ -218,49 +220,27 @@ impl Cli {
.takes_value(true)
.help("Ignore all files & directories matching EXCLUDE."),
)
.arg(
Arg::with_name("show-logo")
.short("S")
.long("show-logo")
.takes_value(false)
.help("If ASCII logo should be shown in all circumstances.")
.conflicts_with("hide-logo")
)
.arg(
Arg::with_name("hide-logo")
.short("H")
.long("hide-logo")
.takes_value(false)
.help("If ASCII logo should be hidden in all circumstances.")
.conflicts_with("show-logo")
)
.arg(
Arg::with_name("max-width")
.short("w")
.long("max-width")
.value_name("AMOUNT")
.value_name("WHEN")
.takes_value(true)
.max_values(1)
.default_value("95")
.help("If ASCII logo should be hidden when terminal width is below AMOUNT.")
.validator(
|t| {
t.parse::<u32>()
.map_err(|_t| "must be a number")
.map(|_t|())
.map_err(|e| e.to_string())
},
.possible_values(&["auto", "never", "always"])
.default_value("always")
.hide_default_value(true)
.help("Specify when to hide the logo (auto, never, *always*).")
.long_help(
"Specify when to hide the logo (auto, never, *always*). \n\
If set to auto, the logo will be hidden if the terminal's width < 95."
)
).get_matches();

let true_color = cli_utils::is_truecolor_terminal();
let no_bold = matches.is_present("no-bold");
let no_merges = matches.is_present("no-merge-commits");
let no_color_palette = matches.is_present("no-color-palette");
let print_languages = matches.is_present("languages");
let print_package_managers = matches.is_present("package-managers");
let mut art_off = matches.is_present("hide-logo") || !matches.is_present("show-logo");
let true_color = cli_utils::is_truecolor_terminal();
let max_term_size: usize = matches.value_of("max-width").unwrap().parse().unwrap();

let fields_to_hide: Vec<String> = if let Some(values) = matches.values_of("disable-fields")
{
Expand All @@ -269,6 +249,19 @@ impl Cli {
Vec::new()
};

let art_off = match matches.value_of("hide-logo") {
Some("always") => true,
Some("never") => false,
Some("auto") => {
if let Some((width, _)) = term_size::dimensions_stdout() {
width < MAX_TERM_WIDTH
} else {
false
}
}
_ => unreachable!("other values for --hide-logo are not allowed"),
};

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 {
Expand All @@ -285,17 +278,6 @@ impl Cli {
None
};

if !matches.is_present("hide-logo") && !matches.is_present("show-logo") {
if let Some((width, _)) = term_size::dimensions_stdout() {
art_off = width <= max_term_size;
} else {
std::eprintln!(
"{}",
("Could not get terminal width. ASCII art will be displayed.")
);
}
}

if image.is_some() && image_backend.is_none() {
return Err("Could not detect a supported image backend".into());
}
Expand Down

0 comments on commit 51a7a4b

Please sign in to comment.