diff options
author | Benjamin Sago <ogham@bsago.me> | 2015-12-17 17:51:42 +0800 |
---|---|---|
committer | Benjamin Sago <ogham@bsago.me> | 2015-12-17 17:51:42 +0800 |
commit | 15cd67abe67ffeaef812f4080e1e35b06bf31784 (patch) | |
tree | b89a02b92016f268a381b1d68598b4641198e7ed /src | |
parent | 39aa2104379cc26b3b75c9c1518e6cdb21a3c3b6 (diff) |
Turn TextCellContents into a struct
The benefit of this is that it make it possible to convert text cell contents
vectors into text cells with a method (see next commit). Casualties include
having to call `.into()` on vectors everywhere, which I'm not convinced is a
bad thing.
Diffstat (limited to 'src')
-rw-r--r-- | src/output/cell.rs | 56 | ||||
-rw-r--r-- | src/output/details.rs | 6 | ||||
-rw-r--r-- | src/output/mod.rs | 6 |
3 files changed, 49 insertions, 19 deletions
diff --git a/src/output/cell.rs b/src/output/cell.rs index 4e4acbe..d900f6d 100644 --- a/src/output/cell.rs +++ b/src/output/cell.rs @@ -26,6 +26,14 @@ pub struct TextCell { pub width: DisplayWidth, } +impl Deref for TextCell { + type Target = TextCellContents; + + fn deref<'a>(&'a self) -> &'a Self::Target { + &self.contents + } +} + impl TextCell { /// Creates a new text cell that holds the given text in the given style, @@ -34,7 +42,7 @@ impl TextCell { let width = DisplayWidth::from(&*text); TextCell { - contents: vec![ style.paint(text) ], + contents: vec![ style.paint(text) ].into(), width: width, } } @@ -46,7 +54,7 @@ impl TextCell { let width = DisplayWidth::from(text); TextCell { - contents: vec![ style.paint(text) ], + contents: vec![ style.paint(text) ].into(), width: width, } } @@ -59,7 +67,7 @@ impl TextCell { /// tabular data when there is *something* in each cell. pub fn blank(style: Style) -> Self { TextCell { - contents: vec![ style.paint("-") ], + contents: vec![ style.paint("-") ].into(), width: DisplayWidth::from(1), } } @@ -73,25 +81,19 @@ impl TextCell { (*self.width) += count; let spaces: String = repeat(' ').take(count).collect(); - self.contents.push(Style::default().paint(spaces)); + self.contents.0.push(Style::default().paint(spaces)); } /// Adds the contents of another `ANSIString` to the end of this cell. pub fn push(&mut self, string: ANSIString<'static>, extra_width: usize) { - self.contents.push(string); + self.contents.0.push(string); (*self.width) += extra_width; } /// Adds all the contents of another `TextCell` to the end of this cell. pub fn append(&mut self, other: TextCell) { (*self.width) += *other.width; - self.contents.extend(other.contents); - } - - /// Produces an `ANSIStrings` value that can be used to print the styled - /// values of this cell as an ANSI-terminal-formatted string. - pub fn strings(&self) -> ANSIStrings { - ANSIStrings(&self.contents) + self.contents.0.extend(other.contents.0); } } @@ -129,7 +131,35 @@ impl TextCell { /// `TextCell` but aren’t concerned with tracking its width, because it occurs /// in the final cell of a table or grid and there’s no point padding it. This /// happens when dealing with file names. -pub type TextCellContents = Vec<ANSIString<'static>>; +#[derive(PartialEq, Debug, Clone, Default)] +pub struct TextCellContents(Vec<ANSIString<'static>>); + +impl From<Vec<ANSIString<'static>>> for TextCellContents { + fn from(strings: Vec<ANSIString<'static>>) -> TextCellContents { + TextCellContents(strings) + } +} + +impl Deref for TextCellContents { + type Target = [ANSIString<'static>]; + + fn deref<'a>(&'a self) -> &'a Self::Target { + &*self.0 + } +} + +// No DerefMut implementation here -- it would be publicly accessible, and as +// the contents only get changed in this module, the mutators in the struct +// above can just access the value directly. + +impl TextCellContents { + + /// Produces an `ANSIStrings` value that can be used to print the styled + /// values of this cell as an ANSI-terminal-formatted string. + pub fn strings(&self) -> ANSIStrings { + ANSIStrings(&self.0) + } +} /// The Unicode “display width” of a string. diff --git a/src/output/details.rs b/src/output/details.rs index 97be06f..0b45b7b 100644 --- a/src/output/details.rs +++ b/src/output/details.rs @@ -546,7 +546,7 @@ impl<U> Table<U> where U: Users { let width = DisplayWidth::from(chars.len()); TextCell { - contents: chars, + contents: chars.into(), width: width, } } @@ -605,7 +605,7 @@ impl<U> Table<U> where U: Users { contents: vec![ self.colours.size.numbers.paint(number), self.colours.size.unit.paint(symbol), - ], + ].into(), } } @@ -638,7 +638,7 @@ impl<U> Table<U> where U: Users { contents: vec![ git_char(git.staged), git_char(git.unstaged) - ], + ].into(), } } diff --git a/src/output/mod.rs b/src/output/mod.rs index 88fcbe4..d1c7e14 100644 --- a/src/output/mod.rs +++ b/src/output/mod.rs @@ -25,7 +25,7 @@ pub fn filename(file: File, colours: &Colours, links: bool) -> TextCellContents else { vec![ file_colour(colours, &file).paint(file.name) - ] + ].into() } } @@ -38,7 +38,7 @@ fn symlink_filename(file: File, colours: &Colours) -> TextCellContents { Style::default().paint(" "), colours.symlink_path.paint(target.path_prefix()), file_colour(colours, &target).paint(target.name) - ], + ].into(), Err(filename) => vec![ file_colour(colours, &file).paint(file.name), @@ -46,6 +46,6 @@ fn symlink_filename(file: File, colours: &Colours) -> TextCellContents { colours.broken_arrow.paint("->"), Style::default().paint(" "), colours.broken_filename.paint(filename), - ], + ].into(), } } |