forked from skim-rs/skim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurses.rs
480 lines (420 loc) · 14.6 KB
/
curses.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
// An abstract layer towards ncurses-rs, which provides keycode, color scheme support
// Modeled after fzf
use ncurses::*;
use getopts;
use std::sync::RwLock;
use std::collections::HashMap;
use libc::{STDIN_FILENO, STDERR_FILENO, fdopen, c_char};
//use std::io::Write;
macro_rules! println_stderr(
($($arg:tt)*) => { {
let r = writeln!(&mut ::std::io::stderr(), $($arg)*);
r.expect("failed printing to stderr");
} }
);
pub static COLOR_NORMAL: i16 = 0;
pub static COLOR_PROMPT: i16 = 1;
pub static COLOR_MATCHED: i16 = 2;
pub static COLOR_CURRENT: i16 = 3;
pub static COLOR_CURRENT_MATCH: i16 = 4;
pub static COLOR_SPINNER: i16 = 5;
pub static COLOR_INFO: i16 = 6;
pub static COLOR_CURSOR: i16 = 7;
pub static COLOR_SELECTED: i16 = 8;
pub static COLOR_HEADER: i16 = 9;
static COLOR_USER: i16 = 10;
lazy_static! {
static ref COLOR_MAP: RwLock<HashMap<i16, attr_t>> = RwLock::new(HashMap::new());
static ref FG: RwLock<i16> = RwLock::new(7);
static ref BG: RwLock<i16> = RwLock::new(0);
static ref USE_COLOR: RwLock<bool> = RwLock::new(true);
}
pub fn init(theme: Option<&ColorTheme>, is_black: bool, _use_mouse: bool) {
// initialize ncurses
let mut use_color = USE_COLOR.write().unwrap();
if let Some(theme) = theme {
let base_theme = if tigetnum("colors") >= 256 {
DARK256
} else {
DEFAULT16
};
init_pairs(&base_theme, theme, is_black);
*use_color = true;
} else {
*use_color = false;
}
}
fn init_pairs(base: &ColorTheme, theme: &ColorTheme, is_black: bool) {
let mut fg = FG.write().unwrap();
let mut bg = BG.write().unwrap();
*fg = shadow(base.fg, theme.fg);
*bg = shadow(base.bg, theme.bg);
if is_black {
*bg = COLOR_BLACK;
} else if theme.use_default {
*fg = COLOR_DEFAULT;
*bg = COLOR_DEFAULT;
use_default_colors();
}
if !theme.use_default {
assume_default_colors(shadow(base.fg, theme.fg) as i32, shadow(base.bg, theme.bg) as i32);
}
start_color();
init_pair(COLOR_PROMPT, shadow(base.prompt, theme.prompt), *bg);
init_pair(COLOR_MATCHED, shadow(base.matched, theme.matched), shadow(base.matched_bg, theme.matched_bg));
init_pair(COLOR_CURRENT, shadow(base.current, theme.current), shadow(base.current_bg, theme.current_bg));
init_pair(COLOR_CURRENT_MATCH, shadow(base.current_match, theme.current_match), shadow(base.current_match_bg, theme.current_match_bg));
init_pair(COLOR_SPINNER, shadow(base.spinner, theme.spinner), *bg);
init_pair(COLOR_INFO, shadow(base.info, theme.info), *bg);
init_pair(COLOR_CURSOR, shadow(base.cursor, theme.cursor), shadow(base.current_bg, theme.current_bg));
init_pair(COLOR_SELECTED, shadow(base.selected, theme.selected), shadow(base.current_bg, theme.current_bg));
init_pair(COLOR_HEADER, shadow(base.header, theme.header), shadow(base.bg, theme.bg));
}
pub fn get_color_pair(fg: i16, bg: i16) -> attr_t {
let fg = if fg == -1 { *FG.read().unwrap() } else {fg};
let bg = if bg == -1 { *BG.read().unwrap() } else {bg};
let key = (fg << 8) + bg;
let mut color_map = COLOR_MAP.write().unwrap();
let pair_num = color_map.len() as i16;
let pair = color_map.entry(key).or_insert_with(|| {
let next_pair = COLOR_USER + pair_num;
init_pair(next_pair, fg, bg);
COLOR_PAIR(next_pair)
});
*pair
}
#[derive(PartialEq, Eq, Clone, Debug, Copy)]
pub enum Margin {
Fixed(i32),
Percent(i32),
}
pub struct Curses {
screen: SCREEN,
top: i32,
bottom: i32,
left: i32,
right: i32,
margin_top: Margin,
margin_bottom: Margin,
margin_left: Margin,
margin_right: Margin,
}
unsafe impl Send for Curses {}
impl Curses {
pub fn new() -> Self {
let local_conf = LcCategory::all;
setlocale(local_conf, "en_US.UTF-8"); // for showing wide characters
let stdin = unsafe { fdopen(STDIN_FILENO, "r".as_ptr() as *const c_char)};
let stderr = unsafe { fdopen(STDERR_FILENO, "w".as_ptr() as *const c_char)};
let screen = newterm(None, stderr, stdin);
set_term(screen);
//let screen = initscr();
raw();
noecho();
Curses {
screen: screen,
top: 0,
bottom: 0,
left: 0,
right: 0,
margin_top: Margin::Fixed(0),
margin_bottom: Margin::Fixed(0),
margin_left: Margin::Fixed(0),
margin_right: Margin::Fixed(0),
}
}
fn parse_margin(&self, margin: &str) -> Margin {
if margin.ends_with("%") {
Margin::Percent(margin[0..margin.len()-1].parse::<i32>().unwrap_or(100))
} else {
Margin::Fixed(margin.parse::<i32>().unwrap_or(0))
}
}
pub fn parse_options(&mut self, options: &getopts::Matches) {
if let Some(margin_option) = options.opt_str("margin") {
let margins = margin_option
.split(",")
.collect::<Vec<&str>>();
match margins.len() {
1 => {
let margin = self.parse_margin(margins[0]);
self.margin_top = margin;
self.margin_bottom = margin;
self.margin_left = margin;
self.margin_right = margin;
}
2 => {
let margin_tb = self.parse_margin(margins[0]);
self.margin_top = margin_tb;
self.margin_bottom = margin_tb;
let margin_rl = self.parse_margin(margins[1]);
self.margin_left = margin_rl;
self.margin_right = margin_rl;
}
3 => {
self.margin_top = self.parse_margin(margins[0]);
let margin_rl = self.parse_margin(margins[1]);
self.margin_left = margin_rl;
self.margin_right = margin_rl;
self.margin_bottom = self.parse_margin(margins[2]);
}
4 => {
self.margin_top = self.parse_margin(margins[0]);
self.margin_right = self.parse_margin(margins[1]);
self.margin_bottom = self.parse_margin(margins[2]);
self.margin_left = self.parse_margin(margins[3]);
}
_ => { }
}
self.resize();
}
}
fn get_color(&self, pair: i16, is_bold: bool) -> attr_t {
if *USE_COLOR.read().unwrap() {
attr_color(pair, is_bold)
} else {
attr_mono(pair, is_bold)
}
}
pub fn resize(&mut self) {
let mut max_y = 0;
let mut max_x = 0;
getmaxyx(stdscr(), &mut max_y, &mut max_x);
self.top = match self.margin_top {
Margin::Fixed(num) => num,
Margin::Percent(per) => per * max_y / 100,
};
self.bottom = match self.margin_bottom {
Margin::Fixed(num) => num,
Margin::Percent(per) => per * max_y / 100,
};
self.left = match self.margin_left {
Margin::Fixed(num) => num,
Margin::Percent(per) => per * max_x / 100,
};
self.right = match self.margin_right {
Margin::Fixed(num) => num,
Margin::Percent(per) => per * max_x / 100,
};
}
pub fn mv(&self, y: i32, x: i32) {
mv(y+self.top, x+self.left);
}
pub fn get_maxyx(&self) -> (i32, i32) {
let mut max_y = 0;
let mut max_x = 0;
getmaxyx(stdscr(), &mut max_y, &mut max_x);
(max_y-self.top-self.bottom, max_x-self.left-self.right)
}
pub fn getyx(&self) -> (i32, i32) {
let mut y = 0;
let mut x = 0;
getyx(stdscr(), &mut y, &mut x);
(y-self.top, x-self.left)
}
pub fn clrtoeol(&self) {
clrtoeol();
}
pub fn endwin(&self) {
endwin();
}
pub fn erase(&self) {
erase();
}
pub fn cprint(&self, text: &str, pair: i16, is_bold: bool) {
let attr = self.get_color(pair, is_bold);
attron(attr);
addstr(text);
attroff(attr);
}
pub fn caddch(&self, ch: char, pair: i16, is_bold: bool) {
let attr = self.get_color(pair, is_bold);
attron(attr);
addstr(&ch.to_string()); // to support wide character
attroff(attr);
}
pub fn printw(&self, text: &str) {
printw(text);
}
pub fn close(&self) {
endwin();
delscreen(self.screen);
}
pub fn attr_on(&self, attr: attr_t) {
if attr == 0 {
attrset(0);
} else {
attron(attr);
}
}
pub fn refresh(&self) {
refresh();
}
}
// use default if x is COLOR_UNDEFINED, else use x
fn shadow(default: i16, x: i16) -> i16 {
if x == COLOR_UNDEFINED { default } else { x }
}
fn attr_color(pair: i16, is_bold: bool) -> attr_t {
let attr = if pair > COLOR_NORMAL {COLOR_PAIR(pair)} else {0};
attr | if is_bold {A_BOLD()} else {0}
}
fn attr_mono(pair: i16, is_bold: bool) -> attr_t {
let mut attr = 0;
match pair {
x if x == COLOR_NORMAL => {
if is_bold {
attr = A_REVERSE();
}
}
x if x == COLOR_MATCHED => {
attr = A_UNDERLINE();
}
x if x == COLOR_CURRENT_MATCH => {
attr = A_UNDERLINE() | A_REVERSE()
}
_ => {}
}
attr | if is_bold {A_BOLD()} else {0}
}
const COLOR_DEFAULT: i16 = -1;
const COLOR_UNDEFINED: i16 = -2;
#[derive(Clone, Debug)]
pub struct ColorTheme {
use_default: bool,
fg: i16, // text fg
bg: i16, // text bg
matched: i16,
matched_bg: i16,
current: i16,
current_bg: i16,
current_match: i16,
current_match_bg: i16,
spinner: i16,
info: i16,
prompt: i16,
cursor: i16,
selected: i16,
header: i16,
}
impl ColorTheme {
pub fn new() -> Self {
ColorTheme {
use_default: true,
fg: COLOR_UNDEFINED,
bg: COLOR_UNDEFINED,
matched: COLOR_UNDEFINED,
matched_bg: COLOR_UNDEFINED,
current: COLOR_UNDEFINED,
current_bg: COLOR_UNDEFINED,
current_match: COLOR_UNDEFINED,
current_match_bg: COLOR_UNDEFINED,
spinner: COLOR_UNDEFINED,
info: COLOR_UNDEFINED,
prompt: COLOR_UNDEFINED,
cursor: COLOR_UNDEFINED,
selected: COLOR_UNDEFINED,
header: COLOR_UNDEFINED,
}
}
pub fn from_options(color: &str) -> Self {
let mut theme = ColorTheme::new();
for pair in color.split(',') {
let color: Vec<&str> = pair.split(':').collect();
if color.len() < 2 {
theme = match color[0] {
"molokai" => MONOKAI256.clone(),
"light" => LIGHT256.clone(),
"16" => DEFAULT16.clone(),
"dark" | _ => DARK256.clone(),
}
}
match color[0] {
"fg" => theme.fg = color[1].parse().unwrap_or(COLOR_UNDEFINED),
"bg" => theme.bg = color[1].parse().unwrap_or(COLOR_UNDEFINED),
"matched" => theme.matched = color[1].parse().unwrap_or(COLOR_UNDEFINED),
"matched_bg" => theme.matched_bg = color[1].parse().unwrap_or(COLOR_UNDEFINED),
"current" => theme.current = color[1].parse().unwrap_or(COLOR_UNDEFINED),
"current_bg" => theme.current_bg = color[1].parse().unwrap_or(COLOR_UNDEFINED),
"current_match" => theme.current_match = color[1].parse().unwrap_or(COLOR_UNDEFINED),
"current_match_bg" => theme.current_match_bg = color[1].parse().unwrap_or(COLOR_UNDEFINED),
"spinner" => theme.spinner = color[1].parse().unwrap_or(COLOR_UNDEFINED),
"info" => theme.info = color[1].parse().unwrap_or(COLOR_UNDEFINED),
"prompt" => theme.prompt = color[1].parse().unwrap_or(COLOR_UNDEFINED),
"cursor" => theme.cursor = color[1].parse().unwrap_or(COLOR_UNDEFINED),
"selected" => theme.selected = color[1].parse().unwrap_or(COLOR_UNDEFINED),
"header" => theme.header = color[1].parse().unwrap_or(COLOR_UNDEFINED),
_ => {}
}
}
theme
}
}
const DEFAULT16: ColorTheme = ColorTheme {
use_default: true,
fg: 15,
bg: 0,
matched: COLOR_GREEN,
matched_bg: COLOR_BLACK,
current: COLOR_YELLOW,
current_bg: COLOR_BLACK,
current_match: COLOR_GREEN,
current_match_bg: COLOR_BLACK,
spinner: COLOR_GREEN,
info: COLOR_WHITE,
prompt: COLOR_BLUE,
cursor: COLOR_RED,
selected: COLOR_MAGENTA,
header: COLOR_CYAN,
};
const DARK256: ColorTheme = ColorTheme {
use_default: true,
fg: 15,
bg: 0,
matched: 108,
matched_bg: 0,
current: 254,
current_bg: 236,
current_match: 151,
current_match_bg: 236,
spinner: 148,
info: 144,
prompt: 110,
cursor: 161,
selected: 168,
header: 109,
};
const MONOKAI256: ColorTheme = ColorTheme {
use_default: true,
fg: 252,
bg: 234,
matched: 234,
matched_bg: 186,
current: 254,
current_bg: 236,
current_match: 234,
current_match_bg: 186,
spinner: 148,
info: 144,
prompt: 110,
cursor: 161,
selected: 168,
header: 109,
};
const LIGHT256: ColorTheme = ColorTheme {
use_default: true,
fg: 15,
bg: 0,
matched: 0,
matched_bg: 220,
current: 237,
current_bg: 251,
current_match: 66,
current_match_bg: 251,
spinner: 65,
info: 101,
prompt: 25,
cursor: 161,
selected: 168,
header: 31,
};