Skip to content

Commit

Permalink
Add new escaped ANSI formats, closes #113
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkdp committed Feb 25, 2020
1 parent 4807607 commit bb3d2ab
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/cli/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,16 @@ pub fn build_cli() -> App<'static, 'static> {
pastel random -n 20 | pastel format rgb")
.arg(
Arg::with_name("type")
.help("Output format type")
.help("Output format type. Note that the 'ansi-*-escaped' formats print \
ansi escape sequences to the terminal that will not be visible \
unless something else is printed in addition.")
.possible_values(&["rgb", "rgb-float", "hex",
"hsl", "hsl-hue", "hsl-saturation", "hsl-lightness",
"lch", "lch-lightness", "lch-chroma", "lch-hue",
"lab", "lab-a", "lab-b",
"luminance", "brightness",
"ansi-8bit", "ansi-24bit",
"ansi-8bit-escaped", "ansi-24bit-escaped",
"name"])
.case_insensitive(true)
.default_value("hex")
Expand Down
12 changes: 9 additions & 3 deletions src/cli/commands/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ impl ColorCommand for FormatCommand {
let format_type = matches.value_of("type").expect("required argument");
let format_type = format_type.to_lowercase();

let replace_escape = |code: &str| {
code.replace("\x1b", "\\x1b")
};

let output = match format_type.as_ref() {
"rgb" => color.to_rgb_string(Format::Spaces),
"rgb-float" => color.to_rgb_float_string(Format::Spaces),
Expand All @@ -34,16 +38,18 @@ impl ColorCommand for FormatCommand {
"lab-b" => format!("{:.2}", color.to_lab().b),
"luminance" => format!("{:.3}", color.luminance()),
"brightness" => format!("{:.3}", color.brightness()),
"ansi-8bit" => color.to_ansi_sequence(Mode::Ansi8Bit),
"ansi-24bit" => color.to_ansi_sequence(Mode::TrueColor),
"ansi-8bit" => replace_escape(&color.to_ansi_sequence(Mode::Ansi8Bit)),
"ansi-24bit" => replace_escape(&color.to_ansi_sequence(Mode::TrueColor)),
"ansi-8bit-escapecode" => color.to_ansi_sequence(Mode::Ansi8Bit),
"ansi-24bit-escapecode" => color.to_ansi_sequence(Mode::TrueColor),
"name" => similar_colors(color)[0].name.to_owned(),
&_ => {
unreachable!("Unknown format type");
}
};

let write_colored_line = match format_type.as_ref() {
"ansi-8bit" | "ansi-24bit" => false,
"ansi-8bit-escapecode" | "ansi-24bit-escapecode" => false,
_ => true,
};

Expand Down

0 comments on commit bb3d2ab

Please sign in to comment.