summaryrefslogtreecommitdiffstats
path: root/src/display/virtual_curses.rs
blob: 6497de0c205749d793d3938411f014b07af8ac2c (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
use crate::display::color_mode::ColorMode;
use crate::display::utils::detect_color_mode;
use lazy_static::lazy_static;
pub use pancurses::{chtype, Input};
use std::cell::RefCell;
use std::sync::Mutex;

pub const A_DIM: chtype = 64;
pub const A_REVERSE: chtype = 128;
pub const A_UNDERLINE: chtype = 256;
pub const COLOR_BLACK: i16 = 0;
pub const COLOR_RED: i16 = 1;
pub const COLOR_GREEN: i16 = 2;
pub const COLOR_YELLOW: i16 = 3;
pub const COLOR_BLUE: i16 = 4;
pub const COLOR_MAGENTA: i16 = 5;
pub const COLOR_CYAN: i16 = 6;
pub const COLOR_WHITE: i16 = 7;

lazy_static! {
	static ref OUTPUT: Mutex<Vec<String>> = Mutex::new(vec![]);
}

#[derive(Copy, Clone, Debug, PartialEq)]
pub enum State {
	Normal,
	Saved,
	Ended,
	Refreshed,
	Resized,
}

pub struct Curses {
	attributes: RefCell<chtype>,
	color_mode: ColorMode,
	color_pairs: [(i16, i16); 255],
	colors: [(i16, i16, i16); 255],
	input: RefCell<Vec<Input>>,
	position: RefCell<(i32, i32)>,
	size: RefCell<(i32, i32)>,
	state: RefCell<State>,
}

impl Curses {
	pub(crate) fn new() -> Self {
		Self {
			attributes: RefCell::new(0),
			color_mode: detect_color_mode(16),
			color_pairs: [(0, 0); 255],
			colors: [(0, 0, 0); 255],
			input: RefCell::new(vec![Input::KeyExit]),
			position: RefCell::new((0, 0)),
			size: RefCell::new((10, 10)),
			state: RefCell::new(State::Normal),
		}
	}

	// Start mock access functions

	pub(crate) const fn get_colors(&self) -> &[(i16, i16, i16); 255] {
		&self.colors
	}

	pub(crate) fn get_output() -> Vec<String> {
		OUTPUT.lock().unwrap().clone()
	}

	pub(crate) const fn get_color_pairs(&self) -> &[(i16, i16); 255] {
		&self.color_pairs
	}

	pub(crate) fn get_state(&self) -> State {
		*self.state.borrow()
	}

	pub(crate) fn is_color_enabled(&self, color_index: chtype) -> bool {
		(*self.attributes.borrow() & color_index) == color_index
	}

	pub(crate) fn is_dimmed(&self) -> bool {
		(*self.attributes.borrow() & A_DIM) == A_DIM
	}

	pub(crate) fn is_reverse(&self) -> bool {
		(*self.attributes.borrow() & A_REVERSE) == A_REVERSE
	}

	pub(crate) fn is_underline(&self) -> bool {
		(*self.attributes.borrow() & A_UNDERLINE) == A_UNDERLINE
	}

	pub(crate) fn set_inputs(&self, mut input: Vec<Input>) {
		input.reverse();
		self.input.replace(input);
	}

	pub(crate) fn set_color_mode(&mut self, color_mode: ColorMode) {
		self.color_mode = color_mode;
	}

	// End mock access functions

	pub(super) fn init_color(&mut self, index: i16, red: i16, green: i16, blue: i16) {
		self.colors[index as usize] = (red, green, blue);
	}

	pub(super) fn init_color_pair(&mut self, index: i16, foreground: i16, background: i16) -> chtype {
		self.color_pairs[index as usize] = (foreground, background);
		index as chtype
	}

	pub(super) const fn get_color_mode(&self) -> &ColorMode {
		&self.color_mode
	}

	#[allow(clippy::unused_self)]
	pub(super) fn erase(&self) {
		OUTPUT.lock().unwrap().clear();
	}

	pub(super) fn refresh(&self) {
		self.state.replace(State::Refreshed);
	}

	#[allow(clippy::unused_self)]
	pub(super) fn addstr(&self, s: &str) {
		OUTPUT.lock().unwrap().push(String::from(s));
	}

	pub(super) fn attrset<T: Into<chtype>>(&self, attributes: T) {
		let attrs = attributes.into();
		self.attributes.replace(attrs);
	}

	pub(super) fn attron<T: Into<chtype>>(&self, attribute: T) {
		let attr = attribute.into();
		let old_attr = *self.attributes.borrow();
		self.attributes.replace(old_attr | attr);
	}

	pub(super) fn attroff<T: Into<chtype>>(&self, attribute: T) {
		let attr = attribute.into();
		let old_attr = *self.attributes.borrow();
		self.attributes.replace(old_attr & !attr);
	}

	pub(super) fn getch(&self) -> Option<Input> {
		self.input.borrow_mut().pop()
	}

	pub(crate) fn get_cur_y(&self) -> i32 {
		(*self.position.borrow()).1
	}

	pub(crate) fn get_cur_x(&self) -> i32 {
		(*self.position.borrow()).0
	}

	pub(super) fn get_max_x(&self) -> i32 {
		(*self.size.borrow()).0
	}

	pub(super) fn get_max_y(&self) -> i32 {
		(*self.size.borrow()).1
	}

	#[allow(clippy::unused_self)]
	pub(crate) fn hline(&self, ch: char, width: i32) {
		OUTPUT.lock().unwrap().push(format!("{{HLINE|{}|{}}}", ch, width));
	}

	pub(crate) fn mv(&self, y: i32, x: i32) {
		self.position.replace((x, y));
	}

	pub(crate) fn resize_term(&self, nlines: i32, ncols: i32) {
		if nlines == 0 && ncols == 0 {
			self.state.replace(State::Resized);
		}
		else {
			self.size.replace((ncols, nlines));
		}
	}

	pub(super) fn def_prog_mode(&self) {
		self.state.replace(State::Saved);
	}

	pub(super) fn reset_prog_mode(&self) {
		self.state.replace(State::Normal);
	}

	pub(super) fn endwin(&self) {
		self.state.replace(State::Ended);
	}
}