diff options
Diffstat (limited to 'src/term/cell.rs')
-rw-r--r-- | src/term/cell.rs | 43 |
1 files changed, 41 insertions, 2 deletions
diff --git a/src/term/cell.rs b/src/term/cell.rs index df648294..a95875bf 100644 --- a/src/term/cell.rs +++ b/src/term/cell.rs @@ -11,11 +11,11 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. - - use std::mem; use ansi::{NamedColor, Color}; +use grid; +use index::Column; bitflags! { #[derive(Serialize, Deserialize)] @@ -35,6 +35,27 @@ pub struct Cell { pub flags: Flags, } +/// Get the length of occupied cells in a line +pub trait LineLength { + /// Calculate the occupied line length + fn line_length(&self) -> Column; +} + +impl LineLength for grid::Row<Cell> { + fn line_length(&self) -> Column { + let mut length = Column(0); + + for (index, cell) in self[..].iter().rev().enumerate() { + if cell.c != ' ' { + length = Column(self.len() - index); + break; + } + } + + length + } +} + impl Cell { pub fn bold(&self) -> bool { self.flags.contains(BOLD) @@ -67,3 +88,21 @@ impl Cell { mem::swap(&mut self.fg, &mut self.bg); } } + +#[cfg(test)] +mod tests { + use super::{Cell, LineLength}; + + use grid::Row; + use index::Column; + use ansi::Color; + + #[test] + fn line_length_works() { + let template = Cell::new(' ', Color::Indexed(0), Color::Indexed(0)); + let mut row = Row::new(Column(10), &template); + row[Column(5)].c = 'a'; + + assert_eq!(row.line_length(), Column(6)); + } +} |