implemented stdin methods for reading a string (async) and reading keystrokes (sync + async). added a very basic shell on top of it for debugging

This commit is contained in:
2025-02-25 02:16:01 +00:00
parent 00d3a1de72
commit 27ee8226d8
7 changed files with 133 additions and 75 deletions
+48 -5
View File
@@ -8,6 +8,11 @@ pub use crate::drivers::io::{
pub mod stdin {
use alloc::string::String;
use crate::drivers::io::{
ascii::WRITER,
keyboard::{KeyStroke, get_keystroke_async, get_keystroke_optional},
};
/// Reads a line of input from standard input asynchronously, returning a `String` containing
/// the input line. Does not include the newline character at the end of the line.
///
@@ -15,7 +20,45 @@ pub mod stdin {
///
/// This function is currently unimplemented.
pub async fn read_line() -> String {
todo!()
let mut writer = WRITER.lock();
let mut buff = String::new();
loop {
match get_keystroke_async().await {
KeyStroke::Char(c) => match c {
'\n' => {
writer.write_glyph(c as u8);
return buff;
}
'\r' => {
writer.write_glyph(c as u8);
return buff;
}
'\x08' => {
if !buff.is_empty() {
buff.pop();
writer.backspace();
}
}
c => {
writer.write_glyph(c as u8);
buff.push(c)
}
},
KeyStroke::Enter => {
writer.write_glyph(b'\n');
return buff;
}
KeyStroke::Backspace => {
if !buff.is_empty() {
buff.pop();
writer.backspace();
}
}
_ => continue,
}
}
}
/// Reads a character from standard input and blocks the current task until a character is
@@ -24,8 +67,8 @@ pub mod stdin {
/// # Note
///
/// This function is not yet implemented.
pub async fn read_char() -> char {
todo!()
pub async fn async_keystroke() -> KeyStroke {
get_keystroke_async().await
}
/// Attempt to read a character from standard input without blocking the current task.
@@ -35,7 +78,7 @@ pub mod stdin {
/// # Note
///
/// This function is not yet implemented.
pub fn try_read_char() -> Option<char> {
todo!()
pub fn keystroke() -> Option<KeyStroke> {
get_keystroke_optional()
}
}