Skip to content

Commit

Permalink
Handle backtab key for backward autocompletion (#321)
Browse files Browse the repository at this point in the history
* Handle backtab key

* Handle backtab in editor
  • Loading branch information
vinc authored Apr 9, 2022
1 parent 042367e commit dd2b20f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
33 changes: 33 additions & 0 deletions src/api/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,38 @@ impl Prompt {
self.completion.pos = Some(pos);
}

fn handle_backtab_key(&mut self) {
self.update_history();
let (bs, pos) = match self.completion.pos {
Some(pos) => {
let n = self.completion.entries.len();
if n == 1 {
self.update_completion();
return;
}
let bs = self.completion.entries[pos].chars().count();
if pos == 0 {
(bs, n - 1)
} else {
(bs, pos - 1)
}
},
None => {
let line: String = self.line.iter().collect();
self.completion.entries = (self.completion.completer)(&line);
if !self.completion.entries.is_empty() {
(0, 0)
} else {
return
}
},
};
let erase = "\x08".repeat(bs);
let complete = &self.completion.entries[pos];
print!("{}{}", erase, complete);
self.completion.pos = Some(pos);
}

fn handle_up_key(&mut self) {
self.update_completion();
let n = self.history.entries.len();
Expand Down Expand Up @@ -228,6 +260,7 @@ impl Perform for Prompt {
'B' => self.handle_down_key(),
'C' => self.handle_forward_key(),
'D' => self.handle_backward_key(),
'Z' => self.handle_backtab_key(),
'~' => {
for param in params.iter() {
if param[0] == 3 { // Delete
Expand Down
13 changes: 11 additions & 2 deletions src/sys/keyboard.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use crate::sys;

use core::sync::atomic::{AtomicBool, Ordering};
use lazy_static::lazy_static;
use pc_keyboard::{layouts, DecodedKey, Error, HandleControl, KeyCode, KeyEvent, Keyboard, ScancodeSet1};
use pc_keyboard::{layouts, DecodedKey, Error, HandleControl, KeyState, KeyCode, KeyEvent, Keyboard, ScancodeSet1};
use spin::Mutex;
use x86_64::instructions::port::Port;

lazy_static! {
pub static ref KEYBOARD: Mutex<Option<KeyboardLayout>> = Mutex::new(None);
}
pub static SHIFT: AtomicBool = AtomicBool::new(false);

pub enum KeyboardLayout {
Azerty(Keyboard<layouts::Azerty, ScancodeSet1>),
Expand Down Expand Up @@ -75,13 +77,20 @@ fn interrupt_handler() {
if let Some(ref mut keyboard) = *KEYBOARD.lock() {
let scancode = read_scancode();
if let Ok(Some(key_event)) = keyboard.add_byte(scancode) {
match key_event.code {
KeyCode::ShiftLeft | KeyCode::ShiftRight => SHIFT.store(key_event.state == KeyState::Down, Ordering::Relaxed),
_ => {}
}
let is_shifted = SHIFT.load(Ordering::Relaxed);

if let Some(key) = keyboard.process_keyevent(key_event) {
match key {
DecodedKey::Unicode(c) => send_key(c),
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_shifted => send_csi('Z'), // Convert Shift+Tab into Backtab
DecodedKey::Unicode(c) => send_key(c),
_ => {},
};
}
Expand Down
3 changes: 3 additions & 0 deletions src/usr/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ impl Editor {
self.x -= 1;
}
},
'Z' if csi => { // Backtab (Shift + Tab)
// Do nothing
},
'\x14' => { // Ctrl T -> Go to top of file
self.x = 0;
self.y = 0;
Expand Down

0 comments on commit dd2b20f

Please sign in to comment.