From cd59514615451a8c550cd898494563fcaaa12b12 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Thu, 17 Mar 2016 11:27:03 -0600 Subject: [PATCH] Use 8-color mode when possible. --- src/control.rs | 61 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 22 deletions(-) diff --git a/src/control.rs b/src/control.rs index 24a68d2..d509121 100644 --- a/src/control.rs +++ b/src/control.rs @@ -96,32 +96,49 @@ pub trait TermWrite { /// Set foreground color. fn color(&mut self, color: Color) -> io::Result { let ansi = color.to_ansi_val(); - self.csi(&[ - b'3', - b'8', - b';', - b'5', - b';', - b'0' + ansi / 100, - b'0' + ansi / 10 % 10, - b'0' + ansi % 10, - b'm', - ]) + if ansi <= 7 { + self.csi(&[ + b'3', + b'0' + ansi, + b'm', + ]) + } else { + self.csi(&[ + b'3', + b'8', + b';', + b'5', + b';', + b'0' + ansi / 100, + b'0' + ansi / 10 % 10, + b'0' + ansi % 10, + b'm', + ]) + } } /// Set background color. fn bg_color(&mut self, color: Color) -> io::Result { let ansi = color.to_ansi_val(); - self.csi(&[ - b'4', - b'8', - b';', - b'5', - b';', - b'0' + ansi / 100, - b'0' + ansi / 10 % 10, - b'0' + ansi % 10, - b'm', - ]) + + if ansi <= 7 { + self.csi(&[ + b'4', + b'0' + ansi, + b'm', + ]) + } else { + self.csi(&[ + b'4', + b'8', + b';', + b'5', + b';', + b'0' + ansi / 100, + b'0' + ansi / 10 % 10, + b'0' + ansi % 10, + b'm', + ]) + } } /// Set rendition mode (SGR). fn style(&mut self, mode: Style) -> io::Result {