Skip to content

Commit

Permalink
Add pastel --layout {horizontal,vertical,detail}
Browse files Browse the repository at this point in the history
'Detail' remains the default layout. The layout can also be set as -v or -h
as program arguments (which displaces the old `pastel -h`, which printed help)
or by setting the PASTEL_LAYOUT environment variable.

- readme video out of date
- Didn't try compiling w/ rustc 1.43.
  • Loading branch information
purpleposeidon committed Sep 11, 2023
1 parent 736fdf2 commit abeec66
Show file tree
Hide file tree
Showing 13 changed files with 177 additions and 108 deletions.
24 changes: 24 additions & 0 deletions src/cli/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,30 @@ pub fn build_cli() -> Command<'static> {
.ignore_case(true)
.help("Use a specific tool to pick the colors")
)
.arg(
Arg::new("layout")
.long("layout")
.value_name("layout")
.max_occurrences(1)
.help("Controls how the colors are printed")
.possible_values(["detail", "vertical", "horizontal"])
.conflicts_with("layout_horizontal")
.conflicts_with("layout_vertical")
)
.arg(
Arg::new("layout_horizontal")
.short('h')
.help("Alias for --layout horizontal")
.conflicts_with("layout")
.conflicts_with("layout_vertical")
)
.arg(
Arg::new("layout_vertical")
.short('v')
.help("Alias for --layout vertical")
.conflicts_with("layout")
.conflicts_with("layout_horizontal")
)
}

#[test]
Expand Down
8 changes: 4 additions & 4 deletions src/cli/commands/color_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ macro_rules! color_command {
$config: &Config,
$color: &Color,
) -> Result<()> {
let output = $body;
out.show_color($config, &output)
out.push_color($body);
Ok(())
}
}
};
Expand Down Expand Up @@ -77,7 +77,7 @@ color_command!(MixCommand, config, matches, color, {
mix(&base, color, fraction)
});

color_command!(ColorblindCommand, config, matches, color, {
color_command!(ColorblindCommand, _config, matches, color, {
// The type of colorblindness selected (protanopia, deuteranopia, tritanopia)
let cb_ty = matches.value_of("type").expect("required argument");
let cb_ty = cb_ty.to_lowercase();
Expand All @@ -95,7 +95,7 @@ color_command!(ColorblindCommand, config, matches, color, {
color.simulate_colorblindness(cb_ty)
});

color_command!(SetCommand, config, matches, color, {
color_command!(SetCommand, _config, matches, color, {
let property = matches.value_of("property").expect("required argument");
let property = property.to_lowercase();
let property = property.as_ref();
Expand Down
4 changes: 2 additions & 2 deletions src/cli/commands/distinct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ impl GenericCommand for DistinctCommand {
print_distance_matrix(&mut stderr.lock(), brush_stderr, &colors, distance_metric)?;
}

for color in colors {
out.show_color(config, &color)?;
for color in &colors {
out.push_color(color.clone());
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/cli/commands/gradient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,8 @@ impl GenericCommand for GradientCommand {
let position = Fraction::from(i as f64 / (count as f64 - 1.0));

let color = color_scale.sample(position, &mix).expect("gradient color");

out.show_color(config, &color)?;
out.push_color(color);
}

Ok(())
}
}
5 changes: 3 additions & 2 deletions src/cli/commands/gray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ use pastel::Color;
pub struct GrayCommand;

impl GenericCommand for GrayCommand {
fn run(&self, out: &mut Output, matches: &ArgMatches, config: &Config) -> Result<()> {
fn run(&self, out: &mut Output, matches: &ArgMatches, _config: &Config) -> Result<()> {
let lightness = number_arg(matches, "lightness")?;
let gray = Color::graytone(lightness);
out.show_color(config, &gray)
out.push_color(gray);
Ok(())
}
}
4 changes: 2 additions & 2 deletions src/cli/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ impl Command {
for color in ColorArgIterator::from_args(config, matches.values_of("color"))? {
cmd.run(&mut out, matches, config, &color?)?;
}

Ok(())
}
}
}?;
out.finish_colors(config)
}
}
2 changes: 1 addition & 1 deletion src/cli/commands/pick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl GenericCommand for PickCommand {

for color_str in color_strings {
let color = ColorArgIterator::from_color_arg(config, &color_str, &mut print_spectrum)?;
out.show_color(config, &color)?;
out.push_color(color);
}

Ok(())
Expand Down
5 changes: 2 additions & 3 deletions src/cli/commands/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use pastel::random::RandomizationStrategy;
pub struct RandomCommand;

impl GenericCommand for RandomCommand {
fn run(&self, out: &mut Output, matches: &ArgMatches, config: &Config) -> Result<()> {
fn run(&self, out: &mut Output, matches: &ArgMatches, _config: &Config) -> Result<()> {
let strategy_arg = matches.value_of("strategy").expect("required argument");

let count = matches.value_of("number").expect("required argument");
Expand All @@ -23,9 +23,8 @@ impl GenericCommand for RandomCommand {
};

for _ in 0..count {
out.show_color(config, &strategy.generate())?;
out.push_color(strategy.generate());
}

Ok(())
}
}
5 changes: 3 additions & 2 deletions src/cli/commands/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use crate::commands::prelude::*;
pub struct ShowCommand;

impl ColorCommand for ShowCommand {
fn run(&self, out: &mut Output, _: &ArgMatches, config: &Config, color: &Color) -> Result<()> {
out.show_color(config, color)
fn run(&self, out: &mut Output, _: &ArgMatches, _config: &Config, color: &Color) -> Result<()> {
out.push_color(color.clone());
Ok(())
}
}
2 changes: 1 addition & 1 deletion src/cli/commands/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl GenericCommand for SortCommand {
}

for color in colors {
out.show_color(config, &color)?;
out.push_color(color);
}

Ok(())
Expand Down
8 changes: 8 additions & 0 deletions src/cli/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@ pub struct Config<'p> {
pub colorpicker: Option<&'p str>,
pub interactive_mode: bool,
pub brush: Brush,
pub layout: Layout,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Layout {
Detail,
Vertical,
Horizontal,
}
17 changes: 16 additions & 1 deletion src/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mod output;
mod utility;

use commands::Command;
use config::Config;
use config::{Config, Layout};
use error::{PastelError, Result};

use pastel::ansi::{self, Brush, Mode};
Expand Down Expand Up @@ -98,13 +98,28 @@ fn run() -> Result<ExitCode> {
}
};

let layout = std::env::var("PASTEL_LAYOUT").ok();
let layout = layout.as_ref().map(String::as_str);
let layout = global_matches.value_of("layout").or(layout);
let config = Config {
padding: 2,
colorpicker_width: 48,
colorcheck_width: 8,
interactive_mode,
brush: Brush::from_mode(color_mode),
colorpicker: global_matches.value_of("color-picker"),
layout: match layout {
Some("detail") => Layout::Detail,
Some("vertical") | Some("v") | Some("|") => Layout::Vertical,
Some("horizontal") | Some("h") | Some("-") => Layout::Horizontal,
Some(wrong) => {
write_stderr(Color::red(), "pastel error", &format!("Unknown layout {wrong:?}"));
std::process::exit(1);
},
_ if global_matches.is_present("layout_horizontal") => Layout::Horizontal,
_ if global_matches.is_present("layout_vertical") => Layout::Vertical,
_ => Layout::Detail,
},
};

if let Some((subcommand, matches)) = global_matches.subcommand() {
Expand Down
Loading

0 comments on commit abeec66

Please sign in to comment.