Skip to content

Commit

Permalink
some improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mazznoer committed Sep 1, 2024
1 parent f0c51e7 commit 1fec559
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 71 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
33 changes: 33 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,39 @@ pub enum Gradient {
Warm,
}

impl Gradient {
pub fn to_gradient(&self) -> Box<dyn colorgrad::Gradient> {
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<dyn colorgrad::Gradient> {
Box::new(
colorgrad::GradientBuilder::new()
.html_colors(colors)
.mode(colorgrad::BlendMode::Oklab)
.build::<colorgrad::CatmullRomGradient>()
.unwrap(),
)
}

#[derive(Clone, Debug, Parser)]
#[command(
name = "lolcrab",
Expand Down
30 changes: 1 addition & 29 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,24 +351,7 @@ impl From<Opt> 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 {
Expand Down Expand Up @@ -403,17 +386,6 @@ fn random_color() -> Color {
}
}

#[cfg(feature = "cli")]
fn build_gradient(colors: &[&str]) -> Box<dyn colorgrad::Gradient> {
Box::new(
colorgrad::GradientBuilder::new()
.html_colors(colors)
.mode(colorgrad::BlendMode::Oklab)
.build::<colorgrad::CatmullRomGradient>()
.unwrap(),
)
}

// Reference http://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef
fn color_luminance(col: &Color) -> f32 {
fn lum(t: f32) -> f32 {
Expand Down
62 changes: 20 additions & 42 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,67 +24,45 @@ 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)?;
}
} 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)?;
Expand Down

0 comments on commit 1fec559

Please sign in to comment.