summaryrefslogtreecommitdiffstats
path: root/src/screens.rs
blob: c4bb56bd8fa0cf630a5a117f99a899338dfdb79e (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use std::io::Write;

use crossterm::{
    cursor,
    terminal::{Clear, ClearType},
    queue,
};
use termimad::{Area, CompoundStyle, InputField};

use crate::{
    app::W,
    app_context::AppContext,
    errors::ProgramError,
    skin::Skin,
};

pub struct Screen {
    pub width: u16,
    pub height: u16,
    pub skin: Skin,
    pub input_field: InputField,
}

impl Screen {
    pub fn new(con: &AppContext, skin: Skin) -> Result<Screen, ProgramError> {
        let mut input_field = InputField::new(Area::new(0, 0, 10, 1));
        input_field.set_normal_style(CompoundStyle::from(skin.input.clone()));
        let mut screen = Screen {
            width: 0,
            height: 0,
            skin,
            input_field,
        };
        screen.read_size(con)?;
        Ok(screen)
    }
    pub fn read_size(&mut self, con: &AppContext) -> Result<(), ProgramError> {
        let (w, h) = termimad::terminal_size();
        self.width = w;
        self.height = h;
        if let Some(h) = con.launch_args.height {
            self.height = h;
        }
        debug!("screen size: {} x {}", self.width, self.height);
        self.input_field.change_area(0, h-1, w - 15);
        debug!("input_field area: {:?}", self.input_field.area);
        Ok(())
    }
    /// move the cursor to x,y and clears the line.
    pub fn goto_clear(&self, w: &mut W, x: u16, y: u16)
    -> Result<(), ProgramError> {
        self.goto(w, x, y)?;
        self.clear_line(w)
    }
    /// move the cursor to x,y
    pub fn goto(
        &self,
        w: &mut W,
        x: u16,
        y: u16
    ) -> Result<(), ProgramError> {
        queue!(w, cursor::MoveTo(x, y))?;
        Ok(())
    }
    /// clear the whole screen
    pub fn clear(&self, w: &mut W) -> Result<(), ProgramError> {
        queue!(w, Clear(ClearType::All))?;
        Ok(())
    }
    /// clear from the cursor to the end of line
    pub fn clear_line(&self, w: &mut W) -> Result<(), ProgramError> {
        queue!(w, Clear(ClearType::UntilNewLine))?;
        Ok(())
    }
}