Bind up arrow key to fetch previous command

This commit is contained in:
Yash Karandikar 2022-03-05 21:30:34 -06:00
parent 5c0df1b8fd
commit bd7413050e

View file

@ -1,6 +1,9 @@
use crate::gdt;
use crate::print;
use crate::println;
use alloc::string::String;
use alloc::string::ToString;
use alloc::vec::Vec;
use lazy_static::lazy_static;
use pic8259::ChainedPics;
use spin::Mutex;
@ -26,6 +29,7 @@ lazy_static! {
.set_handler_fn(general_protection_fault_handler);
idt
};
static ref PREV_COMMANDS: Mutex<Vec<String>> = Mutex::new(Vec::new());
}
pub fn init() {
@ -95,7 +99,6 @@ extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: InterruptStac
} else if character == '\u{9}' {
print!(" ");
} else if character == '\n' {
use alloc::string::String;
let writer = crate::vga_buffer::WRITER.lock();
// Gather all chars in the current row into one ArrayString
@ -110,6 +113,10 @@ extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: InterruptStac
crate::vga_buffer::WRITER.force_unlock();
}
crate::shell::evaluate(&builder);
PREV_COMMANDS
.lock()
.push(builder.strip_prefix(">>> ").unwrap().trim().to_string());
} else {
print!("{}", character)
}
@ -122,6 +129,41 @@ extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: InterruptStac
}
DecodedKey::RawKey(key) => {
match key {
KeyCode::ArrowUp => {
let mut builder = String::new();
let writer = crate::vga_buffer::WRITER.lock();
for character in
&writer.buffer.chars[crate::vga_buffer::BUFFER_HEIGHT - 1]
{
builder.push(character.read().ascii_character as char);
}
drop(writer);
let command = builder.strip_prefix(">>> ").unwrap();
let mut pc = PREV_COMMANDS.lock();
if command.trim() == "" {
let len = pc.len();
if len > 0 {
let prev_command = pc.remove(len - 1);
print!("{}", prev_command);
}
} else {
let pos = pc.iter().position(|s| s.starts_with(command.trim()));
if let Some(pos) = pos {
print!("{}", pc[pos].strip_prefix(command.trim()).unwrap());
pc.remove(pos);
}
}
let writer = crate::vga_buffer::WRITER.lock();
let col = writer.column_position;
let row = crate::vga_buffer::BUFFER_HEIGHT - 1;
crate::vga_buffer::move_cursor(col as u16, row as u16);
}
KeyCode::ArrowLeft => {
let mut writer = crate::vga_buffer::WRITER.lock();
let col = writer.column_position;