diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-05-21 15:12:58 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-05-21 15:21:37 +0800 |
| commit | 74b5a80475e2230c0a494beac5ec86a985c2974f (patch) | |
| tree | 18892e7b6c0a0c6b4a3ac65fb497d7a55d5ae16f /mingling/src/example_docs.rs | |
| parent | 4704f6d54108bcc8f9b2fb7f4b3c4e224b4a7809 (diff) | |
Refactor REPL hooks into modular setups and add new hook types
Diffstat (limited to 'mingling/src/example_docs.rs')
| -rw-r--r-- | mingling/src/example_docs.rs | 68 |
1 files changed, 41 insertions, 27 deletions
diff --git a/mingling/src/example_docs.rs b/mingling/src/example_docs.rs index ab725de..8c93f4b 100644 --- a/mingling/src/example_docs.rs +++ b/mingling/src/example_docs.rs @@ -584,7 +584,12 @@ pub mod example_picker {} /// /// main.rs /// ```ignore -/// use mingling::{REPL, hook::ProgramHook, prelude::*, this}; +/// use mingling::{ +/// hook::ProgramHook, +/// prelude::*, +/// setup::{BasicREPLOutputSetup, BasicREPLPromptSetup, BasicREPLReadlineSetup}, +/// this, REPL, +/// }; /// use std::{env::current_dir, path::PathBuf}; /// /// // Resource to store the current directory @@ -611,34 +616,29 @@ pub mod example_picker {} /// program.with_dispatcher(ChangeDirectoryCommand); /// program.with_dispatcher(ListCommand); /// 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(); @@ -651,6 +651,7 @@ pub mod example_picker {} /// 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); @@ -719,12 +720,25 @@ pub mod example_picker {} /// 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!(); /// ``` pub mod example_repl {} |
