Extract method for making a cell from its contents

This commit is contained in:
Benjamin Sago 2017-05-02 18:16:21 +01:00
parent ba1c8c650f
commit cac80410c9
4 changed files with 16 additions and 20 deletions

View File

@ -160,10 +160,21 @@ impl TextCellContents {
ANSIStrings(&self.0)
}
/// Calculates the width that a cell with these contents would take up, by
/// counting the number of characters in each unformatted ANSI string.
pub fn width(&self) -> DisplayWidth {
let foo = self.0.iter().map(|anstr| anstr.chars().count()).sum();
DisplayWidth(foo)
}
/// Promotes these contents to a full cell containing them alongside
/// their calculated width.
pub fn promote(self) -> TextCell {
TextCell {
width: self.width(),
contents: self,
}
}
}

View File

@ -99,7 +99,7 @@ use fs::feature::xattr::{Attribute, FileAttributes};
use options::{FileFilter, RecurseOptions};
use output::colours::Colours;
use output::column::{Alignment, Column, Columns, SizeFormat};
use output::cell::{TextCell, DisplayWidth};
use output::cell::{TextCell, TextCellContents, DisplayWidth};
use output::tree::TreeTrunk;
use output::file_name::FileName;
@ -307,18 +307,10 @@ impl Details {
let mut files = Vec::new();
let mut errors = egg.errors;
let filename = FileName::new(&egg.file, &self.colours).paint(true, self.classify);
let width = filename.width();
let name = TextCell {
contents: filename,
width: width,
};
let row = Row {
depth: depth,
cells: Some(egg.cells),
name: name,
name: FileName::new(&egg.file, &self.colours).paint(true, self.classify).promote(),
last: index == num_eggs - 1,
};
@ -451,14 +443,8 @@ impl<'a, U: Users+Groups+'a> Table<'a, U> {
self.rows.push(row);
}
pub fn filename_cell(&self, file: File, links: bool) -> TextCell {
let filename = FileName::new(&file, &self.opts.colours).paint(links, self.opts.classify);
let width = filename.width();
TextCell {
contents: filename,
width: width,
}
pub fn filename(&self, file: File, links: bool) -> TextCellContents {
FileName::new(&file, &self.opts.colours).paint(links, self.opts.classify)
}
pub fn add_file_with_cells(&mut self, cells: Vec<TextCell>, name_cell: TextCell, depth: usize, last: bool) {

View File

@ -3,7 +3,6 @@ use std::io::{Write, Result as IOResult};
use term_grid as grid;
use fs::File;
use output::DisplayWidth;
use output::colours::Colours;
use output::file_name::FileName;

View File

@ -45,7 +45,7 @@ impl GridDetails {
.collect::<Vec<_>>();
let file_names = files.into_iter()
.map(|file| first_table.filename_cell(file, false))
.map(|file| first_table.filename(file, false).promote())
.collect::<Vec<_>>();
(cells, file_names)