Skip to content

Commit

Permalink
Windows: fix 'colorpick' 24bit support, see #45
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkdp committed Aug 27, 2019
1 parent a1679d8 commit 4ba074c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
26 changes: 19 additions & 7 deletions src/ansi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ lazy_static! {
.collect();
}

#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Mode {
Ansi8Bit,
TrueColor,
Expand Down Expand Up @@ -243,6 +243,23 @@ impl ToAnsiStyle for Color {
}
}

#[cfg(not(windows))]
pub fn get_colormode() -> Mode {
use std::env;

let env_colorterm = env::var("COLORTERM").ok();
match env_colorterm.as_ref().map(|s| s.as_str()) {
Some("truecolor") | Some("24bit") => Mode::TrueColor,
_ => Mode::Ansi8Bit,
}
}

#[cfg(windows)]
pub fn get_colormode() -> Mode {
// Assume 24bit support on Windows
Mode::TrueColor
}

#[derive(Debug, Clone, Copy)]
pub struct Brush {
mode: Option<Mode>,
Expand All @@ -255,15 +272,10 @@ impl Brush {

pub fn from_environment(stream: Stream) -> Self {
let mode = if atty::is(stream) {
if std::env::var("COLORTERM") == Ok("truecolor".into()) {
Some(Mode::TrueColor)
} else {
Some(Mode::Ansi8Bit)
}
Some(get_colormode())
} else {
None
};

Brush { mode }
}

Expand Down
25 changes: 8 additions & 17 deletions src/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,14 @@ fn run() -> Result<ExitCode> {
Some(value) => {
return Err(PastelError::UnknownColorMode(value.into()));
}
#[cfg(windows)]
None => {
// Assume 24bit support on Windows
Some(ansi::Mode::TrueColor)
}
#[cfg(not(windows))]
None => {
let env_colorterm = std::env::var("COLORTERM").ok();
match env_colorterm.as_ref().map(|s| s.as_str()) {
Some("truecolor") | Some("24bit") => Some(ansi::Mode::TrueColor),
_ => {
if global_matches.subcommand_name() != Some("paint")
&& global_matches.subcommand_name() != Some("colorcheck")
{
write_stderr(Color::yellow(), "pastel warning",
let mode = ansi::get_colormode();

if mode == ansi::Mode::Ansi8Bit
&& global_matches.subcommand_name() != Some("paint")
&& global_matches.subcommand_name() != Some("colorcheck")
{
write_stderr(Color::yellow(), "pastel warning",
"Your terminal emulator does not appear to support 24-bit colors \
(this means that the COLORTERM environment variable is not set to \
'truecolor' or '24bit'). \
Expand All @@ -91,10 +84,8 @@ fn run() -> Result<ExitCode> {
warning or try a different terminal emulator.\n\n\
\
For more information, see https://gist.github.com/XVilka/8346728\n");
}
Some(ansi::Mode::Ansi8Bit)
}
}
Some(mode)
}
}
} else {
Expand Down

0 comments on commit 4ba074c

Please sign in to comment.