Rename cell 'length' to 'width'

Because, strictly speaking, it's not a length, it's a width!

Also, re-order some struct constructors so that they're no longer
order-dependent (it's no longer the case that a value will be borrowed for one
field then consumed in another, meaning they have to be ordered in a certain
way to compile. Now the value is just worked out beforehand and the fields can
be specified in any order)
This commit is contained in:
Benjamin Sago 2015-12-17 10:34:11 +08:00
parent 88653a00eb
commit 39aa210437
3 changed files with 24 additions and 17 deletions

View File

@ -23,7 +23,7 @@ pub struct TextCell {
pub contents: TextCellContents,
/// The Unicode “display width” of this cell.
pub length: DisplayWidth,
pub width: DisplayWidth,
}
impl TextCell {
@ -31,9 +31,11 @@ impl TextCell {
/// Creates a new text cell that holds the given text in the given style,
/// computing the Unicode width of the text.
pub fn paint(style: Style, text: String) -> Self {
let width = DisplayWidth::from(&*text);
TextCell {
length: DisplayWidth::from(&*text),
contents: vec![ style.paint(text) ],
width: width,
}
}
@ -41,9 +43,11 @@ impl TextCell {
/// computing the Unicode width of the text. (This could be merged with
/// `paint`, but.)
pub fn paint_str(style: Style, text: &'static str) -> Self {
let width = DisplayWidth::from(text);
TextCell {
length: DisplayWidth::from(text),
contents: vec![ style.paint(text) ],
width: width,
}
}
@ -55,8 +59,8 @@ impl TextCell {
/// tabular data when there is *something* in each cell.
pub fn blank(style: Style) -> Self {
TextCell {
length: DisplayWidth::from(1),
contents: vec![ style.paint("-") ],
width: DisplayWidth::from(1),
}
}
@ -66,21 +70,21 @@ impl TextCell {
pub fn add_spaces(&mut self, count: usize) {
use std::iter::repeat;
(*self.length) += count;
(*self.width) += count;
let spaces: String = repeat(' ').take(count).collect();
self.contents.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>, length: usize) {
pub fn push(&mut self, string: ANSIString<'static>, extra_width: usize) {
self.contents.push(string);
(*self.length) += length;
(*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.length) += *other.length;
(*self.width) += *other.width;
self.contents.extend(other.contents);
}

View File

@ -279,10 +279,11 @@ impl Details {
for (index, egg) in file_eggs.into_iter().enumerate() {
let mut files = Vec::new();
let mut errors = egg.errors;
let width = DisplayWidth::from(&*egg.file.name);
let name = TextCell {
length: DisplayWidth::from(&*egg.file.name),
contents: filename(egg.file, &self.colours, true),
width: width,
};
let row = Row {
@ -361,7 +362,7 @@ impl Row {
/// not, returns 0.
fn column_width(&self, index: usize) -> usize {
match self.cells {
Some(ref cells) => *cells[index].length,
Some(ref cells) => *cells[index].width,
None => 0,
}
}
@ -460,9 +461,11 @@ impl<U> Table<U> where U: Users {
}
pub fn filename_cell(&self, file: File, links: bool) -> TextCell {
let width = DisplayWidth::from(&*file.name);
TextCell {
length: DisplayWidth::from(&*file.name),
contents: filename(file, &self.colours, links),
width: width,
}
}
@ -543,8 +546,8 @@ impl<U> Table<U> where U: Users {
let width = DisplayWidth::from(chars.len());
TextCell {
length: width,
contents: chars,
width: width,
}
}
@ -598,7 +601,7 @@ impl<U> Table<U> where U: Users {
let width = DisplayWidth::from(number.len() + symbol.len());
TextCell {
length: width,
width: width,
contents: vec![
self.colours.size.numbers.paint(number),
self.colours.size.unit.paint(symbol),
@ -631,7 +634,7 @@ impl<U> Table<U> where U: Users {
};
TextCell {
length: DisplayWidth::from(2),
width: DisplayWidth::from(2),
contents: vec![
git_char(git.staged),
git_char(git.unstaged)
@ -688,7 +691,7 @@ impl<U> Table<U> where U: Users {
if let Some(cells) = row.cells {
for (n, (this_cell, width)) in cells.into_iter().zip(column_widths.iter()).enumerate() {
let padding = width - *this_cell.length;
let padding = width - *this_cell.width;
match self.columns[n].alignment() {
Alignment::Left => { cell.append(this_cell); cell.add_spaces(padding); }

View File

@ -106,7 +106,7 @@ impl GridDetails {
if row < column.len() {
let cell = grid::Cell {
contents: ANSIStrings(&column[row].contents).to_string(),
width: *column[row].length,
width: *column[row].width,
};
grid.add(cell);
@ -119,7 +119,7 @@ impl GridDetails {
for cell in column.iter() {
let cell = grid::Cell {
contents: ANSIStrings(&cell.contents).to_string(),
width: *cell.length,
width: *cell.width,
};
grid.add(cell);