-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy pathwindows.rs
672 lines (610 loc) · 22.4 KB
/
windows.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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
//! Windows specific definitions
#![allow(clippy::try_err)] // suggested fix does not work (cannot infer...)
use std::io::{self, ErrorKind, Write};
use std::mem;
use std::sync::atomic;
use log::{debug, warn};
use unicode_width::UnicodeWidthChar;
use winapi::shared::minwindef::{DWORD, WORD};
use winapi::um::winnt::{CHAR, HANDLE};
use winapi::um::{consoleapi, handleapi, processenv, winbase, wincon, winuser};
use super::{RawMode, RawReader, Renderer, Term};
use crate::config::{BellStyle, ColorMode, Config, OutputStreamType};
use crate::error;
use crate::highlight::Highlighter;
use crate::keys::{self, KeyPress};
use crate::layout::{Layout, Position};
use crate::line_buffer::LineBuffer;
use crate::Result;
const STDIN_FILENO: DWORD = winbase::STD_INPUT_HANDLE;
const STDOUT_FILENO: DWORD = winbase::STD_OUTPUT_HANDLE;
const STDERR_FILENO: DWORD = winbase::STD_ERROR_HANDLE;
fn get_std_handle(fd: DWORD) -> Result<HANDLE> {
let handle = unsafe { processenv::GetStdHandle(fd) };
if handle == handleapi::INVALID_HANDLE_VALUE {
Err(io::Error::last_os_error())?;
} else if handle.is_null() {
Err(io::Error::new(
io::ErrorKind::Other,
"no stdio handle available for this process",
))?;
}
Ok(handle)
}
#[macro_export]
macro_rules! check {
($funcall:expr) => {{
let rc = unsafe { $funcall };
if rc == 0 {
Err(io::Error::last_os_error())?;
}
rc
}};
}
fn get_win_size(handle: HANDLE) -> (usize, usize) {
let mut info = unsafe { mem::zeroed() };
match unsafe { wincon::GetConsoleScreenBufferInfo(handle, &mut info) } {
0 => (80, 24),
_ => (
info.dwSize.X as usize,
(1 + info.srWindow.Bottom - info.srWindow.Top) as usize,
), // (info.srWindow.Right - info.srWindow.Left + 1)
}
}
fn get_console_mode(handle: HANDLE) -> Result<DWORD> {
let mut original_mode = 0;
check!(consoleapi::GetConsoleMode(handle, &mut original_mode));
Ok(original_mode)
}
#[cfg(not(test))]
pub type Mode = ConsoleMode;
#[derive(Clone, Copy, Debug)]
pub struct ConsoleMode {
original_stdin_mode: DWORD,
stdin_handle: HANDLE,
original_stdstream_mode: Option<DWORD>,
stdstream_handle: HANDLE,
}
impl RawMode for ConsoleMode {
/// Disable RAW mode for the terminal.
fn disable_raw_mode(&self) -> Result<()> {
check!(consoleapi::SetConsoleMode(
self.stdin_handle,
self.original_stdin_mode,
));
if let Some(original_stdstream_mode) = self.original_stdstream_mode {
check!(consoleapi::SetConsoleMode(
self.stdstream_handle,
original_stdstream_mode,
));
}
Ok(())
}
}
/// Console input reader
pub struct ConsoleRawReader {
handle: HANDLE,
}
impl ConsoleRawReader {
pub fn create() -> Result<ConsoleRawReader> {
let handle = get_std_handle(STDIN_FILENO)?;
Ok(ConsoleRawReader { handle })
}
}
impl RawReader for ConsoleRawReader {
fn next_key(&mut self, _: bool) -> Result<KeyPress> {
use std::char::decode_utf16;
use winapi::um::wincon::{
LEFT_ALT_PRESSED, LEFT_CTRL_PRESSED, RIGHT_ALT_PRESSED, RIGHT_CTRL_PRESSED,
SHIFT_PRESSED,
};
let mut rec: wincon::INPUT_RECORD = unsafe { mem::zeroed() };
let mut count = 0;
let mut surrogate = 0;
loop {
// TODO GetNumberOfConsoleInputEvents
check!(consoleapi::ReadConsoleInputW(
self.handle,
&mut rec,
1 as DWORD,
&mut count,
));
if rec.EventType == wincon::WINDOW_BUFFER_SIZE_EVENT {
SIGWINCH.store(true, atomic::Ordering::SeqCst);
debug!(target: "rustyline", "SIGWINCH");
return Err(error::ReadlineError::WindowResize); // sigwinch +
// err => err
// ignored
} else if rec.EventType != wincon::KEY_EVENT {
continue;
}
let key_event = unsafe { rec.Event.KeyEvent() };
// writeln!(io::stderr(), "key_event: {:?}", key_event).unwrap();
if key_event.bKeyDown == 0 && key_event.wVirtualKeyCode != winuser::VK_MENU as WORD {
continue;
}
// key_event.wRepeatCount seems to be always set to 1 (maybe because we only
// read one character at a time)
let alt_gr = key_event.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_ALT_PRESSED)
== (LEFT_CTRL_PRESSED | RIGHT_ALT_PRESSED);
let alt = key_event.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED) != 0;
let ctrl = key_event.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED) != 0;
let meta = alt && !alt_gr;
let shift = key_event.dwControlKeyState & SHIFT_PRESSED != 0;
let utf16 = unsafe { *key_event.uChar.UnicodeChar() };
if utf16 == 0 {
match i32::from(key_event.wVirtualKeyCode) {
winuser::VK_LEFT => {
return Ok(if ctrl {
KeyPress::ControlLeft
} else if shift {
KeyPress::ShiftLeft
} else {
KeyPress::Left
});
}
winuser::VK_RIGHT => {
return Ok(if ctrl {
KeyPress::ControlRight
} else if shift {
KeyPress::ShiftRight
} else {
KeyPress::Right
});
}
winuser::VK_UP => {
return Ok(if ctrl {
KeyPress::ControlUp
} else if shift {
KeyPress::ShiftUp
} else {
KeyPress::Up
});
}
winuser::VK_DOWN => {
return Ok(if ctrl {
KeyPress::ControlDown
} else if shift {
KeyPress::ShiftDown
} else {
KeyPress::Down
});
}
winuser::VK_DELETE => return Ok(KeyPress::Delete),
winuser::VK_HOME => return Ok(KeyPress::Home),
winuser::VK_END => return Ok(KeyPress::End),
winuser::VK_PRIOR => return Ok(KeyPress::PageUp),
winuser::VK_NEXT => return Ok(KeyPress::PageDown),
winuser::VK_INSERT => return Ok(KeyPress::Insert),
winuser::VK_F1 => return Ok(KeyPress::F(1)),
winuser::VK_F2 => return Ok(KeyPress::F(2)),
winuser::VK_F3 => return Ok(KeyPress::F(3)),
winuser::VK_F4 => return Ok(KeyPress::F(4)),
winuser::VK_F5 => return Ok(KeyPress::F(5)),
winuser::VK_F6 => return Ok(KeyPress::F(6)),
winuser::VK_F7 => return Ok(KeyPress::F(7)),
winuser::VK_F8 => return Ok(KeyPress::F(8)),
winuser::VK_F9 => return Ok(KeyPress::F(9)),
winuser::VK_F10 => return Ok(KeyPress::F(10)),
winuser::VK_F11 => return Ok(KeyPress::F(11)),
winuser::VK_F12 => return Ok(KeyPress::F(12)),
// winuser::VK_BACK is correctly handled because the key_event.UnicodeChar is
// also set.
_ => continue,
};
} else if utf16 == 27 {
return Ok(KeyPress::Esc);
} else {
if utf16 >= 0xD800 && utf16 < 0xDC00 {
surrogate = utf16;
continue;
}
let orc = if surrogate == 0 {
decode_utf16(Some(utf16)).next()
} else {
decode_utf16([surrogate, utf16].iter().cloned()).next()
};
let rc = if let Some(rc) = orc {
rc
} else {
return Err(error::ReadlineError::Eof);
};
let c = rc?;
if meta {
return Ok(KeyPress::Meta(c));
} else {
let mut key = keys::char_to_key_press(c);
if key == KeyPress::Tab && shift {
key = KeyPress::BackTab;
} else if key == KeyPress::Char(' ') && ctrl {
key = KeyPress::Ctrl(' ');
}
return Ok(key);
}
}
}
}
fn read_pasted_text(&mut self) -> Result<String> {
unimplemented!()
}
}
pub struct ConsoleRenderer {
out: OutputStreamType,
handle: HANDLE,
cols: usize, // Number of columns in terminal
buffer: String,
colors_enabled: bool,
bell_style: BellStyle,
}
impl ConsoleRenderer {
fn new(
handle: HANDLE,
out: OutputStreamType,
colors_enabled: bool,
bell_style: BellStyle,
) -> ConsoleRenderer {
// Multi line editing is enabled by ENABLE_WRAP_AT_EOL_OUTPUT mode
let (cols, _) = get_win_size(handle);
ConsoleRenderer {
out,
handle,
cols,
buffer: String::with_capacity(1024),
colors_enabled,
bell_style,
}
}
fn get_console_screen_buffer_info(&self) -> Result<wincon::CONSOLE_SCREEN_BUFFER_INFO> {
let mut info = unsafe { mem::zeroed() };
check!(wincon::GetConsoleScreenBufferInfo(self.handle, &mut info));
Ok(info)
}
fn set_console_cursor_position(&mut self, pos: wincon::COORD) -> Result<()> {
check!(wincon::SetConsoleCursorPosition(self.handle, pos));
Ok(())
}
fn clear(&mut self, length: DWORD, pos: wincon::COORD) -> Result<()> {
let mut _count = 0;
check!(wincon::FillConsoleOutputCharacterA(
self.handle,
' ' as CHAR,
length,
pos,
&mut _count,
));
Ok(())
}
}
impl Renderer for ConsoleRenderer {
type Reader = ConsoleRawReader;
fn move_cursor(&mut self, old: Position, new: Position) -> Result<()> {
let mut cursor = self.get_console_screen_buffer_info()?.dwCursorPosition;
if new.row > old.row {
cursor.Y += (new.row - old.row) as i16;
} else {
cursor.Y -= (old.row - new.row) as i16;
}
if new.col > old.col {
cursor.X += (new.col - old.col) as i16;
} else {
cursor.X -= (old.col - new.col) as i16;
}
self.set_console_cursor_position(cursor)
}
fn refresh_line(
&mut self,
prompt: &str,
line: &LineBuffer,
hint: Option<&str>,
old_layout: &Layout,
new_layout: &Layout,
highlighter: Option<&dyn Highlighter>,
) -> Result<()> {
let default_prompt = new_layout.default_prompt;
let cursor = new_layout.cursor;
let end_pos = new_layout.end;
let current_row = old_layout.cursor.row;
let old_rows = old_layout.end.row;
self.buffer.clear();
if let Some(highlighter) = highlighter {
// TODO handle ansi escape code (SetConsoleTextAttribute)
// append the prompt
self.buffer
.push_str(&highlighter.highlight_prompt(prompt, default_prompt));
// append the input line
self.buffer
.push_str(&highlighter.highlight(line, line.pos()));
} else {
// append the prompt
self.buffer.push_str(prompt);
// append the input line
self.buffer.push_str(line);
}
// append hint
if let Some(hint) = hint {
if let Some(highlighter) = highlighter {
self.buffer.push_str(&highlighter.highlight_hint(hint));
} else {
self.buffer.push_str(hint);
}
}
// position at the start of the prompt, clear to end of previous input
let info = self.get_console_screen_buffer_info()?;
let mut coord = info.dwCursorPosition;
coord.X = 0;
coord.Y -= current_row as i16;
self.set_console_cursor_position(coord)?;
self.clear((info.dwSize.X * (old_rows as i16 + 1)) as DWORD, coord)?;
// display prompt, input line and hint
self.write_and_flush(self.buffer.as_bytes())?;
// position the cursor
let mut coord = self.get_console_screen_buffer_info()?.dwCursorPosition;
coord.X = cursor.col as i16;
coord.Y -= (end_pos.row - cursor.row) as i16;
self.set_console_cursor_position(coord)?;
Ok(())
}
fn write_and_flush(&self, buf: &[u8]) -> Result<()> {
match self.out {
OutputStreamType::Stdout => {
io::stdout().write_all(buf)?;
io::stdout().flush()?;
}
OutputStreamType::Stderr => {
io::stderr().write_all(buf)?;
io::stderr().flush()?;
}
}
Ok(())
}
/// Characters with 2 column width are correctly handled (not split).
fn calculate_position(&self, s: &str, orig: Position) -> Position {
let mut pos = orig;
for c in s.chars() {
let cw = if c == '\n' {
pos.col = 0;
pos.row += 1;
None
} else {
c.width()
};
if let Some(cw) = cw {
pos.col += cw;
if pos.col > self.cols {
pos.row += 1;
pos.col = cw;
}
}
}
if pos.col == self.cols {
pos.col = 0;
pos.row += 1;
}
pos
}
fn beep(&mut self) -> Result<()> {
match self.bell_style {
BellStyle::Audible => {
io::stderr().write_all(b"\x07")?;
io::stderr().flush()?;
Ok(())
}
_ => Ok(()),
}
}
/// Clear the screen. Used to handle ctrl+l
fn clear_screen(&mut self) -> Result<()> {
let info = self.get_console_screen_buffer_info()?;
let coord = wincon::COORD { X: 0, Y: 0 };
check!(wincon::SetConsoleCursorPosition(self.handle, coord));
let n = info.dwSize.X as DWORD * info.dwSize.Y as DWORD;
self.clear(n, coord)
}
fn sigwinch(&self) -> bool {
SIGWINCH.compare_and_swap(true, false, atomic::Ordering::SeqCst)
}
/// Try to get the number of columns in the current terminal,
/// or assume 80 if it fails.
fn update_size(&mut self) {
let (cols, _) = get_win_size(self.handle);
self.cols = cols;
}
fn get_columns(&self) -> usize {
self.cols
}
/// Try to get the number of rows in the current terminal,
/// or assume 24 if it fails.
fn get_rows(&self) -> usize {
let (_, rows) = get_win_size(self.handle);
rows
}
fn colors_enabled(&self) -> bool {
self.colors_enabled
}
fn move_cursor_at_leftmost(&mut self, _: &mut ConsoleRawReader) -> Result<()> {
self.write_and_flush(b"")?; // we must do this otherwise the cursor position is not reported correctly
let mut info = self.get_console_screen_buffer_info()?;
if info.dwCursorPosition.X == 0 {
return Ok(());
}
debug!(target: "rustyline", "initial cursor location: {:?}, {:?}", info.dwCursorPosition.X, info.dwCursorPosition.Y);
info.dwCursorPosition.X = 0;
info.dwCursorPosition.Y += 1;
let res = self.set_console_cursor_position(info.dwCursorPosition);
if let Err(error::ReadlineError::Io(ref e)) = res {
if e.kind() == ErrorKind::Other && e.raw_os_error() == Some(87) {
warn!(target: "rustyline", "invalid cursor position: ({:?}, {:?}) in ({:?}, {:?})", info.dwCursorPosition.X, info.dwCursorPosition.Y, info.dwSize.X, info.dwSize.Y);
println!("");
return Ok(());
}
}
res
}
}
static SIGWINCH: atomic::AtomicBool = atomic::AtomicBool::new(false);
#[cfg(not(test))]
pub type Terminal = Console;
#[derive(Clone, Debug)]
pub struct Console {
stdin_isatty: bool,
stdin_handle: HANDLE,
stdstream_isatty: bool,
stdstream_handle: HANDLE,
pub(crate) color_mode: ColorMode,
ansi_colors_supported: bool,
stream_type: OutputStreamType,
bell_style: BellStyle,
}
impl Console {
fn colors_enabled(&self) -> bool {
// TODO ANSI Colors & Windows <10
match self.color_mode {
ColorMode::Enabled => self.stdstream_isatty && self.ansi_colors_supported,
ColorMode::Forced => true,
ColorMode::Disabled => false,
}
}
}
impl Term for Console {
type Mode = ConsoleMode;
type Reader = ConsoleRawReader;
type Writer = ConsoleRenderer;
fn new(
color_mode: ColorMode,
stream_type: OutputStreamType,
_tab_stop: usize,
bell_style: BellStyle,
) -> Console {
use std::ptr;
let stdin_handle = get_std_handle(STDIN_FILENO);
let stdin_isatty = match stdin_handle {
Ok(handle) => {
// If this function doesn't fail then fd is a TTY
get_console_mode(handle).is_ok()
}
Err(_) => false,
};
let stdstream_handle = get_std_handle(if stream_type == OutputStreamType::Stdout {
STDOUT_FILENO
} else {
STDERR_FILENO
});
let stdstream_isatty = match stdstream_handle {
Ok(handle) => {
// If this function doesn't fail then fd is a TTY
get_console_mode(handle).is_ok()
}
Err(_) => false,
};
Console {
stdin_isatty,
stdin_handle: stdin_handle.unwrap_or(ptr::null_mut()),
stdstream_isatty,
stdstream_handle: stdstream_handle.unwrap_or(ptr::null_mut()),
color_mode,
ansi_colors_supported: false,
stream_type,
bell_style,
}
}
/// Checking for an unsupported TERM in windows is a no-op
fn is_unsupported(&self) -> bool {
false
}
fn is_stdin_tty(&self) -> bool {
self.stdin_isatty
}
fn is_output_tty(&self) -> bool {
self.stdstream_isatty
}
// pub fn install_sigwinch_handler(&mut self) {
// See ReadConsoleInputW && WINDOW_BUFFER_SIZE_EVENT
// }
/// Enable RAW mode for the terminal.
fn enable_raw_mode(&mut self) -> Result<Self::Mode> {
if !self.stdin_isatty {
Err(io::Error::new(
io::ErrorKind::Other,
"no stdio handle available for this process",
))?;
}
let original_stdin_mode = get_console_mode(self.stdin_handle)?;
// Disable these modes
let mut raw = original_stdin_mode
& !(wincon::ENABLE_LINE_INPUT
| wincon::ENABLE_ECHO_INPUT
| wincon::ENABLE_PROCESSED_INPUT);
// Enable these modes
raw |= wincon::ENABLE_EXTENDED_FLAGS;
raw |= wincon::ENABLE_INSERT_MODE;
raw |= wincon::ENABLE_QUICK_EDIT_MODE;
raw |= wincon::ENABLE_WINDOW_INPUT;
check!(consoleapi::SetConsoleMode(self.stdin_handle, raw));
let original_stdstream_mode = if self.stdstream_isatty {
let original_stdstream_mode = get_console_mode(self.stdstream_handle)?;
let mut mode = original_stdstream_mode;
if mode & wincon::ENABLE_WRAP_AT_EOL_OUTPUT == 0 {
mode |= wincon::ENABLE_WRAP_AT_EOL_OUTPUT;
debug!(target: "rustyline", "activate ENABLE_WRAP_AT_EOL_OUTPUT");
unsafe {
assert!(consoleapi::SetConsoleMode(self.stdstream_handle, mode) != 0);
}
}
// To enable ANSI colors (Windows 10 only):
// https://docs.microsoft.com/en-us/windows/console/setconsolemode
self.ansi_colors_supported = mode & wincon::ENABLE_VIRTUAL_TERMINAL_PROCESSING != 0;
if self.ansi_colors_supported {
if self.color_mode == ColorMode::Disabled {
mode &= !wincon::ENABLE_VIRTUAL_TERMINAL_PROCESSING;
debug!(target: "rustyline", "deactivate ENABLE_VIRTUAL_TERMINAL_PROCESSING");
unsafe {
assert!(consoleapi::SetConsoleMode(self.stdstream_handle, mode) != 0);
}
} else {
debug!(target: "rustyline", "ANSI colors already enabled");
}
} else if self.color_mode != ColorMode::Disabled {
mode |= wincon::ENABLE_VIRTUAL_TERMINAL_PROCESSING;
self.ansi_colors_supported =
unsafe { consoleapi::SetConsoleMode(self.stdstream_handle, mode) != 0 };
debug!(target: "rustyline", "ansi_colors_supported: {}", self.ansi_colors_supported);
}
Some(original_stdstream_mode)
} else {
None
};
Ok(ConsoleMode {
original_stdin_mode,
stdin_handle: self.stdin_handle,
original_stdstream_mode,
stdstream_handle: self.stdstream_handle,
})
}
fn create_reader(&self, _: &Config) -> Result<ConsoleRawReader> {
ConsoleRawReader::create()
}
fn create_writer(&self) -> ConsoleRenderer {
ConsoleRenderer::new(
self.stdstream_handle,
self.stream_type,
self.colors_enabled(),
self.bell_style,
)
}
}
unsafe impl Send for Console {}
unsafe impl Sync for Console {}
#[cfg(test)]
mod test {
use super::Console;
#[test]
fn test_send() {
fn assert_send<T: Send>() {}
assert_send::<Console>();
}
#[test]
fn test_sync() {
fn assert_sync<T: Sync>() {}
assert_sync::<Console>();
}
}