diff options
author | Dan Davison <dandavison7@gmail.com> | 2020-06-19 11:43:57 -0400 |
---|---|---|
committer | Dan Davison <dandavison7@gmail.com> | 2020-06-19 11:44:41 -0400 |
commit | 1c4c4d75386a10b4b8c0c6f2476feab5926b56a6 (patch) | |
tree | 78fb8a1e38f4de1ba16a7a6278060b05723ec6fb | |
parent | f0e6c833b37bbd015074da1a3707de2991b3052c (diff) |
Refactor: add native style.paint() method
-rw-r--r-- | src/draw.rs | 6 | ||||
-rw-r--r-- | src/paint.rs | 8 | ||||
-rw-r--r-- | src/style.rs | 13 |
3 files changed, 20 insertions, 7 deletions
diff --git a/src/draw.rs b/src/draw.rs index aee7743c..9d6fce6e 100644 --- a/src/draw.rs +++ b/src/draw.rs @@ -20,7 +20,7 @@ pub fn write_no_decoration( if text_style.is_raw { writeln!(writer, "{}", raw_text)?; } else { - writeln!(writer, "{}", text_style.ansi_term_style.paint(text))?; + writeln!(writer, "{}", text_style.paint(text))?; } Ok(()) } @@ -180,7 +180,7 @@ fn _write_under_or_over_lined( if text_style.is_raw { writeln!(writer, "{}", raw_text)?; } else { - writeln!(writer, "{}", text_style.ansi_term_style.paint(text))?; + writeln!(writer, "{}", text_style.paint(text))?; } match underoverline { UnderOverline::Over => {} @@ -263,7 +263,7 @@ fn write_boxed_partial( if text_style.is_raw { write!(writer, "{}", raw_text)?; } else { - write!(writer, "{}", text_style.ansi_term_style.paint(text))?; + write!(writer, "{}", text_style.paint(text))?; } write!( writer, diff --git a/src/paint.rs b/src/paint.rs index 05bc714b..97ed4834 100644 --- a/src/paint.rs +++ b/src/paint.rs @@ -188,7 +188,7 @@ impl<'a> Painter<'a> { .number_plus_format_style .ansi_term_style .paint(plus_before), - config.number_plus_style.ansi_term_style.paint(plus_number), + config.number_plus_style.paint(plus_number), config .number_plus_format_style .ansi_term_style @@ -205,7 +205,7 @@ impl<'a> Painter<'a> { ) { if !handled_prefix { if prefix != "" { - ansi_strings.push(section_style.ansi_term_style.paint(prefix)); + ansi_strings.push(section_style.paint(prefix)); if text.len() > 0 { text.remove(0); } @@ -213,14 +213,14 @@ impl<'a> Painter<'a> { handled_prefix = true; } if !text.is_empty() { - ansi_strings.push(section_style.ansi_term_style.paint(text)); + ansi_strings.push(section_style.paint(text)); } } let right_fill_background_color = non_emph_style.has_background_color() && background_color_extends_to_terminal_width .unwrap_or(config.background_color_extends_to_terminal_width); if right_fill_background_color { - ansi_strings.push(non_emph_style.ansi_term_style.paint("")); + ansi_strings.push(non_emph_style.paint("")); } let line = &mut ansi_term::ANSIStrings(&ansi_strings).to_string(); if right_fill_background_color { diff --git a/src/style.rs b/src/style.rs index 7e7bea35..dffe8d19 100644 --- a/src/style.rs +++ b/src/style.rs @@ -1,3 +1,5 @@ +use std::borrow::Cow; +use std::fmt; use std::process; use ansi_term; @@ -49,6 +51,17 @@ impl Style { } } + pub fn paint<'a, I, S: 'a + ToOwned + ?Sized>( + self, + input: I, + ) -> ansi_term::ANSIGenericString<'a, S> + where + I: Into<Cow<'a, S>>, + <S as ToOwned>::Owned: fmt::Debug, + { + self.ansi_term_style.paint(input) + } + pub fn has_background_color(&self) -> bool { if self.ansi_term_style.is_reverse { self.ansi_term_style.foreground.is_some() |