1
use crate::{LineNumber, Position};
2
use std::fmt::Debug;
3
use std::io::Error;
4
use termion::color;
5
use termion::event::{Event, MouseEvent};
6

            
7
#[derive(Debug)]
8
#[allow(clippy::module_name_repetitions)]
9
pub struct ConsoleSize {
10
    pub height: u16,
11
    pub width: u16,
12
}
13

            
14
impl From<(u16, u16)> for ConsoleSize {
15
    fn from(t: (u16, u16)) -> Self {
16
        Self {
17
            height: t.1,
18
            width: t.0,
19
        }
20
    }
21
}
22

            
23
impl Default for ConsoleSize {
24
109
    fn default() -> Self {
25
109
        Self {
26
            height: 80,
27
            width: 120,
28
        }
29
218
    }
30
}
31

            
32
impl ConsoleSize {
33
    #[must_use]
34
    pub fn restrict_to_text_area(&self) -> Self {
35
        Self {
36
            height: self.height.saturating_sub(2),
37
            width: self.width,
38
        }
39
    }
40
}
41

            
42
// Note to self: ": Debug" means that all implementations of that traut
43
// must implement the Debug trait as well.
44
pub trait Console: Debug {
45
    /// Read the next event from the console input.termion
46
    ///
47
    /// # Errors
48
    /// Will return an error if an event can't be read from the console input.
49
    fn read_event(&mut self) -> Result<Event, Error>;
50

            
51
    fn clear_screen(&self);
52

            
53
    fn clear_current_line(&self);
54

            
55
    /// # Errors
56
    /// Will return an error if the terminal can't be flushed
57
    fn flush(&self) -> Result<(), Error>;
58

            
59
    fn hide_cursor(&self);
60

            
61
    fn show_cursor(&self);
62

            
63
    fn set_bg_color(&self, color: color::Rgb);
64

            
65
    fn reset_bg_color(&self);
66

            
67
    fn set_fg_color(&self, color: color::Rgb);
68

            
69
    fn reset_fg_color(&self);
70

            
71
    fn to_alternate_screen(&self);
72

            
73
    fn to_main_screen(&self);
74

            
75
    fn clear_all(&self);
76

            
77
    fn size(&self) -> ConsoleSize;
78

            
79
    fn text_area_size(&self) -> ConsoleSize;
80

            
81
    fn middle_of_screen_line_number(&self) -> LineNumber;
82

            
83
    fn bottom_of_screen_line_number(&self) -> LineNumber;
84

            
85
    fn get_cursor_index_from_mouse_event(&self, mouse_event: MouseEvent, x_offset: u8) -> Position;
86

            
87
    fn set_cursor_position_in_text_area(&self, position: &Position, row_prefix_length: u8);
88

            
89
    fn set_cursor_position_anywhere(&self, position: &Position);
90

            
91
    fn set_cursor_as_steady_bar(&self);
92

            
93
    fn set_cursor_as_steady_block(&self);
94
}