aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-05-20 22:46:47 +0800
committer魏曹先生 <1992414357@qq.com>2026-05-20 22:46:47 +0800
commita9755711b490529c26bdb4b024c6caf825940dd3 (patch)
treea89232814e506ffd6948142854a40d7cd61f8b5d /examples
parent39d66182f0290bacc10886c2659874bd9edc2d4b (diff)
Add ClearCommand dispatcher and clear screen handler for exmaple-repl
Diffstat (limited to 'examples')
-rw-r--r--examples/example-repl/src/main.rs15
1 files changed, 15 insertions, 0 deletions
diff --git a/examples/example-repl/src/main.rs b/examples/example-repl/src/main.rs
index 2eb92e1..dab5b15 100644
--- a/examples/example-repl/src/main.rs
+++ b/examples/example-repl/src/main.rs
@@ -25,6 +25,7 @@ fn main() {
program.with_dispatcher(ChangeDirectoryCommand);
program.with_dispatcher(ListCommand);
program.with_dispatcher(ExitCommand);
+ program.with_dispatcher(ClearCommand);
// Add hooks to handle REPL-related events
program.with_hook(
@@ -65,6 +66,7 @@ pack!(ErrorDirectoryNotExist = PathBuf);
dispatcher!("cd", ChangeDirectoryCommand => ChangeDirectoryEntry);
dispatcher!("ls", ListCommand => ListEntry);
dispatcher!("exit", ExitCommand => ExitEntry);
+dispatcher!("clear", ClearCommand => ClearEntry);
// Define data needed for the cd command's execution phase
pack!(StateChangeDirectory = String);
@@ -133,10 +135,23 @@ fn handle_exit(
repl.exit = true;
}
+// Handle clear command event
+#[chain]
+fn handle_clear(_prev: ClearEntry) {
+ // Clear the terminal screen
+ print!("\x1B[2J\x1B[1;1H");
+}
+
// Handle path not found event
#[renderer]
fn render_error_directory_not_exist(err: ErrorDirectoryNotExist) {
r_println!("Directory not found: {}", err.inner.display())
}
+// Handle dispatcher not found event
+#[renderer]
+fn dispatcher_not_found(prev: DispatcherNotFound) {
+ r_println!("Command not found: \"{}\"", prev.join(", "))
+}
+
gen_program!();