add palette_glyph for macchina backend

This commit is contained in:
kendy-lovely
2026-04-27 01:26:04 +07:00
parent e64f7efca6
commit 3bf7a79b4c
4 changed files with 159 additions and 7 deletions

View File

@@ -135,6 +135,10 @@ fn main() -> Result<()> {
let backend = options.backend.unwrap_or(config.backend);
let args = options.args.as_ref().or(config.args.as_ref());
#[cfg(feature = "macchina")]
let palette_glyph: Option<&String> = options.palette_glyph.as_ref().or(config.palette_glyph.as_ref());
#[cfg(not(feature = "macchina"))]
let palette_glyph: Option<&String> = None;
fn parse_preset_string(preset_string: &str, config: &Config) -> Result<ColorProfile> {
if preset_string.contains('#') {
@@ -225,7 +229,8 @@ fn main() -> Result<()> {
let asc = asc
.to_recolored(&color_align, &color_profile, color_mode, theme)
.context("failed to recolor ascii")?;
neofetch_util::run(asc, backend, args)?;
neofetch_util::run(asc, backend, args, palette_glyph)?;
if options.ask_exit {
input(Some("Press enter to exit...")).context("failed to read input")?;
@@ -1231,6 +1236,101 @@ fn create_config(
backend.as_ref(),
);
//////////////////////////////
// 7.5. If using macchina, ask for custom palette glyph
#[cfg(feature = "macchina")]
let mut palette_glyph: Option<String> = None;
#[cfg(feature = "macchina")]
if backend == Backend::Macchina {
clear_screen(Some(&title), color_mode, debug_mode)
.context("failed to clear screen")?;
print_title_prompt(option_counter, "Create a glyph for macchina (leave empty for None):");
let color_palette: [&str; 8] = match theme {
TerminalTheme::Dark => ["&0","&4","&2","&6","&1","&5","&3","&8"],
TerminalTheme::Light => ["&8", "&c", "&a", "&e", "&9", "&d", "&b", "&f"]
};
let print_palette = |glyph: &str, palette_list: [&str; 8]| -> Result<()> {
clear_screen(Some(&title), color_mode, debug_mode)
.context("failed to clear screen")?;
print_title_prompt(option_counter, "Create a glyph for macchina (leave empty for None):");
let palette_for_print: String = palette_list
.into_iter()
.map(|s| s.to_owned() + glyph + "&r" )
.collect();
printc!("Preview:\n{}", palette_for_print);
print!("> {glyph}");
io::stdout().flush().context("failed to flush preset prompt")?;
Ok(())
};
let mut raw_mode = RawModeGuard::new().context("failed to initialize raw input mode")?;
let mut entered_glyph = String::new();
loop {
raw_mode
.disable()
.context("failed to disable raw mode for rendering")?;
print_palette(&entered_glyph, color_palette).context("failed to clear screen during glyph input")?;
raw_mode
.enable()
.context("failed to enable raw mode for key input")?;
let event = event::read().context("failed to read keyboard event")?;
let Event::Key(key) = event else {
continue;
};
if !matches!(key.kind, KeyEventKind::Press | KeyEventKind::Repeat) {
continue;
}
match key.code {
KeyCode::Enter => {
break;
},
KeyCode::Backspace => {
entered_glyph.pop();
},
KeyCode::Esc => {
entered_glyph.clear();
},
KeyCode::Char('c') if key.modifiers.contains(KeyModifiers::CONTROL) => {
raw_mode
.disable()
.context("failed to disable raw mode before interrupting")?;
println!();
return Err(anyhow::anyhow!("interrupted by user"));
},
KeyCode::Char(c)
if !key.modifiers.contains(KeyModifiers::CONTROL)
&& !key.modifiers.contains(KeyModifiers::ALT) =>
{
entered_glyph.push(c);
},
_ => {},
}
}
raw_mode
.disable()
.context("failed to disable raw mode after preset selection")?;
palette_glyph = if !entered_glyph.is_empty() {
Some(entered_glyph)
} else { None };
update_title(
&mut title,
&mut option_counter,
"Selected glyph",
&palette_glyph.as_ref().unwrap_or(&String::from("None")),
);
}
//////////////////////////////
// 8. Custom ASCII file
let mut custom_ascii_path: Option<String> = None;
@@ -1292,6 +1392,8 @@ fn create_config(
pride_month_disable: false,
custom_ascii_path,
custom_presets: None,
#[cfg(feature = "macchina")]
palette_glyph
};
debug!(?config, "created config");

View File

@@ -32,6 +32,8 @@ pub struct Options {
pub test_print: bool,
pub ask_exit: bool,
pub auto_detect_light_dark: Option<bool>,
#[cfg(feature = "macchina")]
pub palette_glyph: Option<String>
}
pub fn options() -> OptionParser<Options> {
@@ -153,7 +155,14 @@ BACKEND={{{backends}}}",
.argument("BOOL")
.optional();
construct!(Options {
#[cfg(feature = "macchina")]
let palette_glyph = long("palette-glyph")
.help("Sets the glyph to be used for the macchina backend")
.argument("STR")
.optional();
#[cfg(feature = "macchina")]
return construct!(Options {
config,
config_file,
preset,
@@ -171,6 +180,7 @@ BACKEND={{{backends}}}",
test_print,
ask_exit,
auto_detect_light_dark,
palette_glyph
})
.to_options()
.header(
@@ -180,7 +190,37 @@ BACKEND={{{backends}}}",
)
.expect("header should not contain invalid color codes"),
)
.version(env!("CARGO_PKG_VERSION"))
.version(env!("CARGO_PKG_VERSION"));
#[cfg(not(feature = "macchina"))]
return construct!(Options {
config,
config_file,
preset,
mode,
backend,
args,
scale,
lightness,
june,
debug,
distro,
ascii_file,
print_font_logo,
// hidden
test_print,
ask_exit,
auto_detect_light_dark
})
.to_options()
.header(
&*color(
"&l&bhyfetch&~&L - neofetch with flags <3",
AnsiMode::Ansi256,
)
.expect("header should not contain invalid color codes"),
)
.version(env!("CARGO_PKG_VERSION"));
}
#[cfg(feature = "autocomplete")]

View File

@@ -24,6 +24,8 @@ pub struct Config {
pub pride_month_disable: bool,
pub custom_ascii_path: Option<String>,
pub custom_presets: Option<HashMap<String, Vec<String>>>,
#[cfg(feature = "macchina")]
pub palette_glyph: Option<String>
}
impl Config {

View File

@@ -1,6 +1,5 @@
use std::borrow::Cow;
use std::ffi::OsStr;
#[cfg(feature = "macchina")]
use std::fs;
use std::io::{Write as _};
use std::path::PathBuf;
@@ -33,7 +32,10 @@ use crate::ascii::{RawAsciiArt, RecoloredAsciiArt};
use crate::color_util::{printc, NeofetchAsciiIndexedColor, PresetIndexedColor};
use crate::distros::Distro;
use crate::types::{AnsiMode, Backend};
#[cfg(feature = "macchina")]
use crate::utils::{find_in_path, get_cache_path, input, process_command_status};
#[cfg(not(feature = "macchina"))]
use crate::utils::{get_cache_path, input, process_command_status};
pub const TEST_ASCII: &str = r####################"
### |\___/| ###
@@ -273,14 +275,14 @@ where
}
#[tracing::instrument(level = "debug", skip(asc), fields(asc.w = asc.w, asc.h = asc.h))]
pub fn run(asc: RecoloredAsciiArt, backend: Backend, args: Option<&Vec<String>>) -> Result<()> {
pub fn run(asc: RecoloredAsciiArt, backend: Backend, args: Option<&Vec<String>>, palette_glyph: Option<&String>) -> Result<()> {
let asc = asc.lines.join("\n");
match backend {
Backend::Neofetch => run_neofetch(asc, args).context("failed to run neofetch")?,
Backend::Fastfetch => run_fastfetch(asc, args).context("failed to run fastfetch")?,
#[cfg(feature = "macchina")]
Backend::Macchina => run_macchina(asc, args).context("failed to run macchina")?,
Backend::Macchina => run_macchina(asc, args, palette_glyph).context("failed to run macchina")?,
}
Ok(())
@@ -664,7 +666,7 @@ fn run_fastfetch(asc: String, args: Option<&Vec<String>>) -> Result<()> {
/// Runs macchina with custom ascii art.
#[cfg(feature = "macchina")]
#[tracing::instrument(level = "debug", skip(asc))]
fn run_macchina(asc: String, args: Option<&Vec<String>>) -> Result<()> {
fn run_macchina(asc: String, args: Option<&Vec<String>>, palette_glyph: Option<&String>) -> Result<()> {
// Write ascii art to temp file
let asc_file_path = {
let mut temp_file = tempfile::Builder::new()
@@ -695,6 +697,12 @@ fn run_macchina(asc: String, args: Option<&Vec<String>>) -> Result<()> {
"path",
&*asc_file_path.to_string_lossy(),
)]));
if let Some(palette_glyph) = palette_glyph {
doc["palette"] = Item::Table(Table::from_iter([
("glyph", value(palette_glyph)),
("visible", value(true))
]))
}
doc
};
debug!(%theme_doc, "macchina theme");