use std::path::Path;
use clap::Parser;
use anyhow::Result;
use owo_colors::OwoColorize;
use wallust::{
args, cache, config::{self, WalStr}, gen_colors, themes
};
const ISSUE: &str = "please report this at <https://codeberg.org/explosion-mental/wallust/issues>";
fn main() -> Result<()> {
let cli = args::Cli::parse();
let info = "I".blue();
let info = info.bold();
let Some(cache_path) = dirs::cache_dir() else {
anyhow::bail!("The cache path for the platform could not be found, {ISSUE}");
};
let quiet = cli.globals.quiet;
let skip_templates = &cli.globals.skip_templates;
let mut conf = config::Config::new(&cli.globals)?;
match cli.subcmds {
args::Subcmds::Run(s) => {
run(&mut conf, &cache_path, &s, &cli.globals)?
},
args::Subcmds::Pywal(s) => {
match s.file {
Some(_) => run(&mut conf, &cache_path, &s.into(), &cli.globals)?, None => { let theme = &s.theme.expect("SHOULD BE NON EMPTY, from clap");
if !quiet { println!("[{info}] {}: Using {theme}", "theme".magenta().bold(), theme = theme.italic()); }
let colors = themes::built_in_theme(theme, quiet).ok_or_else(||anyhow::anyhow!("Theme not found. Quitting..."))?;
colors.print();
cli.globals.set_seq(&colors, &cache_path)?;
cli.globals.update_cur(&colors)?;
if ! skip_templates { conf.write_entry(&WalStr::Theme(theme.to_owned()), &colors, quiet)?; }
}
}
},
#[cfg(feature = "themes")]
args::Subcmds::Theme { theme, preview } => {
if theme == themes::LIST { if !quiet { themes::list_themes(); }
return Ok(())
}
if !quiet && !preview { println!("[{info}] {}: Using {theme}", "theme".magenta().bold(), theme = theme.italic()); }
let colors = themes::built_in_theme(&theme, quiet).ok_or_else(||anyhow::anyhow!("Theme not found. Quitting..."))?;
if ! quiet {
colors.print();
if preview { return Ok(()); } }
cli.globals.set_seq(&colors, &cache_path)?;
cli.globals.update_cur(&colors)?;
if ! skip_templates { conf.write_entry(&WalStr::Theme(theme), &colors, quiet)?; }
if ! quiet { colors.done() }
},
args::Subcmds::Cs { colorscheme, format } => {
let (walstr, colors) = themes::search_theme_or_cs(&colorscheme, quiet, &conf.dir, format)?;
let msg = match walstr {
WalStr::Path(ref p) => format!("Using a colorscheme from file {}", p.display()),
WalStr::Theme(ref p) => format!("Using the theme {p}"),
};
if ! quiet { println!("[{info}] {cs}: {msg}", cs = "colorscheme".magenta().bold()); }
if ! quiet { colors.print(); }
cli.globals.set_seq(&colors, &cache_path)?;
cli.globals.update_cur(&colors)?;
if ! skip_templates { conf.write_entry(&walstr, &colors, quiet)?;
}
if ! quiet { colors.done() }
},
args::Subcmds::Debug => {
use cache::CACHE_VER;
println!(
"Cache version: {CACHE_VER}
Cache path: {}
{conf}
~ make sure to report any issue at <https://codeberg.org/explosion-mental/wallust/issues> ~ ",
cache_path.display(),
);
},
args::Subcmds::Migrate => wallust::args::migrate(&conf)?,
}
Ok(())
}
fn run(conf: &mut config::Config, cache_path: &Path, cli: &args::WallustArgs, g: &args::Globals) -> Result<()> {
let info = "I".blue();
let info = info.bold();
let quiet = g.quiet || cli.print_scheme;
conf.customs_cli(cli);
conf.true_th = conf.threshold.unwrap_or_default();
if !quiet {
let f = match cli.file.file_name() {
Some(s) => s.to_string_lossy(),
None => cli.file.to_string_lossy(),
};
println!("[{info}] {img}: {f}", img = "image".magenta().bold());
conf.print();
}
if !quiet && cli.overwrite_cache { println!("[{info}] {c}: Overwriting cache, if present, `-w` flag provided.", c = "cache".magenta().bold()); }
let colors = gen_colors(&cli.file, conf, cli.dynamic_threshold, cache_path, cli.no_cache, quiet, cli.overwrite_cache)?;
if !quiet { colors.print(); }
g.set_seq(&colors, cache_path)?;
g.update_cur(&colors)?;
if !g.skip_templates { conf.write_entry(&WalStr::Path(cli.file.clone()), &colors, quiet)?; }
if !conf.no_hooks { conf.run_hooks(quiet); }
if !quiet && cli.no_cache { println!("[{info}] {}: Skipping caching the palette, `-n` flag provided.", "cache".magenta().bold()); }
if !cli.no_cache && !quiet { println!("[{info}] {}: Saving scheme to cache.", "cache".magenta().bold()); }
if cli.save_scheme {
let f = match cli.file.file_prefix() {
Some(s) => s.to_string_lossy(),
None => cli.file.to_string_lossy(),
};
let p = conf.dir.join("colorschemes").join(format!("{f}.json"));
cache::write_json(p, &colors, &conf.dir.display().to_string(), true)?;
if !quiet { println!("[{info}] {} into colorscheme directory.", "Saving Scheme".bold()); }
}
if cli.print_scheme { colors.output_nl(); }
if !quiet { colors.done(); }
Ok(())
}