[O] shorten code
This commit is contained in:
parent
8f5199974b
commit
c722c73e79
4 changed files with 32 additions and 94 deletions
|
|
@ -2,7 +2,7 @@ use std::borrow::Cow;
|
||||||
use std::cmp;
|
use std::cmp;
|
||||||
use std::fmt::Write as _;
|
use std::fmt::Write as _;
|
||||||
use std::fs::{self, File};
|
use std::fs::{self, File};
|
||||||
use std::io::{self, IsTerminal as _, Read as _, Write as _};
|
use std::io::{self, IsTerminal as _, Read as _};
|
||||||
use std::iter;
|
use std::iter;
|
||||||
use std::iter::zip;
|
use std::iter::zip;
|
||||||
use std::num::NonZeroU8;
|
use std::num::NonZeroU8;
|
||||||
|
|
@ -66,14 +66,13 @@ fn main() -> Result<()> {
|
||||||
|
|
||||||
if options.test_print {
|
if options.test_print {
|
||||||
let asc = get_distro_ascii(distro, backend).context("failed to get distro ascii")?;
|
let asc = get_distro_ascii(distro, backend).context("failed to get distro ascii")?;
|
||||||
writeln!(io::stdout(), "{asc}", asc = asc.asc)
|
println!("{asc}", asc = asc.asc);
|
||||||
.context("failed to write ascii to stdout")?;
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
if options.print_font_logo {
|
if options.print_font_logo {
|
||||||
let logo = get_font_logo(backend).context("failed to get font logo")?;
|
let logo = get_font_logo(backend).context("failed to get font logo")?;
|
||||||
writeln!(io::stdout(), "{}", logo).context("failed to write logo to stdout")?;
|
println!("{logo}");
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -111,12 +110,7 @@ fn main() -> Result<()> {
|
||||||
|
|
||||||
if show_pride_month && !config.pride_month_disable {
|
if show_pride_month && !config.pride_month_disable {
|
||||||
pride_month::start_animation(color_mode).context("failed to draw pride month animation")?;
|
pride_month::start_animation(color_mode).context("failed to draw pride month animation")?;
|
||||||
writeln!(
|
println!("\nHappy pride month!\n(You can always view the animation again with `hyfetch --june`)\n");
|
||||||
io::stdout(),
|
|
||||||
"\nHappy pride month!\n(You can always view the animation again with `hyfetch \
|
|
||||||
--june`)\n"
|
|
||||||
)
|
|
||||||
.context("failed to write message to stdout")?;
|
|
||||||
|
|
||||||
if !june_path.is_file() && !options.june {
|
if !june_path.is_file() && !options.june {
|
||||||
File::create(&june_path)
|
File::create(&june_path)
|
||||||
|
|
@ -432,18 +426,13 @@ fn create_config(
|
||||||
printc(line, AnsiMode::Rgb).context("failed to print RGB color test line")?;
|
printc(line, AnsiMode::Rgb).context("failed to print RGB color test line")?;
|
||||||
}
|
}
|
||||||
|
|
||||||
writeln!(io::stdout()).context("failed to write to stdout")?;
|
println!();
|
||||||
print_title_prompt(
|
print_title_prompt(
|
||||||
option_counter,
|
option_counter,
|
||||||
"Which &bcolor system &ado you want to use?",
|
"Which &bcolor system &ado you want to use?",
|
||||||
color_mode,
|
color_mode,
|
||||||
)
|
)?;
|
||||||
.context("failed to print title prompt")?;
|
println!("(If you can't see colors under \"RGB Color Testing\", please choose 8bit)\n");
|
||||||
writeln!(
|
|
||||||
io::stdout(),
|
|
||||||
"(If you can't see colors under \"RGB Color Testing\", please choose 8bit)\n"
|
|
||||||
)
|
|
||||||
.context("failed to write message to stdout")?;
|
|
||||||
|
|
||||||
let choice = literal_input(
|
let choice = literal_input(
|
||||||
"Your choice?",
|
"Your choice?",
|
||||||
|
|
@ -562,16 +551,11 @@ fn create_config(
|
||||||
clear_screen(Some(&title), color_mode, debug_mode).context("failed to clear screen")?;
|
clear_screen(Some(&title), color_mode, debug_mode).context("failed to clear screen")?;
|
||||||
print_title_prompt(option_counter, "Let's choose a flag!", color_mode)
|
print_title_prompt(option_counter, "Let's choose a flag!", color_mode)
|
||||||
.context("failed to print title prompt")?;
|
.context("failed to print title prompt")?;
|
||||||
writeln!(
|
println!("Available flag presets:\nPage: {page_num} of {num_pages}\n", page_num = page_num + 1);
|
||||||
io::stdout(),
|
|
||||||
"Available flag presets:\nPage: {page_num} of {num_pages}\n",
|
|
||||||
page_num = page_num.checked_add(1).unwrap()
|
|
||||||
)
|
|
||||||
.context("failed to write header to stdout")?;
|
|
||||||
for &row in page {
|
for &row in page {
|
||||||
print_flag_row(row, color_mode).context("failed to print flag row")?;
|
print_flag_row(row, color_mode).context("failed to print flag row")?;
|
||||||
}
|
}
|
||||||
writeln!(io::stdout()).context("failed to write to stdout")?;
|
println!();
|
||||||
Ok(())
|
Ok(())
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -583,7 +567,7 @@ fn create_config(
|
||||||
}
|
}
|
||||||
printc(line.join(" "), color_mode).context("failed to print line")?;
|
printc(line.join(" "), color_mode).context("failed to print line")?;
|
||||||
}
|
}
|
||||||
writeln!(io::stdout()).context("failed to write to stdout")?;
|
println!();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -608,16 +592,9 @@ fn create_config(
|
||||||
let mut opts: Vec<&str> = <Preset as VariantNames>::VARIANTS.into();
|
let mut opts: Vec<&str> = <Preset as VariantNames>::VARIANTS.into();
|
||||||
opts.extend(["next", "n", "prev", "p"]);
|
opts.extend(["next", "n", "prev", "p"]);
|
||||||
|
|
||||||
writeln!(
|
println!("Enter '[n]ext' to go to the next page and '[p]rev' to go to the previous page.");
|
||||||
io::stdout(),
|
|
||||||
"Enter '[n]ext' to go to the next page and '[p]rev' to go to the previous page."
|
|
||||||
)
|
|
||||||
.context("failed to write message to stdout")?;
|
|
||||||
let selection = literal_input(
|
let selection = literal_input(
|
||||||
format!(
|
format!("Which {preset_default_colored} do you want to use? "),
|
||||||
"Which {preset} do you want to use? ",
|
|
||||||
preset = preset_default_colored
|
|
||||||
),
|
|
||||||
&opts[..],
|
&opts[..],
|
||||||
Preset::Rainbow.as_ref(),
|
Preset::Rainbow.as_ref(),
|
||||||
false,
|
false,
|
||||||
|
|
@ -672,16 +649,14 @@ fn create_config(
|
||||||
color_mode,
|
color_mode,
|
||||||
)
|
)
|
||||||
.context("failed to print title prompt")?;
|
.context("failed to print title prompt")?;
|
||||||
writeln!(
|
println!(
|
||||||
io::stdout(),
|
|
||||||
"The colors might be a little bit too {bright_dark} for {light_dark} mode.\n",
|
"The colors might be a little bit too {bright_dark} for {light_dark} mode.\n",
|
||||||
bright_dark = match theme {
|
bright_dark = match theme {
|
||||||
TerminalTheme::Light => "bright",
|
TerminalTheme::Light => "bright",
|
||||||
TerminalTheme::Dark => "dark",
|
TerminalTheme::Dark => "dark",
|
||||||
},
|
},
|
||||||
light_dark = theme.as_ref()
|
light_dark = theme.as_ref()
|
||||||
)
|
);
|
||||||
.context("failed to write message to stdout")?;
|
|
||||||
|
|
||||||
let color_align = ColorAlignment::Horizontal;
|
let color_align = ColorAlignment::Horizontal;
|
||||||
|
|
||||||
|
|
@ -739,14 +714,10 @@ fn create_config(
|
||||||
}
|
}
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
writeln!(
|
println!("\nWhich brightness level looks the best? (Default: {default:.0}% for {light_dark} mode)",
|
||||||
io::stdout(),
|
|
||||||
"\nWhich brightness level looks the best? (Default: {default:.0}% for \
|
|
||||||
{light_dark} mode)",
|
|
||||||
default = f32::from(default_lightness) * 100.0,
|
default = f32::from(default_lightness) * 100.0,
|
||||||
light_dark = theme.as_ref()
|
light_dark = theme.as_ref()
|
||||||
)
|
);
|
||||||
.context("failed to write prompt to stdout")?;
|
|
||||||
let lightness = input(Some("> "))
|
let lightness = input(Some("> "))
|
||||||
.context("failed to read input")?
|
.context("failed to read input")?
|
||||||
.trim()
|
.trim()
|
||||||
|
|
@ -762,8 +733,7 @@ fn create_config(
|
||||||
"&cUnable to parse lightness value, please enter a lightness value such \
|
"&cUnable to parse lightness value, please enter a lightness value such \
|
||||||
as 45%, .45, or 45",
|
as 45%, .45, or 45",
|
||||||
color_mode,
|
color_mode,
|
||||||
)
|
)?;
|
||||||
.context("failed to print message")?;
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -887,8 +857,8 @@ fn create_config(
|
||||||
}
|
}
|
||||||
printc(line.join(" "), color_mode).context("failed to print ascii line")?;
|
printc(line.join(" "), color_mode).context("failed to print ascii line")?;
|
||||||
}
|
}
|
||||||
|
|
||||||
writeln!(io::stdout()).context("failed to write to stdout")?;
|
println!();
|
||||||
}
|
}
|
||||||
|
|
||||||
print_title_prompt(
|
print_title_prompt(
|
||||||
|
|
@ -1011,7 +981,7 @@ fn create_config(
|
||||||
}
|
}
|
||||||
printc(line.join(" "), color_mode).context("failed to print ascii line")?;
|
printc(line.join(" "), color_mode).context("failed to print ascii line")?;
|
||||||
}
|
}
|
||||||
writeln!(io::stdout()).context("failed to write to stdout")?;
|
println!();
|
||||||
}
|
}
|
||||||
|
|
||||||
print_title_prompt(
|
print_title_prompt(
|
||||||
|
|
@ -1020,12 +990,7 @@ fn create_config(
|
||||||
color_mode,
|
color_mode,
|
||||||
)
|
)
|
||||||
.context("failed to print title prompt")?;
|
.context("failed to print title prompt")?;
|
||||||
writeln!(
|
println!("You can choose standard horizontal or vertical alignment, or use one of the random color schemes.\nYou can type \"roll\" to randomize again.\n");
|
||||||
io::stdout(),
|
|
||||||
"You can choose standard horizontal or vertical alignment, or use one of the random \
|
|
||||||
color schemes.\nYou can type \"roll\" to randomize again.\n"
|
|
||||||
)
|
|
||||||
.context("failed to write message to stdout")?;
|
|
||||||
let mut opts: Vec<Cow<str>> = ["horizontal", "vertical", "roll"].map(Into::into).into();
|
let mut opts: Vec<Cow<str>> = ["horizontal", "vertical", "roll"].map(Into::into).into();
|
||||||
opts.extend((0..random_count).map(|i| format!("random{i}").into()));
|
opts.extend((0..random_count).map(|i| format!("random{i}").into()));
|
||||||
let choice = literal_input("Your choice?", &opts[..], "horizontal", true, color_mode)
|
let choice = literal_input("Your choice?", &opts[..], "horizontal", true, color_mode)
|
||||||
|
|
|
||||||
|
|
@ -408,13 +408,8 @@ pub fn printc<S>(msg: S, mode: AnsiMode) -> Result<()>
|
||||||
where
|
where
|
||||||
S: AsRef<str>,
|
S: AsRef<str>,
|
||||||
{
|
{
|
||||||
writeln!(
|
println!("{msg}", msg = color(format!("{msg}&r", msg = msg.as_ref()), mode).context("failed to color message")?);
|
||||||
io::stdout(),
|
Ok(())
|
||||||
"{msg}",
|
|
||||||
msg = color(format!("{msg}&r", msg = msg.as_ref()), mode)
|
|
||||||
.context("failed to color message")?
|
|
||||||
)
|
|
||||||
.context("failed to write message to stdout")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Clears screen using ANSI escape codes.
|
/// Clears screen using ANSI escape codes.
|
||||||
|
|
|
||||||
|
|
@ -113,16 +113,12 @@ where
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(selected) = find_selection(&selection, options) {
|
if let Some(selected) = find_selection(&selection, options) {
|
||||||
writeln!(io::stdout()).context("failed to write to stdout")?;
|
println!();
|
||||||
|
|
||||||
return Ok(selected);
|
return Ok(selected);
|
||||||
} else {
|
} else {
|
||||||
let options_text = options.iter().map(AsRef::as_ref).join("|");
|
let options_text = options.iter().map(AsRef::as_ref).join("|");
|
||||||
writeln!(
|
println!("Invalid selection! {selection} is not one of {options_text}");
|
||||||
io::stdout(),
|
|
||||||
"Invalid selection! {selection} is not one of {options_text}"
|
|
||||||
)
|
|
||||||
.context("failed to write message to stdout")?;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -165,8 +165,7 @@ pub fn start_animation(color_mode: AnsiMode) -> Result<()> {
|
||||||
.rem_euclid(colors.len())]
|
.rem_euclid(colors.len())]
|
||||||
.to_ansi_string(color_mode, ForegroundBackground::Background),
|
.to_ansi_string(color_mode, ForegroundBackground::Background),
|
||||||
fg = fg.to_ansi_string(color_mode, ForegroundBackground::Foreground)
|
fg = fg.to_ansi_string(color_mode, ForegroundBackground::Foreground)
|
||||||
)
|
)?;
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
// Loop over the width
|
// Loop over the width
|
||||||
for x in 0..w.get() {
|
for x in 0..w.get() {
|
||||||
|
|
@ -215,19 +214,9 @@ pub fn start_animation(color_mode: AnsiMode) -> Result<()> {
|
||||||
{
|
{
|
||||||
let c: LinSrgba = c.with_alpha(1.0).into_linear();
|
let c: LinSrgba = c.with_alpha(1.0).into_linear();
|
||||||
let c = Srgb::<u8>::from_linear(c.overlay(black).without_alpha());
|
let c = Srgb::<u8>::from_linear(c.overlay(black).without_alpha());
|
||||||
write!(
|
write!(buf, "{bg}", bg = c.to_ansi_string(color_mode, ForegroundBackground::Background))?;
|
||||||
buf,
|
|
||||||
"{bg}",
|
|
||||||
bg = c.to_ansi_string(color_mode, ForegroundBackground::Background),
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
} else {
|
} else {
|
||||||
write!(
|
write!(buf, "{bg}", bg = c.to_ansi_string(color_mode, ForegroundBackground::Background))?;
|
||||||
buf,
|
|
||||||
"{bg}",
|
|
||||||
bg = c.to_ansi_string(color_mode, ForegroundBackground::Background),
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -240,8 +229,7 @@ pub fn start_animation(color_mode: AnsiMode) -> Result<()> {
|
||||||
.chars()
|
.chars()
|
||||||
.nth(usize::from(x.checked_sub(text_start_x).unwrap()))
|
.nth(usize::from(x.checked_sub(text_start_x).unwrap()))
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
)
|
)?;
|
||||||
.unwrap();
|
|
||||||
} else if y == notice_y && notice_start_x <= x && x < notice_end_x {
|
} else if y == notice_y && notice_start_x <= x && x < notice_end_x {
|
||||||
write!(
|
write!(
|
||||||
buf,
|
buf,
|
||||||
|
|
@ -250,21 +238,15 @@ pub fn start_animation(color_mode: AnsiMode) -> Result<()> {
|
||||||
.chars()
|
.chars()
|
||||||
.nth(usize::from(x.checked_sub(notice_start_x).unwrap()))
|
.nth(usize::from(x.checked_sub(notice_start_x).unwrap()))
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
)
|
)?;
|
||||||
.unwrap();
|
|
||||||
} else {
|
} else {
|
||||||
write!(buf, " ").unwrap();
|
write!(buf, " ")?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// New line if it isn't the last line
|
// New line if it isn't the last line
|
||||||
if y != h.get().checked_sub(1).unwrap() {
|
if y != h.get().checked_sub(1).unwrap() {
|
||||||
writeln!(
|
writeln!(buf, "{reset}", reset = color("&r", color_mode)?)?;
|
||||||
buf,
|
|
||||||
"{reset}",
|
|
||||||
reset = color("&r", color_mode).expect("reset should be valid"),
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue