Implement select_lightness

This commit is contained in:
Teoh Han Hui 2024-07-14 04:59:37 +08:00
parent da5f6ef655
commit a5de091883
No known key found for this signature in database
GPG key ID: D43E2BABAF97DCAE
6 changed files with 249 additions and 74 deletions

View file

@ -25,7 +25,10 @@ impl AsciiDistro {
fn main() { fn main() {
let neofetch_path = Path::new(env!("CARGO_WORKSPACE_DIR")).join("neofetch"); let neofetch_path = Path::new(env!("CARGO_WORKSPACE_DIR")).join("neofetch");
println!("cargo:rerun-if-changed={}", neofetch_path.display()); println!(
"cargo:rerun-if-changed={neofetch_path}",
neofetch_path = neofetch_path.display()
);
let out_dir = env::var_os("OUT_DIR").unwrap(); let out_dir = env::var_os("OUT_DIR").unwrap();
let out_path = Path::new(&out_dir); let out_path = Path::new(&out_dir);

View file

@ -5,13 +5,14 @@ use std::io::{self, IsTerminal, Read, Write};
use std::path::Path; use std::path::Path;
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use deranged::RangedU8;
use hyfetch::cli_options::options; use hyfetch::cli_options::options;
use hyfetch::color_util::{clear_screen, color, printc, ForegroundBackground, Theme}; use hyfetch::color_util::{clear_screen, color, printc, ForegroundBackground, Lightness, Theme};
use hyfetch::models::Config; use hyfetch::models::Config;
#[cfg(windows)] #[cfg(windows)]
use hyfetch::neofetch_util::ensure_git_bash; use hyfetch::neofetch_util::ensure_git_bash;
use hyfetch::neofetch_util::{self, ascii_size, get_distro_ascii, ColorAlignment}; use hyfetch::neofetch_util::{self, ascii_size, get_distro_ascii, ColorAlignment};
use hyfetch::presets::{AssignLightness, ColorProfile, Preset}; use hyfetch::presets::{AssignLightness, Preset};
use hyfetch::types::{AnsiMode, Backend, TerminalTheme}; use hyfetch::types::{AnsiMode, Backend, TerminalTheme};
use hyfetch::utils::get_cache_path; use hyfetch::utils::get_cache_path;
use palette::Srgb; use palette::Srgb;
@ -20,6 +21,22 @@ use terminal_colorsaurus::{background_color, QueryOptions};
use terminal_size::terminal_size; use terminal_size::terminal_size;
use time::{Month, OffsetDateTime}; use time::{Month, OffsetDateTime};
use tracing::debug; use tracing::debug;
use unicode_segmentation::UnicodeSegmentation;
const TEST_ASCII: &str = r####################"
### |\___/| ###
### ) ( ###
## =\ /= ##
#### )===( ####
### / \ ###
### | | ###
## / {txt} \ ##
## \ / ##
_/\_\_ _/_/\_
|##| ( ( |##|
|##| ) ) |##|
|##| (_( |##|
"####################;
fn main() -> Result<()> { fn main() -> Result<()> {
#[cfg(windows)] #[cfg(windows)]
@ -81,7 +98,7 @@ fn main() -> Result<()> {
let now = let now =
OffsetDateTime::now_local().context("failed to get current datetime in local timezone")?; OffsetDateTime::now_local().context("failed to get current datetime in local timezone")?;
let cache_path = get_cache_path().context("failed to get cache path")?; let cache_path = get_cache_path().context("failed to get cache path")?;
let june_path = cache_path.join(format!("animation-displayed-{}", now.year())); let june_path = cache_path.join(format!("animation-displayed-{year}", year = now.year()));
let show_pride_month = options.june let show_pride_month = options.june
|| now.month() == Month::June && !june_path.is_file() && io::stdout().is_terminal(); || now.month() == Month::June && !june_path.is_file() && io::stdout().is_terminal();
@ -144,7 +161,7 @@ fn main() -> Result<()> {
config.color_align config.color_align
}; };
let asc = color_align let asc = color_align
.recolor_ascii(asc, color_profile, color_mode, theme) .recolor_ascii(asc, &color_profile, color_mode, theme)
.context("failed to recolor ascii")?; .context("failed to recolor ascii")?;
neofetch_util::run(asc, backend, args)?; neofetch_util::run(asc, backend, args)?;
@ -252,10 +269,7 @@ fn create_config(
} else { } else {
format!("{k}:").into() format!("{k}:").into()
}; };
title.push_str({ title.push_str(&format!("\n&e{option_counter}. {k:<30} &~{v}"));
let pad = " ".repeat(30 - k.len());
&format!("\n&e{option_counter}. {k}{pad} &~{v}")
});
*option_counter += 1; *option_counter += 1;
} }
@ -270,9 +284,9 @@ fn create_config(
// TODO // TODO
////////////////////////////// //////////////////////////////
// 1. Select color system // 1. Select color mode
let select_color_system = || -> Result<(AnsiMode, &str)> { let select_color_mode = || -> Result<(AnsiMode, &str)> {
if det_ansi == Some(AnsiMode::Rgb) { if det_ansi == Some(AnsiMode::Rgb) {
return Ok((AnsiMode::Rgb, "Detected color mode")); return Ok((AnsiMode::Rgb, "Detected color mode"));
} }
@ -299,10 +313,10 @@ fn create_config(
}; };
let color_mode = { let color_mode = {
let (color_system, ttl) = select_color_system().context("failed to select color system")?; let (color_mode, ttl) = select_color_mode().context("failed to select color mode")?;
debug!(?color_system, "selected color mode"); debug!(?color_mode, "selected color mode");
update_title(&mut title, &mut option_counter, ttl, color_system.into()); update_title(&mut title, &mut option_counter, ttl, color_mode.into());
color_system color_mode
}; };
////////////////////////////// //////////////////////////////
@ -320,10 +334,10 @@ fn create_config(
}; };
let theme = { let theme = {
let (selected_theme, ttl) = select_theme().context("failed to select theme")?; let (theme, ttl) = select_theme().context("failed to select theme")?;
debug!(?selected_theme, "selected theme"); debug!(?theme, "selected theme");
update_title(&mut title, &mut option_counter, ttl, selected_theme.into()); update_title(&mut title, &mut option_counter, ttl, theme.into());
selected_theme theme
}; };
////////////////////////////// //////////////////////////////
@ -354,12 +368,7 @@ fn create_config(
.with_context(|| format!("failed to color flag using preset: {preset:?}"))?; .with_context(|| format!("failed to color flag using preset: {preset:?}"))?;
let name = { let name = {
let name: &'static str = preset.into(); let name: &'static str = preset.into();
let name_len = name.chars().count(); format!("{name:^spacing$}", spacing = spacing as usize)
let name_len: u8 = name_len.try_into().expect("`name_len` should fit in `u8`");
let pad_start = " ".repeat(((spacing - name_len) / 2) as usize);
let pad_end =
" ".repeat(((spacing - name_len) / 2 + (spacing - name_len) % 2) as usize);
format!("{pad_start}{name}{pad_end}")
}; };
flags.push([name, flag.clone(), flag.clone(), flag]); flags.push([name, flag.clone(), flag.clone(), flag]);
} }
@ -422,7 +431,6 @@ fn create_config(
println!(); println!();
}; };
let color_profile: ColorProfile;
let preset_rainbow = Preset::Rainbow let preset_rainbow = Preset::Rainbow
.color_profile() .color_profile()
.with_lightness_adaptive(Config::default_lightness(theme), theme, use_overlay) .with_lightness_adaptive(Config::default_lightness(theme), theme, use_overlay)
@ -434,6 +442,9 @@ fn create_config(
) )
.expect("coloring text with rainbow preset should not fail"); .expect("coloring text with rainbow preset should not fail");
let preset: Preset;
let color_profile;
let mut page: u8 = 0; let mut page: u8 = 0;
loop { loop {
print_flag_page(&pages[page as usize], page); print_flag_page(&pages[page as usize], page);
@ -447,7 +458,10 @@ fn create_config(
} }
println!("Enter 'next' to go to the next page and 'prev' to go to the previous page."); println!("Enter 'next' to go to the next page and 'prev' to go to the previous page.");
let selection = literal_input( let selection = literal_input(
format!("Which {preset_rainbow} do you want to use? "), format!(
"Which {preset} do you want to use? ",
preset = preset_rainbow
),
&opts[..], &opts[..],
Preset::Rainbow.into(), Preset::Rainbow.into(),
false, false,
@ -459,28 +473,23 @@ fn create_config(
} else if selection == "prev" { } else if selection == "prev" {
page -= 1; page -= 1;
} else { } else {
let selected_preset: Preset = preset = selection.parse().expect("selected preset should be valid");
selection.parse().expect("selected preset should be valid"); debug!(?preset, "selected preset");
debug!(?selected_preset, "selected preset"); color_profile = preset.color_profile();
color_profile = selected_preset.color_profile();
{
let preset_name: &'static str = selected_preset.into();
let preset_colored_name = color_profile
.with_lightness_adaptive(Config::default_lightness(theme), theme, use_overlay)
.color_text(
preset_name,
color_mode,
ForegroundBackground::Foreground,
false,
)
.expect("coloring text with selected preset should not fail");
update_title( update_title(
&mut title, &mut title,
&mut option_counter, &mut option_counter,
"Selected flag", "Selected flag",
&preset_colored_name, &color_profile
.with_lightness_adaptive(Config::default_lightness(theme), theme, use_overlay)
.color_text(
<&'static str>::from(preset),
color_mode,
ForegroundBackground::Foreground,
false,
)
.expect("coloring text with selected preset should not fail"),
); );
}
break; break;
} }
} }
@ -488,7 +497,165 @@ fn create_config(
////////////////////////////// //////////////////////////////
// 4. Dim/lighten colors // 4. Dim/lighten colors
// TODO let test_ascii = &TEST_ASCII[1..(TEST_ASCII.len() - 1)];
let Some(test_ascii_width) = test_ascii
.split('\n')
.map(|line| line.graphemes(true).count())
.max()
else {
unreachable!();
};
let test_ascii_width: u8 = test_ascii_width
.try_into()
.expect("`test_ascii_width` should fit in `u8`");
let test_ascii_height = test_ascii.split('\n').count();
let test_ascii_height: u8 = test_ascii_height
.try_into()
.expect("`test_ascii_height` should fit in `u8`");
let select_lightness = || -> Result<Lightness> {
clear_screen(Some(&title), color_mode, debug_mode)
.expect("title should not contain invalid color codes");
print_title_prompt(
option_counter,
"Let's adjust the color brightness!",
color_mode,
);
printc(
format!(
"The colors might be a little bit too {bright_dark} for {light_dark} mode.",
bright_dark = match theme {
TerminalTheme::Light => "bright",
TerminalTheme::Dark => "dark",
},
light_dark = <&'static str>::from(theme)
),
color_mode,
)
.expect("message should not contain invalid color codes");
println!();
// Print cats
{
let (term_w, _) = terminal_size().context("failed to get terminal size")?;
let num_cols = cmp::max(1, term_w.0 / (test_ascii_width as u16 + 2));
let num_cols: u8 = num_cols.try_into().expect("`num_cols` should fit in `u8`");
const MIN: f32 = 0.15;
const MAX: f32 = 0.85;
let ratios =
(0..num_cols)
.map(|col| col as f32 / num_cols as f32)
.map(|r| match theme {
TerminalTheme::Light => r * (MAX - MIN) / 2.0 + MIN,
TerminalTheme::Dark => (r * (MAX - MIN) + (MAX + MIN)) / 2.0,
});
let row: Vec<Vec<String>> = ratios
.map(|r| {
let color_align = ColorAlignment::Horizontal { fore_back: None };
let asc = color_align
.recolor_ascii(
test_ascii.replace(
"{txt}",
&format!(
"{lightness:^5}",
lightness = format!("{lightness:.0}%", lightness = r * 100.0)
),
),
&color_profile.with_lightness_adaptive(
Lightness::new(r)
.expect("generated lightness should not be invalid"),
theme,
use_overlay,
),
color_mode,
theme,
)
.expect("recoloring test ascii should not fail");
asc.split('\n').map(ToOwned::to_owned).collect::<Vec<_>>()
})
.collect();
for i in 0..(test_ascii_height as usize) {
let mut line = String::new();
for lines in &row {
line.push_str(&lines[i]);
line.push_str(" ");
}
printc(line, color_mode)
.expect("test ascii line should not contain invalid color codes");
}
}
let default_lightness = Config::default_lightness(theme);
let parse_lightness = |lightness: String| -> Result<Lightness> {
if lightness.is_empty() || ["unset", "none"].contains(&&*lightness) {
return Ok(default_lightness);
}
let lightness = if let Some(lightness) = lightness.strip_suffix('%') {
let lightness: RangedU8<0, 100> = lightness.parse()?;
lightness.get() as f32 / 100.0
} else {
match lightness.parse::<RangedU8<0, 100>>() {
Ok(lightness) => lightness.get() as f32 / 100.0,
Err(_) => lightness.parse::<f32>()?,
}
};
Ok(Lightness::new(lightness)?)
};
loop {
println!();
printc(
format!(
"Which brightness level looks the best? (Default: {default:.0}% for \
{light_dark} mode)",
default = f32::from(default_lightness) * 100.0,
light_dark = <&'static str>::from(theme)
),
color_mode,
)
.expect("prompt should not contain invalid color codes");
let lightness = {
let mut buf = String::new();
print!("> ");
io::stdout().flush()?;
io::stdin()
.read_line(&mut buf)
.context("failed to read line from input")?;
buf.trim().to_lowercase()
};
match parse_lightness(lightness) {
Ok(lightness) => {
return Ok(lightness);
},
Err(err) => {
debug!(%err, "could not parse lightness");
printc(
"&cUnable to parse lightness value, please enter a lightness value such \
as 45%, .45, or 45",
color_mode,
)
.expect("message should not contain invalid color codes");
},
}
}
};
let lightness = select_lightness().context("failed to select lightness")?;
debug!(?lightness, "selected lightness");
let color_profile = color_profile.with_lightness_adaptive(lightness, theme, use_overlay);
update_title(
&mut title,
&mut option_counter,
"Selected brightness",
&format!("{lightness:.2}", lightness = f32::from(lightness)),
);
//////////////////////////////
// 5. Color arrangement
todo!() todo!()
} }

