on master help sign in
Add bold, italic and underline font styles
commitcb7b158172a8336778dc860795a1f2cdc0180356 authorsharkdp <davidpeter@web.de> dateFri 11 May 2018, 13:52 (8 years ago) parent22c8978
1 file changed+17−5
src/terminal.rs+17−5
src/terminal.rs+17−5
@@ -2,7 +2,7 @@ use std::fmt::Write;
22
33use ansi_term::Colour::{Fixed, RGB};
44use ansi_term::Style;
5−use syntect::highlighting;
5+use syntect::highlighting::{self, FontStyle};
66
77/// Approximate a 24 bit color value by a 8 bit ANSI code
88fn rgb2ansi(r: u8, g: u8, b: u8) -> u8 {
@@ -35,11 +35,23 @@ pub fn as_terminal_escaped(
3535 for &(ref style, text) in v.iter() {
3636 let style = if !colored {
3737 Style::default()
38− } else if true_color {
39− RGB(style.foreground.r, style.foreground.g, style.foreground.b).normal()
4038 } else {
41− let ansi = rgb2ansi(style.foreground.r, style.foreground.g, style.foreground.b);
42− Fixed(ansi).normal()
39+ let color = if true_color {
40+ RGB(style.foreground.r, style.foreground.g, style.foreground.b)
41+ } else {
42+ let ansi = rgb2ansi(style.foreground.r, style.foreground.g, style.foreground.b);
43+ Fixed(ansi)
44+ };
45+
46+ if style.font_style.contains(FontStyle::BOLD) {
47+ color.bold()
48+ } else if style.font_style.contains(FontStyle::UNDERLINE) {
49+ color.underline()
50+ } else if style.font_style.contains(FontStyle::ITALIC) {
51+ color.italic()
52+ } else {
53+ color.normal()
54+ }
4355 };
4456
4557 write!(s, "{}", style.paint(text)).unwrap();