1use crate::style::{Style, StyleCursor};
2use std::fmt::{Debug, Display, Error, Formatter};
3use term::{self, Terminal};
4
5pub struct Row {
6 text: String,
7 styles: Vec<Style>,
8}
9
10impl Row {
11 pub fn new(chars: &[char], styles: &[Style]) -> Row {
12 assert_eq!(chars.len(), styles.len());
13 Row {
14 text: chars.iter().cloned().collect(),
15 styles: styles.to_vec(),
16 }
17 }
18
19 pub fn write_to<T: Terminal + ?Sized>(&self, term: &mut T) -> term::Result<()> {
20 let mut cursor = StyleCursor::new(term)?;
21 for (character, &style) in self.text.trim_end().chars().zip(&self.styles) {
22 cursor.set_style(style)?;
23 write!(cursor.term(), "{}", character)?;
24 }
25 Ok(())
26 }
27}
28
29impl Display for Row {
32 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
33 Display::fmt(self.text.trim_end(), fmt)
34 }
35}
36
37impl Debug for Row {
38 fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), Error> {
39 write!(fmt, "\"")?;
41 Display::fmt(self.text.trim_end(), fmt)?;
42 write!(fmt, "\"")
43 }
44}