diff --git a/Cargo.toml b/Cargo.toml index 7b0527c..6a8be3f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -50,3 +50,4 @@ unicode-width = "0.1.10" [build-dependencies] clap = { version = "4.5.9", features = ["derive", "wrap_help"], optional = true } clap_complete = { version = "4.5.8", optional = true } +colorgrad = { version = "0.7.0" } diff --git a/src/cli.rs b/src/cli.rs index 7692922..5af9245 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -19,6 +19,39 @@ pub enum Gradient { Warm, } +impl Gradient { + pub fn to_gradient(&self) -> Box { + match self { + Gradient::Cividis => Box::new(colorgrad::preset::cividis()), + Gradient::Cool => Box::new(colorgrad::preset::cool()), + Gradient::Cubehelix => Box::new(colorgrad::preset::cubehelix_default()), + Gradient::Inferno => Box::new(colorgrad::preset::inferno()), + Gradient::Magma => Box::new(colorgrad::preset::magma()), + Gradient::Plasma => Box::new(colorgrad::preset::plasma()), + Gradient::Rainbow => Box::new(colorgrad::preset::rainbow()), + Gradient::RdYlGn => Box::new(colorgrad::preset::rd_yl_gn()), + Gradient::Sinebow => Box::new(colorgrad::preset::sinebow()), + Gradient::Spectral => Box::new(colorgrad::preset::spectral()), + Gradient::Turbo => Box::new(colorgrad::preset::turbo()), + Gradient::Viridis => Box::new(colorgrad::preset::viridis()), + Gradient::Warm => Box::new(colorgrad::preset::warm()), + Gradient::Fruits => build_gradient(&[ + "#00c21c", "#009dc9", "#ffd43e", "#ff2a70", "#b971ff", "#7ce300", "#feff62", + ]), + } + } +} + +fn build_gradient(colors: &[&str]) -> Box { + Box::new( + colorgrad::GradientBuilder::new() + .html_colors(colors) + .mode(colorgrad::BlendMode::Oklab) + .build::() + .unwrap(), + ) +} + #[derive(Clone, Debug, Parser)] #[command( name = "lolcrab", diff --git a/src/lib.rs b/src/lib.rs index 6b8ca61..ca4d3da 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -351,24 +351,7 @@ impl From for Lolcrab { .unwrap(), ) } else { - match cmd.gradient { - Gradient::Cividis => Box::new(colorgrad::preset::cividis()), - Gradient::Cool => Box::new(colorgrad::preset::cool()), - Gradient::Cubehelix => Box::new(colorgrad::preset::cubehelix_default()), - Gradient::Inferno => Box::new(colorgrad::preset::inferno()), - Gradient::Magma => Box::new(colorgrad::preset::magma()), - Gradient::Plasma => Box::new(colorgrad::preset::plasma()), - Gradient::Rainbow => Box::new(colorgrad::preset::rainbow()), - Gradient::RdYlGn => Box::new(colorgrad::preset::rd_yl_gn()), - Gradient::Sinebow => Box::new(colorgrad::preset::sinebow()), - Gradient::Spectral => Box::new(colorgrad::preset::spectral()), - Gradient::Turbo => Box::new(colorgrad::preset::turbo()), - Gradient::Viridis => Box::new(colorgrad::preset::viridis()), - Gradient::Warm => Box::new(colorgrad::preset::warm()), - Gradient::Fruits => build_gradient(&[ - "#00c21c", "#009dc9", "#ffd43e", "#ff2a70", "#b971ff", "#7ce300", "#feff62", - ]), - } + cmd.gradient.to_gradient() }; let grad = if let Some(n) = cmd.sharp { @@ -403,17 +386,6 @@ fn random_color() -> Color { } } -#[cfg(feature = "cli")] -fn build_gradient(colors: &[&str]) -> Box { - Box::new( - colorgrad::GradientBuilder::new() - .html_colors(colors) - .mode(colorgrad::BlendMode::Oklab) - .build::() - .unwrap(), - ) -} - // Reference http://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef fn color_luminance(col: &Color) -> f32 { fn lum(t: f32) -> f32 { diff --git a/src/main.rs b/src/main.rs index cb806b7..68eed95 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,59 +24,37 @@ o888o.`Y8bod8P'.o888o.`Y8bod8P'.d888b....`Y888''8o..`Y8bod8P. fn main() -> Result<(), io::Error> { let opt = Opt::parse(); - let stdout = io::stdout(); - let mut stdout = stdout.lock(); + let mut stdout = io::stdout().lock(); + let mut lol: Lolcrab = opt.clone().into(); - if opt.help || opt.version { - let mut lol = Lolcrab::new(None, None); - if opt.help { - lol.colorize_str( - &Opt::command().render_help().ansi().to_string(), - &mut stdout, - )?; - } else { - lol.colorize_str(&Opt::command().render_long_version(), &mut stdout)?; - } + if opt.help { + lol.colorize_str( + &Opt::command().render_help().ansi().to_string(), + &mut stdout, + )?; + return Ok(()); + } + + if opt.version { + lol.colorize_str(&Opt::command().render_long_version(), &mut stdout)?; return Ok(()); } if opt.presets { - let presets = [ - "cividis", - "cool", - "cubehelix", - "fruits", - "inferno", - "magma", - "plasma", - "rainbow", - "rd-yl-gn", - "sinebow", - "spectral", - "turbo", - "viridis", - "warm", - ]; - for s in &presets { - let mut opt = opt.clone(); - opt.gradient = Gradient::from_str(s, true).unwrap(); - opt.custom = None; - opt.random_colors = None; - let mut lol: Lolcrab = opt.into(); - writeln!(stdout, "\n{s}\n")?; + for g in Gradient::value_variants() { + let name = format!("{g:?}").to_lowercase(); + let name = if name == "rdylgn" { "rd-yl-gn" } else { &name }; + writeln!(stdout, "\n{name}\n")?; + lol.gradient = g.to_gradient(); lol.colorize_str(SAMPLE_TEXT, &mut stdout)?; } return Ok(()); } - let animate = opt.animate; - let mut lol: Lolcrab = opt.clone().into(); - for path in opt.files { if path == PathBuf::from("-") { - let stdin = io::stdin(); - let mut stdin = stdin.lock(); - if animate { + let mut stdin = io::stdin().lock(); + if opt.animate { lol.colorize_read_anim(&mut stdin, &mut stdout)?; } else { lol.colorize_read(&mut stdin, &mut stdout)?; @@ -84,7 +62,7 @@ fn main() -> Result<(), io::Error> { } else { let f = File::open(path).unwrap(); let mut b = BufReader::new(f); - if animate { + if opt.animate { lol.colorize_read_anim(&mut b, &mut stdout)?; } else { lol.colorize_read(&mut b, &mut stdout)?;