summaryrefslogtreecommitdiffstats
path: root/src/term.rs
blob: b2b7f873eb34ec5231131025bdde9c19441e5769 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use super::config::Colorname;
use super::errors::FxError;

use crossterm::cursor::{Hide, MoveLeft, MoveRight, MoveTo, Show};
use crossterm::style::{Color, ResetColor, SetBackgroundColor, SetForegroundColor};
use crossterm::terminal::Clear;

pub enum TermColor<'a> {
    ForeGround(&'a Colorname),
    BackGround(&'a Colorname),
}

/// Puts the terminal into raw mode. Requires calling `leave_raw_mode` on program exit!
///
/// **Warning!** Not calling `leave_raw_mode` will leave *nix terminals in an unusable state!
///
/// Changing the underlying terminal to raw mode is needed to allow for direct input.
/// This change is not undone automatically on program exit and must be managed gracefully to avoid
/// leaving the user with a broken terminal.
///
pub fn enter_raw_mode() {
    crossterm::terminal::enable_raw_mode().ok();
    hide_cursor();
}

pub fn leave_raw_mode() {
    show_cursor();
    crossterm::terminal::disable_raw_mode().ok();
}

pub fn terminal_size() -> Result<(u16, u16), FxError> {
    crossterm::terminal::size().map_err(|_| FxError::TerminalSizeDetection)
}

pub fn cursor_pos() -> Result<(u16, u16), FxError> {
    Ok(crossterm::cursor::position()?)
}

pub fn move_to(x: u16, y: u16) {
    print!("{}", MoveTo(x - 1, y - 1));
}

pub fn to_info_line() {
    move_to(2, 2);
}

pub fn clear_current_line() {
    print!("{}", Clear(crossterm::terminal::ClearType::CurrentLine));
}

pub fn go_to_info_line_and_reset() {
    to_info_line();
    clear_current_line();
}

pub fn clear_until_newline() {
    print!("{}", Clear(crossterm::terminal::ClearType::UntilNewLine));
}

pub fn clear_all() {
    print!("{}", Clear(crossterm::terminal::ClearType::All));
}

pub fn move_left(x: u16) {
    print!("{}", MoveLeft(x));
}

pub fn move_right(x: u16) {
    print!("{}", MoveRight(x));
}

pub fn hide_cursor() {
    print!("{}", Hide);
}

pub fn show_cursor() {
    print!("{}", Show);
}

pub fn print_pointer() {
    print!(">");
    move_left(1);
}

pub fn delete_pointer() {
    print!(" ");
    move_left(1);
}

pub fn set_color(c: &TermColor) {
    match c {
        TermColor::ForeGround(c) => match c {
            Colorname::Black => print!("{}", SetForegroundColor(Color::Black)),
            Colorname::Red => print!("{}", SetForegroundColor(Color::DarkRed)),
            Colorname::Green => print!("{}", SetForegroundColor(Color::DarkGreen)),
            Colorname::Yellow => print!("{}", SetForegroundColor(Color::DarkYellow)),
            Colorname::Blue => print!("{}", SetForegroundColor(Color::DarkBlue)),
            Colorname::Magenta => print!("{}", SetForegroundColor(Color::DarkMagenta)),
            Colorname::Cyan => print!("{}", SetForegroundColor(Color::DarkCyan)),
            Colorname::White => print!("{}", SetForegroundColor(Color::Grey)),
            Colorname::LightBlack => print!("{}", SetForegroundColor(Color::DarkGrey)),
            Colorname::LightRed => print!("{}", SetForegroundColor(Color::Red)),
            Colorname::LightGreen => print!("{}", SetForegroundColor(Color::Green)),
            Colorname::LightYellow => print!("{}", SetForegroundColor(Color::Yellow)),
            Colorname::LightBlue => print!("{}", SetForegroundColor(Color::Blue)),
            Colorname::LightMagenta => print!("{}", SetForegroundColor(Color::Magenta)),
            Colorname::LightCyan => print!("{}", SetForegroundColor(Color::Cyan)),
            Colorname::LightWhite => print!("{}", SetForegroundColor(Color::White)),
            Colorname::Rgb(r, g, b) => print!(
                "{}",
                SetForegroundColor(Color::Rgb {
                    r: *r,
                    g: *g,
                    b: *b
                })
            ),
            Colorname::AnsiValue(n) => print!("{}", SetForegroundColor(Color::AnsiValue(*n))),
        },
        TermColor::BackGround(c) => match c {
            Colorname::Black => print!("{}", SetBackgroundColor(Color::Black)),
            Colorname::Red => print!("{}", SetBackgroundColor(Color::DarkRed)),
            Colorname::Green => print!("{}", SetBackgroundColor(Color::DarkGreen)),
            Colorname::Yellow => print!("{}", SetBackgroundColor(Color::DarkYellow)),
            Colorname::Blue => print!("{}", SetBackgroundColor(Color::DarkBlue)),
            Colorname::Magenta => print!("{}", SetBackgroundColor(Color::DarkMagenta)),
            Colorname::Cyan => print!("{}", SetBackgroundColor(Color::DarkCyan)),
            Colorname::White => print!("{}", SetBackgroundColor(Color::Grey)),
            Colorname::LightBlack => print!("{}", SetBackgroundColor(Color::DarkGrey)),
            Colorname::LightRed => print!("{}", SetBackgroundColor(Color::Red)),
            Colorname::LightGreen => print!("{}", SetBackgroundColor(Color::Green)),
            Colorname::LightYellow => print!("{}", SetBackgroundColor(Color::Yellow)),
            Colorname::LightBlue => print!("{}", SetBackgroundColor(Color::Blue)),
            Colorname::LightMagenta => print!("{}", SetBackgroundColor(Color::Magenta)),
            Colorname::LightCyan => print!("{}", SetBackgroundColor(Color::Cyan)),
            Colorname::LightWhite => print!("{}", SetBackgroundColor(Color::White)),
            Colorname::Rgb(r, g, b) => print!(
                "{}",
                SetBackgroundColor(Color::Rgb {
                    r: *r,
                    g: *g,
                    b: *b
                })
            ),
            Colorname::AnsiValue(n) => print!("{}", SetBackgroundColor(Color::AnsiValue(*n))),
        },
    }
}

pub fn set_color_current_dir() {
    set_color(&TermColor::ForeGround(&Colorname::Cyan));
}

pub fn set_color_read_only() {
    set_color(&TermColor::ForeGround(&Colorname::Red));
}

pub fn set_color_git_repo() {
    set_color(&TermColor::ForeGround(&Colorname::LightMagenta));
}

pub fn reset_color() {
    print!("{}", ResetColor);
}