From 56114e95d944ec14510213e4c3bb23b2faa4d208 Mon Sep 17 00:00:00 2001 From: Ben S Date: Sun, 28 Jun 2015 21:22:39 +0100 Subject: [PATCH] Add some new methods for exa --- Cargo.toml | 2 +- src/lib.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 2e22ba9..62d927c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/lib.rs b/src/lib.rs index a07783d..53e245d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -177,6 +177,12 @@ struct Dimensions { widths: Vec, } +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 {