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.
This commit is contained in:
Benjamin Sago 2015-12-17 17:51:42 +08:00
parent 39aa210437
commit 15cd67abe6
3 changed files with 49 additions and 19 deletions

View File

@ -26,6 +26,14 @@ pub struct TextCell {
pub width: DisplayWidth, pub width: DisplayWidth,
} }
impl Deref for TextCell {
type Target = TextCellContents;
fn deref<'a>(&'a self) -> &'a Self::Target {
&self.contents
}
}
impl TextCell { impl TextCell {
/// Creates a new text cell that holds the given text in the given style, /// 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); let width = DisplayWidth::from(&*text);
TextCell { TextCell {
contents: vec![ style.paint(text) ], contents: vec![ style.paint(text) ].into(),
width: width, width: width,
} }
} }
@ -46,7 +54,7 @@ impl TextCell {
let width = DisplayWidth::from(text); let width = DisplayWidth::from(text);
TextCell { TextCell {
contents: vec![ style.paint(text) ], contents: vec![ style.paint(text) ].into(),
width: width, width: width,
} }
} }
@ -59,7 +67,7 @@ impl TextCell {
/// tabular data when there is *something* in each cell. /// tabular data when there is *something* in each cell.
pub fn blank(style: Style) -> Self { pub fn blank(style: Style) -> Self {
TextCell { TextCell {
contents: vec![ style.paint("-") ], contents: vec![ style.paint("-") ].into(),
width: DisplayWidth::from(1), width: DisplayWidth::from(1),
} }
} }
@ -73,25 +81,19 @@ impl TextCell {
(*self.width) += count; (*self.width) += count;
let spaces: String = repeat(' ').take(count).collect(); 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. /// Adds the contents of another `ANSIString` to the end of this cell.
pub fn push(&mut self, string: ANSIString<'static>, extra_width: usize) { pub fn push(&mut self, string: ANSIString<'static>, extra_width: usize) {
self.contents.push(string); self.contents.0.push(string);
(*self.width) += extra_width; (*self.width) += extra_width;
} }
/// Adds all the contents of another `TextCell` to the end of this cell. /// Adds all the contents of another `TextCell` to the end of this cell.
pub fn append(&mut self, other: TextCell) { pub fn append(&mut self, other: TextCell) {
(*self.width) += *other.width; (*self.width) += *other.width;
self.contents.extend(other.contents); self.contents.0.extend(other.contents.0);
}
/// 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)
} }
} }
@ -129,7 +131,35 @@ impl TextCell {
/// `TextCell` but arent concerned with tracking its width, because it occurs /// `TextCell` but arent concerned with tracking its width, because it occurs
/// in the final cell of a table or grid and theres no point padding it. This /// in the final cell of a table or grid and theres no point padding it. This
/// happens when dealing with file names. /// 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. /// The Unicode “display width” of a string.

View File

@ -546,7 +546,7 @@ impl<U> Table<U> where U: Users {
let width = DisplayWidth::from(chars.len()); let width = DisplayWidth::from(chars.len());
TextCell { TextCell {
contents: chars, contents: chars.into(),
width: width, width: width,
} }
} }
@ -605,7 +605,7 @@ impl<U> Table<U> where U: Users {
contents: vec![ contents: vec![
self.colours.size.numbers.paint(number), self.colours.size.numbers.paint(number),
self.colours.size.unit.paint(symbol), self.colours.size.unit.paint(symbol),
], ].into(),
} }
} }
@ -638,7 +638,7 @@ impl<U> Table<U> where U: Users {
contents: vec![ contents: vec![
git_char(git.staged), git_char(git.staged),
git_char(git.unstaged) git_char(git.unstaged)
], ].into(),
} }
} }

View File

@ -25,7 +25,7 @@ pub fn filename(file: File, colours: &Colours, links: bool) -> TextCellContents
else { else {
vec![ vec![
file_colour(colours, &file).paint(file.name) file_colour(colours, &file).paint(file.name)
] ].into()
} }
} }
@ -38,7 +38,7 @@ fn symlink_filename(file: File, colours: &Colours) -> TextCellContents {
Style::default().paint(" "), Style::default().paint(" "),
colours.symlink_path.paint(target.path_prefix()), colours.symlink_path.paint(target.path_prefix()),
file_colour(colours, &target).paint(target.name) file_colour(colours, &target).paint(target.name)
], ].into(),
Err(filename) => vec![ Err(filename) => vec![
file_colour(colours, &file).paint(file.name), file_colour(colours, &file).paint(file.name),
@ -46,6 +46,6 @@ fn symlink_filename(file: File, colours: &Colours) -> TextCellContents {
colours.broken_arrow.paint("->"), colours.broken_arrow.paint("->"),
Style::default().paint(" "), Style::default().paint(" "),
colours.broken_filename.paint(filename), colours.broken_filename.paint(filename),
], ].into(),
} }
} }