View file

@ -53,8 +53,8 @@ pub fn options() -> OptionParser<Options> {
.short('p') .short('p')
.help(&*format!( .help(&*format!(
"Use preset "Use preset
PRESET={{{}}}", PRESET={{{presets}}}",
Preset::VARIANTS.join(",") presets = Preset::VARIANTS.join(",")
)) ))
.argument("PRESET"); .argument("PRESET");
#[cfg(feature = "autocomplete")] #[cfg(feature = "autocomplete")]
@ -62,7 +62,10 @@ PRESET={{{}}}",
let preset = preset let preset = preset
.parse(|s| { .parse(|s| {
Preset::from_str(&s).with_context(|| { Preset::from_str(&s).with_context(|| {
format!("PRESET should be one of {{{}}}", Preset::VARIANTS.join(",")) format!(
"PRESET should be one of {{{presets}}}",
presets = Preset::VARIANTS.join(",")
)
}) })
}) })
.optional(); .optional();
@ -70,8 +73,8 @@ PRESET={{{}}}",
.short('m') .short('m')
.help(&*format!( .help(&*format!(
"Color mode "Color mode
MODE={{{}}}", MODE={{{modes}}}",
AnsiMode::VARIANTS.join(",") modes = AnsiMode::VARIANTS.join(",")
)) ))
.argument("MODE"); .argument("MODE");
#[cfg(feature = "autocomplete")] #[cfg(feature = "autocomplete")]
@ -79,7 +82,10 @@ MODE={{{}}}",
let mode = mode let mode = mode
.parse(|s| { .parse(|s| {
AnsiMode::from_str(&s).with_context(|| { AnsiMode::from_str(&s).with_context(|| {
format!("MODE should be one of {{{}}}", AnsiMode::VARIANTS.join(",")) format!(
"MODE should be one of {{{modes}}}",
modes = AnsiMode::VARIANTS.join(",")
)
}) })
}) })
.optional(); .optional();
@ -87,8 +93,8 @@ MODE={{{}}}",
.short('b') .short('b')
.help(&*format!( .help(&*format!(
"Choose a *fetch backend "Choose a *fetch backend
BACKEND={{{}}}", BACKEND={{{backends}}}",
Backend::VARIANTS.join(",") backends = Backend::VARIANTS.join(",")
)) ))
.argument("BACKEND"); .argument("BACKEND");
#[cfg(feature = "autocomplete")] #[cfg(feature = "autocomplete")]
@ -97,8 +103,8 @@ BACKEND={{{}}}",
.parse(|s| { .parse(|s| {
Backend::from_str(&s).with_context(|| { Backend::from_str(&s).with_context(|| {
format!( format!(
"BACKEND should be one of {{{}}}", "BACKEND should be one of {{{backends}}}",
Backend::VARIANTS.join(",") backends = Backend::VARIANTS.join(",")
) )
}) })
}) })

View file

@ -66,9 +66,9 @@ pub struct Lightness(f32);
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum LightnessError { pub enum LightnessError {
#[error( #[error(
"invalid lightness {0}, expected value between {} and {}", "invalid lightness {0}, expected value between {min} and {max}",
Lightness::MIN, min = Lightness::MIN,
Lightness::MAX max = Lightness::MAX
)] )]
OutOfRange(f32), OutOfRange(f32),
} }
@ -346,8 +346,8 @@ where
let msg = msg.as_ref(); let msg = msg.as_ref();
println!( println!(
"{}", "{msg}",
color(format!("{msg}&r"), mode).context("failed to color message")? msg = color(format!("{msg}&r"), mode).context("failed to color message")?
); );
Ok(()) Ok(())

View file

@ -78,13 +78,16 @@ impl ColorAlignment {
/// Uses the color alignment to recolor an ascii art. /// Uses the color alignment to recolor an ascii art.
#[tracing::instrument(level = "debug", skip(asc))] #[tracing::instrument(level = "debug", skip(asc))]
pub fn recolor_ascii( pub fn recolor_ascii<S>(
&self, &self,
asc: String, asc: S,
color_profile: ColorProfile, color_profile: &ColorProfile,
color_mode: AnsiMode, color_mode: AnsiMode,
theme: TerminalTheme, theme: TerminalTheme,
) -> Result<String> { ) -> Result<String>
where
S: AsRef<str>,
{
let reset = color("&~&*", color_mode).expect("color reset should not be invalid"); let reset = color("&~&*", color_mode).expect("color reset should not be invalid");
let asc = match self { let asc = match self {
@ -94,15 +97,12 @@ impl ColorAlignment {
| &Self::Vertical { | &Self::Vertical {
fore_back: Some((fore, back)), fore_back: Some((fore, back)),
} => { } => {
let fore: u8 = fore.into();
let back: u8 = back.into();
let asc = fill_starting(asc) let asc = fill_starting(asc)
.context("failed to fill in starting neofetch color codes")?; .context("failed to fill in starting neofetch color codes")?;
// Replace foreground colors // Replace foreground colors
let asc = asc.replace( let asc = asc.replace(
&format!("${{c{fore}}}"), &format!("${{c{fore}}}", fore = u8::from(fore)),
&color( &color(
match theme { match theme {
TerminalTheme::Light => "&0", TerminalTheme::Light => "&0",
@ -129,7 +129,7 @@ impl ColorAlignment {
let mut asc = String::new(); let mut asc = String::new();
for (i, line) in lines.into_iter().enumerate() { for (i, line) in lines.into_iter().enumerate() {
let line = line.replace( let line = line.replace(
&format!("${{c{back}}}"), &format!("${{c{back}}}", back = u8::from(back)),
&colors[i].to_ansi_string(color_mode, { &colors[i].to_ansi_string(color_mode, {
// note: this is "background" in the ascii art, but foreground // note: this is "background" in the ascii art, but foreground
// text in terminal // text in terminal
@ -170,7 +170,7 @@ impl ColorAlignment {
.get_or_init(|| AhoCorasick::new(NEOFETCH_COLOR_PATTERNS).unwrap()); .get_or_init(|| AhoCorasick::new(NEOFETCH_COLOR_PATTERNS).unwrap());
const N: usize = NEOFETCH_COLOR_PATTERNS.len(); const N: usize = NEOFETCH_COLOR_PATTERNS.len();
const REPLACEMENTS: [&str; N] = [""; N]; const REPLACEMENTS: [&str; N] = [""; N];
ac.replace_all(&asc, &REPLACEMENTS) ac.replace_all(asc.as_ref(), &REPLACEMENTS)
}; };
let lines: Vec<_> = asc.split('\n').collect(); let lines: Vec<_> = asc.split('\n').collect();
@ -301,9 +301,8 @@ pub fn neofetch_path() -> Result<Option<PathBuf>> {
)); ));
}, },
Err(err) => { Err(err) => {
return Err(err).with_context(|| { return Err(err)
format!("failed to check for existence of {workspace_path:?}") .with_context(|| format!("failed to check existence of {workspace_path:?}"));
});
}, },
}; };
let neofetch_path = workspace_path.join("neofetch"); let neofetch_path = workspace_path.join("neofetch");

View file

@ -95,8 +95,8 @@ pub fn process_command_status(status: &ExitStatus) -> Result<()> {
#[cfg(unix)] #[cfg(unix)]
{ {
anyhow!( anyhow!(
"child process terminated by signal: {}", "child process terminated by signal: {signal}",
status signal = status
.signal() .signal()
.expect("either one of status code or signal should be set") .expect("either one of status code or signal should be set")
) )