diff options
author | Dan Davison <dandavison7@gmail.com> | 2020-03-01 13:13:18 -0600 |
---|---|---|
committer | Dan Davison <dandavison7@gmail.com> | 2020-03-01 16:25:10 -0600 |
commit | 37cc69ea434303ec8ebf4f248d4bd7f1febfe3e2 (patch) | |
tree | 9858059d214be7d3e9405d11403be418cc71f062 /src/draw.rs | |
parent | eeee3d4732479295248b49164f8db58d3244b1ff (diff) |
Support 256-color terminal environments
Fixes #110
With this commit, delta enters "24 bit color mode" iff either of the
following are true:
1. The --24-bit-color command line option value is "always"
2. The --24-bit-color command line option value is "auto" and the
COLORTERM environment variable is set to "truecolor" or "24bit".
See
https://gist.github.com/XVilka/8346728#true-color-detection
https://github.com/sharkdp/bat/blob/7779d9f6221b3e98c43a43ceb5596ba285fdf4f8/src/bin/bat/app.rs#L29-L33
Otherwise, delta enters "8-bit color mode".
In "24 bit color mode", delta will
1. Emit 24-bit RGB color shell escape sequences that will only be
displayed correctly by a terminal application that supports 24 bit
colors.
2. Select default background colors that will only be displayed
correctly by a terminal application that supports 24 bit colors.
In "8-bit color mode", delta will
1. Emit color shell escape sequences specifying the entry in the ANSI
256 color palette that is closest (according to the ansi_colours
library) to the requested color.
2. Select default background colors that will work well in a terminal
application that supports 8-bit color but not 24-bit color.
Diffstat (limited to 'src/draw.rs')
-rw-r--r-- | src/draw.rs | 41 |
1 files changed, 28 insertions, 13 deletions
diff --git a/src/draw.rs b/src/draw.rs index 751f99f9..03b93bdb 100644 --- a/src/draw.rs +++ b/src/draw.rs @@ -15,6 +15,7 @@ pub fn write_boxed( _line_width: usize, // ignored color: Color, heavy: bool, + true_color: bool, ) -> std::io::Result<()> { let up_left = if heavy { box_drawing::heavy::UP_LEFT @@ -22,8 +23,12 @@ pub fn write_boxed( box_drawing::light::UP_LEFT }; let box_width = strip_ansi_codes(text).graphemes(true).count() + 1; - write_boxed_partial(writer, text, box_width, color, heavy)?; - write!(writer, "{}", paint::paint_text_foreground(up_left, color))?; + write_boxed_partial(writer, text, box_width, color, heavy, true_color)?; + write!( + writer, + "{}", + paint::paint_text_foreground(up_left, color, true_color) + )?; Ok(()) } @@ -35,9 +40,10 @@ pub fn write_boxed_with_line( line_width: usize, color: Color, heavy: bool, + true_color: bool, ) -> std::io::Result<()> { let box_width = strip_ansi_codes(text).graphemes(true).count() + 1; - write_boxed_with_horizontal_whisker(writer, text, box_width, color, heavy)?; + write_boxed_with_horizontal_whisker(writer, text, box_width, color, heavy, true_color)?; write_horizontal_line( writer, if line_width > box_width { @@ -47,6 +53,7 @@ pub fn write_boxed_with_line( }, color, heavy, + true_color, )?; write!(writer, "\n")?; Ok(()) @@ -58,9 +65,14 @@ pub fn write_underlined( line_width: usize, color: Color, heavy: bool, + true_color: bool, ) -> std::io::Result<()> { - writeln!(writer, "{}", paint::paint_text_foreground(text, color))?; - write_horizontal_line(writer, line_width - 1, color, heavy)?; + writeln!( + writer, + "{}", + paint::paint_text_foreground(text, color, true_color) + )?; + write_horizontal_line(writer, line_width - 1, color, heavy, true_color)?; write!(writer, "\n")?; Ok(()) } @@ -70,6 +82,7 @@ fn write_horizontal_line( line_width: usize, color: Color, heavy: bool, + true_color: bool, ) -> std::io::Result<()> { let horizontal = if heavy { box_drawing::heavy::HORIZONTAL @@ -79,7 +92,7 @@ fn write_horizontal_line( write!( writer, "{}", - paint::paint_text_foreground(&horizontal.repeat(line_width), color) + paint::paint_text_foreground(&horizontal.repeat(line_width), color, true_color) ) } @@ -89,17 +102,18 @@ pub fn write_boxed_with_horizontal_whisker( box_width: usize, color: Color, heavy: bool, + true_color: bool, ) -> std::io::Result<()> { let up_horizontal = if heavy { box_drawing::heavy::UP_HORIZONTAL } else { box_drawing::light::UP_HORIZONTAL }; - write_boxed_partial(writer, text, box_width, color, heavy)?; + write_boxed_partial(writer, text, box_width, color, heavy, true_color)?; write!( writer, "{}", - paint::paint_text_foreground(up_horizontal, color) + paint::paint_text_foreground(up_horizontal, color, true_color) )?; Ok(()) } @@ -110,6 +124,7 @@ fn write_boxed_partial( box_width: usize, color: Color, heavy: bool, + true_color: bool, ) -> std::io::Result<()> { let horizontal = if heavy { box_drawing::heavy::HORIZONTAL @@ -131,10 +146,10 @@ fn write_boxed_partial( write!( writer, "{}{}\n{} {}\n{}", - paint::paint_text_foreground(&horizontal_edge, color), - paint::paint_text_foreground(down_left, color), - paint::paint_text_foreground(text, color), - paint::paint_text_foreground(vertical, color), - paint::paint_text_foreground(&horizontal_edge, color), + paint::paint_text_foreground(&horizontal_edge, color, true_color), + paint::paint_text_foreground(down_left, color, true_color), + paint::paint_text_foreground(text, color, true_color), + paint::paint_text_foreground(vertical, color, true_color), + paint::paint_text_foreground(&horizontal_edge, color, true_color), ) } |