summaryrefslogtreecommitdiffstats
path: root/src/display/screen.rs
blob: 3ea1fe059873dea4095240300af2135a50487678 (plain)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use {
    crate::{
        app::AppContext,
        conf::Conf,
        errors::ProgramError,
    },
    crossterm::{
        cursor,
        terminal::{Clear, ClearType},
        QueueableCommand,
    },
    super::W,
    termimad::Area,
};

pub struct Screen {
    pub width: u16,
    pub height: u16,
}

impl Screen {
    pub fn new(con: &AppContext, _conf: &Conf) -> Result<Screen, ProgramError> {
        let mut screen = Screen {
            width: 0,
            height: 0,
        };
        screen.read_size(con)?;
        Ok(screen)
    }
    pub fn set_terminal_size(&mut self, w: u16, h: u16, con: &AppContext) {
        self.width = w;
        self.height = h;
        if let Some(h) = con.launch_args.height {
            self.height = h;
        }
    }
    pub fn read_size(&mut self, con: &AppContext) -> Result<(), ProgramError> {
        let (w, h) = termimad::terminal_size();
        self.set_terminal_size(w, h, con);
        Ok(())
    }
    /// move the cursor to x,y
    pub fn goto(&self, w: &mut W, x: u16, y: u16) -> Result<(), ProgramError> {
        w.queue(cursor::MoveTo(x, y))?;
        Ok(())
    }
    /// clear from the cursor to the end of line
    pub fn clear_line(&self, w: &mut W) -> Result<(), ProgramError> {
        w.queue(Clear(ClearType::UntilNewLine))?;
        Ok(())
    }
    /// clear the area and everything to the right.
    /// Should be used with parcimony as it could lead to flickering.
    pub fn clear_area_to_right(&self, w: &mut W, area: &Area)  -> Result<(), ProgramError> {
        for y in area.top..area.top+area.height {
            self.goto(w, area.left, y)?;
            self.clear_line(w)?;
        }
        Ok(())
    }
}