aboutsummaryrefslogtreecommitdiff
path: root/examples/example-repl/src/main.rs
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-05-21 15:12:58 +0800
committer魏曹先生 <1992414357@qq.com>2026-05-21 15:21:37 +0800
commit74b5a80475e2230c0a494beac5ec86a985c2974f (patch)
tree18892e7b6c0a0c6b4a3ac65fb497d7a55d5ae16f /examples/example-repl/src/main.rs
parent4704f6d54108bcc8f9b2fb7f4b3c4e224b4a7809 (diff)
Refactor REPL hooks into modular setups and add new hook types
Diffstat (limited to 'examples/example-repl/src/main.rs')
-rw-r--r--examples/example-repl/src/main.rs54
1 files changed, 27 insertions, 27 deletions
diff --git a/examples/example-repl/src/main.rs b/examples/example-repl/src/main.rs
index dab5b15..2d8d9b0 100644
--- a/examples/example-repl/src/main.rs
+++ b/examples/example-repl/src/main.rs
@@ -1,4 +1,10 @@
-use mingling::{REPL, hook::ProgramHook, prelude::*, this};
+use mingling::{
+ REPL,
+ hook::ProgramHook,
+ prelude::*,
+ setup::{BasicREPLOutputSetup, BasicREPLPromptSetup, BasicREPLReadlineSetup},
+ this,
+};
use std::{env::current_dir, path::PathBuf};
// Resource to store the current directory
@@ -27,33 +33,27 @@ fn main() {
program.with_dispatcher(ExitCommand);
program.with_dispatcher(ClearCommand);
+ // Add setups
+ program.with_setup(BasicREPLReadlineSetup);
+ program.with_setup(BasicREPLOutputSetup);
+ program.with_setup(BasicREPLPromptSetup::func(|| {
+ let res = this::<ThisProgram>().res::<CurrentDir>().unwrap();
+ let dir_str: String = res.dir.to_string_lossy().into();
+ let prompt = format!(
+ "{}> ",
+ dir_str
+ .replace(&['/', '\\'][..], ">")
+ .trim_start_matches('>')
+ .trim_end_matches('>')
+ );
+ prompt
+ }));
+
// Add hooks to handle REPL-related events
- program.with_hook(
- ProgramHook::empty()
- .on_repl_begin(|| {
- // Print welcome message
- println!("Welcome!")
- })
- .on_repl_pre_readline(|| {
- // Print prompt
- let res = this::<ThisProgram>().res::<CurrentDir>().unwrap();
- let dir_str: String = res.dir.to_string_lossy().into();
- let prompt = format!(
- "{}> ",
- dir_str
- .replace(&['/', '\\'][..], ">")
- .trim_start_matches('>')
- .trim_end_matches('>')
- );
- print!("{}", prompt)
- })
- .on_repl_receive_result(|r| {
- // Print output
- if !r.is_empty() {
- println!("{}", r.trim())
- }
- }),
- );
+ program.with_hook(ProgramHook::empty().on_repl_begin(|| {
+ // Print welcome message
+ println!("Welcome!")
+ }));
// Start the REPL loop
program.exec_repl();