Add some new methods for exa

This commit is contained in:
Ben S
2015-06-28 21:30:33 +01:00
parent 7dcdaeba4d
commit 56114e95d9
2 changed files with 27 additions and 1 deletions
+1 -1
View File
@@ -7,7 +7,7 @@ documentation = "http://bsago.me/doc/term_grid/"
homepage = "https://github.com/ogham/rust-term-grid"
license = "MIT"
readme = "README.md"
version = "0.1.0"
version = "0.1.1"
[lib]
name = "term_grid"
+26
View File
@@ -177,6 +177,12 @@ struct Dimensions {
widths: Vec<Width>,
}
impl Dimensions {
pub fn total_width(&self, separator_width: Width) -> Width {
sum(self.widths.iter().map(|&x| x)) + separator_width * (self.widths.len() - 1)
}
}
/// Everything needed to format the cells with the grid options.
///
@@ -302,6 +308,26 @@ pub struct Display<'grid> {
dimensions: Dimensions,
}
impl<'grid> Display<'grid> {
/// Returns how many columns this display takes up, based on the separator
/// width and the number and width of the columns.
pub fn width(&self) -> Width {
self.dimensions.total_width(self.grid.options.separator_width)
}
/// Returns whether this display takes up as many columns as were allotted
/// to it.
///
/// It's possible to construct tables that don't actually use up all the
/// columns that they could, such as when there are more columns than
/// cells! In this case, a column would have a width of zero. This just
/// checks for that.
pub fn is_complete(&self) -> bool {
self.dimensions.widths.iter().all(|&x| x > 0)
}
}
impl<'grid> fmt::Display for Display<'grid> {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
for y in 0 .. self.dimensions.num_lines {