Skip to content

Commit

Permalink
fb_console: only allow backspace to delete user input
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacWoods committed Sep 2, 2024
1 parent d78bbb1 commit 361b8a3
Showing 1 changed file with 34 additions and 23 deletions.
57 changes: 34 additions & 23 deletions user/fb_console/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,39 +93,50 @@ fn spawn_framebuffer(
if let Some(event) = console.input_events.recv().await {
match event {
InputEvent::KeyPressed(key) => {
write!(console.console.lock(), "{}", key).unwrap();
needs_redraw = true;
// TODO: `noline` is a no-std REPL impl crate thingy that could be useful
// for improving this experience
match key {
'\n' => {
let mut stmts = Parser::new(&current_line).parse().unwrap();
current_line.clear();

for mut statement in &mut stmts {
resolver.resolve_bindings(&mut statement);
}

if key == '\n' {
let mut stmts = Parser::new(&current_line).parse().unwrap();
current_line.clear();
let mut result = None;
for statement in stmts {
if let Some(value) = interpreter.eval_stmt(statement) {
result = Some(value);
}
}

for mut statement in &mut stmts {
resolver.resolve_bindings(&mut statement);
}
write!(console.console.lock(), "{}", key);
while let Ok(output) = output_receiver.try_recv() {
writeln!(console.console.lock(), "Output: {}", output).unwrap();
}

let mut result = None;
for statement in stmts {
if let Some(value) = interpreter.eval_stmt(statement) {
result = Some(value);
if let Some(result) = result {
writeln!(console.console.lock(), "Result: {}", result).unwrap();
}
}

while let Ok(output) = output_receiver.try_recv() {
writeln!(console.console.lock(), "Output: {}", output).unwrap();
write!(console.console.lock(), "\n> ").unwrap();
needs_redraw = true;
}

if let Some(result) = result {
writeln!(console.console.lock(), "Result: {}", result).unwrap();
// ASCII `DEL` is produced by backspace
'\x7f' => {
// Only allow the user to delete characters they've typed.
if current_line.pop().is_some() {
write!(console.console.lock(), "{}", key).unwrap();
needs_redraw = true;
}
}

write!(console.console.lock(), "\n> ").unwrap();
} else {
// Handle backspace (ASCII `DEL`) to delete the last char
if key == '\x7f' {
current_line.pop();
} else {
other => {
write!(console.console.lock(), "{}", key).unwrap();
current_line.push(key);
needs_redraw = true;
}
}
}
Expand Down

0 comments on commit 361b8a3

Please sign in to comment.