Files
FoundryOS/kernel/src/std/io/stdin.rs
T

76 lines
2.0 KiB
Rust

use crate::arch::x86_64::drivers::{
ascii::WRITER,
keyboard::{KeyStroke, get_keystroke_async, get_keystroke_optional},
};
use alloc::string::String;
/// 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.
///
/// If the user presses the abort key (usually Ctrl+C), the returned string
/// will be empty.
pub async fn read_line() -> String {
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 available.
///
/// # Note
///
/// This function is not yet implemented.
pub async fn async_keystroke() -> KeyStroke {
get_keystroke_async().await
}
/// Attempt to read a character from standard input without blocking the
/// current task.
///
/// If no character is available, returns `None`.
///
/// # Note
///
/// This function is not yet implemented.
pub fn keystroke() -> Option<KeyStroke> {
get_keystroke_optional()
}