shell/term: ^L to clear screen

This commit is contained in:
Mark Poliakov 2025-03-03 15:24:50 +02:00
parent 91d05d352f
commit 7485476caa
6 changed files with 31 additions and 1 deletions

2
userspace/Cargo.lock generated
View File

@ -2439,6 +2439,8 @@ version = "0.1.0"
dependencies = [
"clap",
"cross",
"log",
"logsink",
"nom",
"runtime",
"thiserror",

View File

@ -5,9 +5,11 @@ edition = "2021"
[dependencies]
cross.workspace = true
logsink.workspace = true
clap.workspace = true
thiserror.workspace = true
log.workspace = true
nom = "7.1.3"

View File

@ -139,6 +139,7 @@ fn run_wrapper(args: ShellArgs, env: &Env) -> Result<(), Error> {
fn main() -> ExitCode {
const PROFILE_PATH: &str = "/etc/profile";
logsink::setup_logging(false);
let args = ShellArgs::parse();
let mut env = Env::new();

View File

@ -6,6 +6,7 @@ use crate::Error;
enum Outcome {
Interrupt,
Clear,
Data(usize),
}
@ -40,13 +41,16 @@ fn readline_inner(
buffer: &mut String,
) -> Result<Outcome, Error> {
let mut ch_buffer = [0; 8];
let mut pos = 0;
let mut pos = buffer.chars().count();
let mut ch = [0];
let mut decoder = Utf8Decoder {
buffer: [0; 4],
len: 0,
};
stdout.write_all(buffer.as_bytes()).ok();
stdout.flush().ok();
loop {
let len = stdin.read(&mut ch)?;
if len == 0 {
@ -69,6 +73,8 @@ fn readline_inner(
}
// ^C
c if c as u32 == 0x03 => return Ok(Outcome::Interrupt),
// ^L
c if c as u32 == 0x0C => return Ok(Outcome::Clear),
// TODO completion
'\t' => (),
c if c as u32 == 0x7F => {
@ -116,8 +122,12 @@ pub fn readline<P: Fn(&mut Stdout)>(
match readline_inner(&mut stdin, stdout, buffer)? {
Outcome::Data(n) => break Ok(n),
Outcome::Clear => {
stdout.write_all(b"\x1B[H\x1B[J").ok();
}
Outcome::Interrupt => {
stdout.write_all(b"\r\n").ok();
buffer.clear();
}
}
}

View File

@ -240,6 +240,11 @@ impl Terminal<'_> {
pty_master.write_all(&[termios.chars.eof]).unwrap();
need_redraw = s.scroll_end();
}
(KeyModifiers::CTRL, Key::Char(b'l')) => {
s.clear();
pty_master.write_all(&[0x0C]).unwrap();
need_redraw = true;
}
(KeyModifiers::SHIFT, Key::PageUp) => {
need_redraw = s.scroll_up();
}

View File

@ -255,6 +255,10 @@ impl State {
}
}
pub fn clear(&mut self) {
self.buffer.clear(self.attributes.bg);
}
pub fn resize(&mut self, width: usize, height: usize) {
self.buffer
.resize(width, height, self.default_attributes.bg);
@ -412,6 +416,12 @@ impl State {
true
}
// Move cursor to home position (0; 0)
'H' => {
self.buffer.set_row_dirty(self.cursor.row);
self.cursor = Cursor { row: 0, col: 0 };
true
}
// Clear rows/columns/screen
'J' => match self.esc_args[0] {
// Erase lines down