| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
use std::{ |
| 5 |
env, |
| 6 |
io::{self, Write as _}, |
| 7 |
time::Duration, |
| 8 |
}; |
| 9 |
|
| 10 |
#[cfg(windows)] |
| 11 |
use termina::windows; |
| 12 |
use termina::{ |
| 13 |
escape::csi::{self, KittyKeyboardFlags}, |
| 14 |
event::{KeyCode, KeyEvent}, |
| 15 |
Event, PlatformTerminal, Terminal, WindowSize, |
| 16 |
}; |
| 17 |
|
| 18 |
const HELP: &str = r#"Blocking read() |
| 19 |
- Keyboard, mouse, focus and terminal resize events enabled |
| 20 |
- Hit "c" to print current cursor position |
| 21 |
- Use Esc to quit |
| 22 |
"#; |
| 23 |
|
| 24 |
macro_rules! decset { |
| 25 |
($mode:ident) => { |
| 26 |
csi::Csi::Mode(csi::Mode::SetDecPrivateMode(csi::DecPrivateMode::Code( |
| 27 |
csi::DecPrivateModeCode::$mode, |
| 28 |
))) |
| 29 |
}; |
| 30 |
} |
| 31 |
macro_rules! decreset { |
| 32 |
($mode:ident) => { |
| 33 |
csi::Csi::Mode(csi::Mode::ResetDecPrivateMode(csi::DecPrivateMode::Code( |
| 34 |
csi::DecPrivateModeCode::$mode, |
| 35 |
))) |
| 36 |
}; |
| 37 |
} |
| 38 |
|
| 39 |
fn main() -> io::Result<()> { |
| 40 |
println!("{HELP}"); |
| 41 |
|
| 42 |
let args: Vec<_> = env::args().collect(); |
| 43 |
let windows_legacy = cfg!(windows) && args.iter().any(|a| a == "--windows-legacy"); |
| 44 |
|
| 45 |
#[cfg(windows)] |
| 46 |
let mut terminal = PlatformTerminal::with_mode(if windows_legacy { |
| 47 |
windows::InputReaderMode::Legacy |
| 48 |
} else { |
| 49 |
windows::InputReaderMode::Vte |
| 50 |
})?; |
| 51 |
#[cfg(not(windows))] |
| 52 |
let mut terminal = PlatformTerminal::new()?; |
| 53 |
|
| 54 |
terminal.enter_raw_mode()?; |
| 55 |
|
| 56 |
let keyboard_flags = if windows_legacy { |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
"".to_string() |
| 61 |
} else { |
| 62 |
csi::Csi::Keyboard(csi::Keyboard::PushFlags( |
| 63 |
KittyKeyboardFlags::DISAMBIGUATE_ESCAPE_CODES |
| 64 |
| KittyKeyboardFlags::REPORT_ALTERNATE_KEYS, |
| 65 |
)) |
| 66 |
.to_string() |
| 67 |
}; |
| 68 |
write!( |
| 69 |
terminal, |
| 70 |
"{}{}{}{}{}{}{}{}", |
| 71 |
keyboard_flags, |
| 72 |
decset!(FocusTracking), |
| 73 |
decset!(BracketedPaste), |
| 74 |
decset!(MouseTracking), |
| 75 |
decset!(ButtonEventMouse), |
| 76 |
decset!(AnyEventMouse), |
| 77 |
decset!(RXVTMouse), |
| 78 |
decset!(SGRMouse), |
| 79 |
)?; |
| 80 |
terminal.flush()?; |
| 81 |
|
| 82 |
let mut size = terminal.get_dimensions()?; |
| 83 |
loop { |
| 84 |
let event = terminal.read(|event| !event.is_escape())?; |
| 85 |
|
| 86 |
println!("Event: {event:?}\r"); |
| 87 |
|
| 88 |
match event { |
| 89 |
Event::Key(KeyEvent { |
| 90 |
code: KeyCode::Escape, |
| 91 |
.. |
| 92 |
}) => break, |
| 93 |
Event::Key(KeyEvent { |
| 94 |
code: KeyCode::Char('c'), |
| 95 |
.. |
| 96 |
}) => { |
| 97 |
if windows_legacy { |
| 98 |
#[cfg(windows)] |
| 99 |
{ |
| 100 |
let (line, col) = termina::windows::cursor_position()?; |
| 101 |
println!( |
| 102 |
"Cursor position: {:?}\r", |
| 103 |
(line.get_zero_based(), col.get_zero_based()) |
| 104 |
); |
| 105 |
} |
| 106 |
} else { |
| 107 |
write!( |
| 108 |
terminal, |
| 109 |
"{}", |
| 110 |
csi::Csi::Cursor(csi::Cursor::RequestActivePositionReport), |
| 111 |
)?; |
| 112 |
terminal.flush()?; |
| 113 |
let filter = |event: &Event| { |
| 114 |
matches!( |
| 115 |
event, |
| 116 |
Event::Csi(csi::Csi::Cursor(csi::Cursor::ActivePositionReport { .. })) |
| 117 |
) |
| 118 |
}; |
| 119 |
if terminal.poll(filter, Some(Duration::from_millis(50)))? { |
| 120 |
let Event::Csi(csi::Csi::Cursor(csi::Cursor::ActivePositionReport { |
| 121 |
line, |
| 122 |
col, |
| 123 |
})) = terminal.read(filter)? |
| 124 |
else { |
| 125 |
unreachable!() |
| 126 |
}; |
| 127 |
println!( |
| 128 |
"Cursor position: {:?}\r", |
| 129 |
(line.get_zero_based(), col.get_zero_based()) |
| 130 |
); |
| 131 |
} else { |
| 132 |
eprintln!("Failed to read the cursor position within 50msec\r"); |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
Event::WindowResized(dimensions) => { |
| 137 |
let new_size = flush_resize_events(&terminal, dimensions); |
| 138 |
println!("Resize from {size:?} to {new_size:?}\r"); |
| 139 |
size = new_size; |
| 140 |
} |
| 141 |
_ => (), |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
let keyboard_flags = if windows_legacy { |
| 146 |
"".to_string() |
| 147 |
} else { |
| 148 |
csi::Csi::Keyboard(csi::Keyboard::PopFlags(1)).to_string() |
| 149 |
}; |
| 150 |
write!( |
| 151 |
terminal, |
| 152 |
"{}{}{}{}{}{}{}{}", |
| 153 |
keyboard_flags, |
| 154 |
decreset!(FocusTracking), |
| 155 |
decreset!(BracketedPaste), |
| 156 |
decreset!(MouseTracking), |
| 157 |
decreset!(ButtonEventMouse), |
| 158 |
decreset!(AnyEventMouse), |
| 159 |
decreset!(RXVTMouse), |
| 160 |
decreset!(SGRMouse), |
| 161 |
)?; |
| 162 |
|
| 163 |
Ok(()) |
| 164 |
} |
| 165 |
|
| 166 |
fn flush_resize_events(terminal: &PlatformTerminal, original_size: WindowSize) -> WindowSize { |
| 167 |
let mut size = original_size; |
| 168 |
let filter = |event: &Event| matches!(event, Event::WindowResized { .. }); |
| 169 |
while let Ok(true) = terminal.poll(filter, Some(Duration::from_millis(50))) { |
| 170 |
if let Ok(Event::WindowResized(dimensions)) = terminal.read(filter) { |
| 171 |
size = dimensions; |
| 172 |
} |
| 173 |
} |
| 174 |
size |
| 175 |
} |