refactor: 📦 Refactor command_prompt
This commit is contained in:
parent
b1d54d3b44
commit
33418e79ab
|
@ -34,12 +34,8 @@ pub mod interface {
|
|||
fn read_char(&self) -> char;
|
||||
}
|
||||
|
||||
pub trait ConsoleTools {
|
||||
fn command_prompt<'a>(&self, buf: &'a mut [u8]) -> &'a [u8];
|
||||
}
|
||||
|
||||
/// Trait alias for a full-fledged console.
|
||||
pub trait All: Write + ConsoleOps + ConsoleTools {}
|
||||
pub trait All: Write + ConsoleOps {}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
|
@ -66,3 +62,30 @@ pub fn register_console(new_console: &'static (dyn interface::All + Sync)) {
|
|||
pub fn console() -> &'static dyn interface::All {
|
||||
CONSOLE.lock(|con| *con)
|
||||
}
|
||||
|
||||
/// A command prompt.
|
||||
pub fn command_prompt(buf: &mut [u8]) -> &[u8] {
|
||||
use interface::ConsoleOps;
|
||||
|
||||
console().write_string("\n$> ");
|
||||
|
||||
let mut i = 0;
|
||||
let mut input;
|
||||
loop {
|
||||
input = console().read_char();
|
||||
|
||||
if input == '\n' {
|
||||
console().write_char('\n'); // do \r\n output
|
||||
return &buf[..i];
|
||||
} else {
|
||||
if i < buf.len() {
|
||||
buf[i] = input as u8;
|
||||
i += 1;
|
||||
} else {
|
||||
return &buf[..i];
|
||||
}
|
||||
|
||||
console().write_char(input);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -45,10 +45,4 @@ impl SerialOps for NullConsole {
|
|||
fn clear_rx(&self) {}
|
||||
}
|
||||
|
||||
impl interface::ConsoleTools for NullConsole {
|
||||
fn command_prompt<'a>(&self, buf: &'a mut [u8]) -> &'a [u8] {
|
||||
buf
|
||||
}
|
||||
}
|
||||
|
||||
impl interface::All for NullConsole {}
|
||||
|
|
|
@ -102,35 +102,6 @@ impl Console {
|
|||
}
|
||||
}
|
||||
|
||||
impl interface::ConsoleTools for Console {
|
||||
/// A command prompt.
|
||||
fn command_prompt<'a>(&self, buf: &'a mut [u8]) -> &'a [u8] {
|
||||
use interface::ConsoleOps;
|
||||
|
||||
self.write_string("\n$> ");
|
||||
|
||||
let mut i = 0;
|
||||
let mut input;
|
||||
loop {
|
||||
input = self.read_char();
|
||||
|
||||
if input == '\n' {
|
||||
self.write_char('\n'); // do \r\n output
|
||||
return &buf[..i];
|
||||
} else {
|
||||
if i < buf.len() {
|
||||
buf[i] = input as u8;
|
||||
i += 1;
|
||||
} else {
|
||||
return &buf[..i];
|
||||
}
|
||||
|
||||
self.write_char(input);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The global console. Output of the kernel print! and println! macros goes here.
|
||||
pub fn console() -> &'static dyn crate::console::interface::All {
|
||||
&CONSOLE
|
||||
|
|
|
@ -401,10 +401,4 @@ impl interface::ConsoleOps for MiniUart {
|
|||
}
|
||||
}
|
||||
|
||||
impl interface::ConsoleTools for MiniUart {
|
||||
fn command_prompt<'a>(&self, buf: &'a mut [u8]) -> &'a [u8] {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
impl interface::All for MiniUart {}
|
||||
|
|
Loading…
Reference in New Issue