aboutsummaryrefslogtreecommitdiffstats
path: root/src/window.rs
blob: 178c484b1c074bdd5875be6654e5706a2a2a176b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//! A window is an abstraction for the UI. Buffers (like [Threads]) draw "to" windows.

use std::io::Write;
use termion::{clear, color, cursor};

pub enum Line {
    Normal(String),
    Highlight(String),
}

pub struct Window {
    pub lines: Vec<Line>,
}

impl Window {
    pub fn new() -> Self {
        Self {
            lines: Vec::new(),
        }
    }

    pub fn draw<W: Write>(&mut self, out: &mut W) -> Result<(), std::io::Error> {
        write!(out, "{}", clear::All)?;

        for (i, line) in self.lines.iter().enumerate() {
            write!(out, "{}", cursor::Goto(1, (i + 1) as u16))?;
            match line {
                Line::Normal(s) => write!(out, "{}", s)?,
                Line::Highlight(s) => write!(
                    out,
                    "{}{}{}",
                    color::Fg(color::Red),
                    s,
                    color::Fg(color::Reset),
                )?,
            }
        }
        out.flush()?;

        self.lines.clear();
        Ok(())
    }
}