summaryrefslogtreecommitdiffstats
path: root/termcolor
diff options
context:
space:
mode:
authorAndrew Gallant <jamslam@gmail.com>2017-01-06 20:07:29 -0500
committerAndrew Gallant <jamslam@gmail.com>2017-01-06 20:09:51 -0500
commitb187c1a8175d88b743e2e225dbec98661f7a2f94 (patch)
treebfb55fdc1e8704fd2a63d6ce922e527b62921faa /termcolor
parentf7a2fe30d4f0fe055e63a630c940383a1d63d6a9 (diff)
Rejigger bold and intense settings.
Previously, ripgrep would only emit the 'bold' ANSI escape sequence if no foreground or background color was set. Instead, it would convert colors to their "intense" versions if bold was set. The intent was to do the same thing on Windows and Unix. However, this had a few negative side effects: 1. Omitting the 'bold' ANSI escape when 'bold' was set is surprising. 2. Intense colors can look quite bad and be hard to read. To fix this, we introduce a new setting called 'intense' in the --colors flag, and thread that down through to the public API of the `termcolor` crate. The 'intense' setting has environment specific behavior: 1. In ANSI mode, it will convert the selected color to its "intense" variant. 2. In the Windows console, it will make the text "intense." There is no longer any "smart" handling of the 'bold' style. The 'bold' ANSI escape is always emitted when it is selected. In the Windows console, the 'bold' setting now has no effect. Note that this is a breaking change. Fixes #266, #293
Diffstat (limited to 'termcolor')
-rw-r--r--termcolor/src/lib.rs31
1 files changed, 21 insertions, 10 deletions
diff --git a/termcolor/src/lib.rs b/termcolor/src/lib.rs
index 6b6492b0..8b434d97 100644
--- a/termcolor/src/lib.rs
+++ b/termcolor/src/lib.rs
@@ -760,12 +760,12 @@ impl<W: io::Write> WriteColor for Ansi<W> {
fn set_color(&mut self, spec: &ColorSpec) -> io::Result<()> {
try!(self.reset());
if let Some(ref c) = spec.fg_color {
- try!(self.write_color(true, c, spec.bold));
+ try!(self.write_color(true, c, spec.intense));
}
if let Some(ref c) = spec.bg_color {
- try!(self.write_color(false, c, spec.bold));
+ try!(self.write_color(false, c, spec.intense));
}
- if spec.bold && spec.fg_color.is_none() && spec.bg_color.is_none() {
+ if spec.bold {
try!(self.write_str("\x1B[1m"));
}
Ok(())
@@ -785,11 +785,8 @@ impl<W: io::Write> Ansi<W> {
&mut self,
fg: bool,
c: &Color,
- bold: bool,
+ intense: bool,
) -> io::Result<()> {
- // *sigh*... The termion crate doesn't compile on Windows, and we
- // need to be able to write ANSI escape sequences on Windows, so I
- // guess we have to roll this ourselves.
macro_rules! w {
($selfie:expr, $fg:expr, $clr:expr) => {
if $fg {
@@ -799,7 +796,7 @@ impl<W: io::Write> Ansi<W> {
}
}
}
- if bold {
+ if intense {
match *c {
Color::Black => w!(self, fg, "8"),
Color::Blue => w!(self, fg, "12"),
@@ -935,12 +932,13 @@ pub struct ColorSpec {
fg_color: Option<Color>,
bg_color: Option<Color>,
bold: bool,
+ intense: bool,
}
impl ColorSpec {
/// Create a new color specification that has no colors or styles.
pub fn new() -> ColorSpec {
- ColorSpec { fg_color: None, bg_color: None, bold: false }
+ ColorSpec::default()
}
/// Get the foreground color.
@@ -962,14 +960,27 @@ impl ColorSpec {
}
/// Get whether this is bold or not.
+ ///
+ /// Note that the bold setting has no effect in a Windows console.
pub fn bold(&self) -> bool { self.bold }
/// Set whether the text is bolded or not.
+ ///
+ /// Note that the bold setting has no effect in a Windows console.
pub fn set_bold(&mut self, yes: bool) -> &mut ColorSpec {
self.bold = yes;
self
}
+ /// Get whether this is intense or not.
+ pub fn intense(&self) -> bool { self.intense }
+
+ /// Set whether the text is intense or not.
+ pub fn set_intense(&mut self, yes: bool) -> &mut ColorSpec {
+ self.intense = yes;
+ self
+ }
+
/// Returns true if this color specification has no colors or styles.
pub fn is_none(&self) -> bool {
self.fg_color.is_none() && self.bg_color.is_none() && !self.bold
@@ -990,7 +1001,7 @@ impl ColorSpec {
) -> io::Result<()> {
use wincolor::Intense;
- let intense = if self.bold { Intense::Yes } else { Intense::No };
+ let intense = if self.intense { Intense::Yes } else { Intense::No };
if let Some(color) = self.fg_color.as_ref().map(|c| c.to_windows()) {
try!(console.fg(intense, color));
}