Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PageUp and PageDown keys support #515

Merged
merged 4 commits into from
Jul 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Changelog

## Unreleased
- Add PageUp and PageDown keys support ([#515](https://github.com/vinc/moros/pull/515))
- Bump pbkdf2 from 0.12.1 to 0.12.2 ([#513](https://github.com/vinc/moros/pull/513))
- Bump uart_16550 from 0.2.18 to 0.2.19 ([#514](https://github.com/vinc/moros/pull/514))
- Add namespaces to lisp ([#511](https://github.com/vinc/moros/pull/511))
- Update smoltcp from 0.9.1 to 0.10.0 ([#510](https://github.com/vinc/moros/pull/510))

Expand Down
18 changes: 11 additions & 7 deletions src/sys/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,12 @@ fn send_key(c: char) {
sys::console::key_handle(c);
}

fn send_csi(c: char) {
fn send_csi(code: &str) {
send_key('\x1B'); // ESC
send_key('[');
send_key(c);
for c in code.chars() {
send_key(c);
}
}

fn interrupt_handler() {
Expand All @@ -91,11 +93,13 @@ fn interrupt_handler() {
if let Some(key) = keyboard.process_keyevent(event) {
match key {
DecodedKey::Unicode('\u{7f}') if is_alt && is_ctrl => syscall::reboot(), // Ctrl-Alt-Del
DecodedKey::RawKey(KeyCode::ArrowUp) => send_csi('A'),
DecodedKey::RawKey(KeyCode::ArrowDown) => send_csi('B'),
DecodedKey::RawKey(KeyCode::ArrowRight) => send_csi('C'),
DecodedKey::RawKey(KeyCode::ArrowLeft) => send_csi('D'),
DecodedKey::Unicode('\t') if is_shift => send_csi('Z'), // Convert Shift-Tab into Backtab
DecodedKey::RawKey(KeyCode::PageUp) => send_csi("5~"),
DecodedKey::RawKey(KeyCode::PageDown) => send_csi("6~"),
DecodedKey::RawKey(KeyCode::ArrowUp) => send_csi("A"),
DecodedKey::RawKey(KeyCode::ArrowDown) => send_csi("B"),
DecodedKey::RawKey(KeyCode::ArrowRight) => send_csi("C"),
DecodedKey::RawKey(KeyCode::ArrowLeft) => send_csi("D"),
DecodedKey::Unicode('\t') if is_shift => send_csi("Z"), // Convert Shift-Tab into Backtab
DecodedKey::Unicode(c) => send_key(c),
_ => {},
};
Expand Down
28 changes: 24 additions & 4 deletions src/usr/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ impl Editor {

let mut escape = false;
let mut csi = false;
let mut csi_params = String::new();
loop {
let c = io::stdin().read_char().unwrap_or('\0');
print!("\x1b[?25l"); // Disable cursor
Expand Down Expand Up @@ -296,7 +297,21 @@ impl Editor {
self.offset.x = 0;
self.print_screen();
},
'A' if csi => { // Arrow up
'~' if csi && csi_params == "5" => { // Page Up
let scroll = self.rows() - 1; // Keep one previous line on screen
self.offset.y -= cmp::min(scroll, self.offset.y);
self.print_screen();
},
'~' if csi && csi_params == "6" => { // Page Down
let scroll = self.rows() - 1; // Keep one previous line on screen
let remaining = cmp::max(self.lines.len(), 1) - self.offset.y - 1;
self.offset.y += cmp::min(scroll, remaining);
if self.cursor.y + scroll > remaining {
self.cursor.y = 0;
}
self.print_screen();
},
'A' if csi => { // Arrow Up
if self.cursor.y > 0 {
self.cursor.y -= 1
} else if self.offset.y > 0 {
Expand All @@ -305,7 +320,7 @@ impl Editor {
}
self.cursor.x = self.next_pos(self.cursor.x, self.cursor.y);
},
'B' if csi => { // Arrow down
'B' if csi => { // Arrow Down
let is_eof = self.offset.y + self.cursor.y == self.lines.len() - 1;
let is_bottom = self.cursor.y == self.rows() - 1;
if self.cursor.y < cmp::min(self.rows(), self.lines.len() - 1) {
Expand All @@ -320,7 +335,7 @@ impl Editor {
self.cursor.x = self.next_pos(self.cursor.x, self.cursor.y);
}
},
'C' if csi => { // Arrow right
'C' if csi => { // Arrow Right
let line = &self.lines[self.offset.y + self.cursor.y];
if line.is_empty() || self.cursor.x + self.offset.x >= line.chars().count() {
print!("\x1b[?25h"); // Enable cursor
Expand All @@ -333,7 +348,7 @@ impl Editor {
self.cursor.x += 1;
}
},
'D' if csi => { // Arrow left
'D' if csi => { // Arrow Left
if self.cursor.x + self.offset.x == 0 {
print!("\x1b[?25h"); // Enable cursor
continue;
Expand Down Expand Up @@ -465,6 +480,10 @@ impl Editor {
print!("\x1b[2K\x1b[1G{}", line);
}
},
c if csi => {
csi_params.push(c);
continue;
},
c => {
if let Some(s) = self.render_char(c) {
let y = self.offset.y + self.cursor.y;
Expand All @@ -487,6 +506,7 @@ impl Editor {
}
escape = false;
csi = false;
csi_params = String::new();
self.print_editing_status();
self.print_highlighted();
print!("\x1b[{};{}H", self.cursor.y + 1, self.cursor.x + 1);
Expand Down