-
Notifications
You must be signed in to change notification settings - Fork 44
/
mod.rs
199 lines (169 loc) · 5.22 KB
/
mod.rs
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
197
198
199
#![allow(non_camel_case_types, non_snake_case)]
pub mod constants;
use self::constants::*;
use ncurses::{box_, getmouse, keyname, setlocale, LcCategory, COLORS, COLOR_PAIRS};
use ncurses::ll::{chtype, ungetch, wattroff, wattron, wattrset, MEVENT, NCURSES_ATTR_T, WINDOW};
use ncurses::ll::{resize_term, wgetch, wmouse_trafo};
use libc::c_int;
use input::Input;
use std::string::FromUtf8Error;
pub fn pre_init() {
setlocale(LcCategory::all, "");
}
pub fn _attron(w: WINDOW, attributes: chtype) -> i32 {
unsafe { wattron(w, attributes as NCURSES_ATTR_T) }
}
pub fn _attroff(w: WINDOW, attributes: chtype) -> i32 {
unsafe { wattroff(w, attributes as NCURSES_ATTR_T) }
}
pub fn _attrset(w: WINDOW, attributes: chtype) -> i32 {
unsafe { wattrset(w, attributes as NCURSES_ATTR_T) }
}
pub fn _COLORS() -> i32 {
COLORS()
}
pub fn _COLOR_PAIRS() -> i32 {
COLOR_PAIRS()
}
pub fn _draw_box(w: WINDOW, verch: chtype, horch: chtype) -> i32 {
box_(w, verch, horch)
}
pub fn _getmouse() -> Result<MEVENT, i32> {
let mut mevent = MEVENT {
id: 0,
x: 0,
y: 0,
z: 0,
bstate: 0,
};
let error = getmouse(&mut mevent);
if error == 0 {
Ok(mevent)
} else {
Err(error)
}
}
pub fn _keyname(code: i32) -> Option<String> {
keyname(code)
}
pub fn _mouse_trafo(w: &mut WINDOW, y: &mut i32, x: &mut i32, to_screen: bool) {
unsafe {
wmouse_trafo(w, y, x, to_screen as u8);
}
}
pub fn _resize_term(nlines: i32, ncols: i32) -> i32 {
unsafe { resize_term(nlines, ncols) }
}
pub fn _set_blink(_: bool) -> i32 {
0 // Not supported
}
pub fn _set_title(_: &str) {
//Not supported
}
/// Converts an integer returned by getch() to a Input value
pub fn to_special_keycode(i: i32) -> Option<Input> {
let index = if i <= KEY_F15 {
i - KEY_OFFSET
} else {
i - KEY_OFFSET - 48
};
if index < 0 || index as usize >= SPECIAL_KEY_CODES.len() {
None
} else {
Some(SPECIAL_KEY_CODES[index as usize])
}
}
pub fn _ungetch(input: &Input) -> i32 {
match *input {
Input::Character(c) => {
// Need to convert to UTF-8 bytes, it's how we get them from getch()
let mut utf8_buffer = [0; 4];
c.encode_utf8(&mut utf8_buffer)
.as_bytes()
.into_iter()
.rev()
.map(|x| unsafe { ungetch(*x as c_int) })
.fold(0, |res, x| i32::min(res, x))
}
Input::Unknown(i) => unsafe { ungetch(i) },
specialKeyCode => {
for (i, skc) in SPECIAL_KEY_CODES.into_iter().enumerate() {
if *skc == specialKeyCode {
let result = i as c_int + KEY_OFFSET;
if result <= KEY_F15 {
return unsafe { ungetch(result) };
} else {
return unsafe { ungetch(result + 48) };
}
}
}
panic!("Failed to convert Input back to a c_int");
}
}
}
pub fn _wgetch(w: WINDOW) -> Option<Input> {
let i = unsafe { wgetch(w) };
if i < 0 {
None
} else {
Some(to_special_keycode(i).unwrap_or_else(|| {
// Assume that on Linux input is UTF-8
fn try_decode(mut v: Vec<u8>, w: WINDOW) -> Result<String, FromUtf8Error> {
let res = String::from_utf8(v.clone());
if res.is_err() && v.len() < 4 {
let next_byte = unsafe { wgetch(w) };
v.push(next_byte as u8);
try_decode(v, w)
} else {
res
}
}
let v = vec![i as u8];
try_decode(v, w)
.map(|s| Input::Character(s.chars().next().unwrap()))
.unwrap_or_else(|error| {
warn!("Decoding input as UTF-8 failed: {:?}", error);
Input::Unknown(i)
})
}))
}
}
#[cfg(test)]
mod tests {
use super::*;
use input::Input;
use ncurses::{endwin, initscr};
#[test]
fn test_key_dl_to_special_keycode() {
let keyDl = 0o510;
assert_eq!(Input::KeyDL, to_special_keycode(keyDl).unwrap());
}
#[test]
fn test_key_f15_to_input() {
let keyF15 = 0o410 + 15;
assert_eq!(Input::KeyF15, to_special_keycode(keyF15).unwrap());
}
#[test]
fn test_key_up_to_input() {
let keyUp = 0o403;
assert_eq!(Input::KeyUp, to_special_keycode(keyUp).unwrap());
}
#[test]
fn test_ungetch() {
let w = initscr();
let chars = [
'a', 'b', 'c', 'ä', 'ö', 'å', 'A', 'B', 'C', 'Ä', 'Ö', 'Å', '𤭢', '𐍈',
'€', 'ᚠ', 'ᛇ', 'ᚻ', 'þ', 'ð', 'γ', 'λ', 'ώ', 'б', 'е', 'р', 'ვ',
'ე', 'პ', 'ხ', 'இ', 'ங', 'க', 'ಬ', 'ಇ', 'ಲ', 'ಸ',
];
chars.into_iter().for_each(|c| {
_ungetch(&Input::Character(*c));
assert_eq!(_wgetch(w).unwrap(), Input::Character(*c));
});
SPECIAL_KEY_CODES.into_iter().for_each(|i| {
_ungetch(i);
assert_eq!(_wgetch(w).unwrap(), *i);
});
endwin();
}
}