pub struct TerminalApp {
pub stdout_handle: Stdout,
pub command_history: Vec<String>,
pub current_input: String,
pub history_index: Option<usize>,
pub last_ctrl_c: Option<Instant>,
pub cursor_position: usize,
pub should_exit: bool,
pub action_sender: Option<UnboundedSender<AppAction>>,
pub action_receiver: Option<UnboundedReceiver<AppAction>>,
pub events_tx: Option<Sender<DaemonConsoleEvent>>,
/* private fields */
}Expand description
Main terminal application structure managing state and command execution.
TerminalApp provides a complete terminal interface with:
- Command history navigation
- Cursor management
- Custom command registration (sync and async)
- Configurable unknown command handling
- Non-blocking async command execution
Fields§
§stdout_handle: Stdout§command_history: Vec<String>§current_input: String§history_index: Option<usize>§last_ctrl_c: Option<Instant>§cursor_position: usize§should_exit: bool§action_sender: Option<UnboundedSender<AppAction>>§action_receiver: Option<UnboundedReceiver<AppAction>>§events_tx: Option<Sender<DaemonConsoleEvent>>Implementations§
Source§impl TerminalApp
impl TerminalApp
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new terminal application instance with default settings.
Examples found in repository?
83async fn main() {
84 let mut app = TerminalApp::new();
85
86 app.set_unknown_command_handler(|_| {
87 get_warn!("The command system disabled for developing the event system.")
88 });
89
90 let action_tx = app
91 .get_action_sender()
92 .expect("Failed to get action sender");
93 let event_rx = app
94 .subscribe_events()
95 .expect("Failed to subscribe to events");
96
97 start_event_listener(event_rx, action_tx);
98
99 app.info("This message used for debugging 'TerminalLog' event.");
100
101 let _ = app
102 .run(&get_info!("App demo starting...", "Demo"), "")
103 .await;
104}Sourcepub fn get_action_sender(&self) -> Option<UnboundedSender<AppAction>>
pub fn get_action_sender(&self) -> Option<UnboundedSender<AppAction>>
Gets a clone of the action sender for communication with async commands
Examples found in repository?
83async fn main() {
84 let mut app = TerminalApp::new();
85
86 app.set_unknown_command_handler(|_| {
87 get_warn!("The command system disabled for developing the event system.")
88 });
89
90 let action_tx = app
91 .get_action_sender()
92 .expect("Failed to get action sender");
93 let event_rx = app
94 .subscribe_events()
95 .expect("Failed to subscribe to events");
96
97 start_event_listener(event_rx, action_tx);
98
99 app.info("This message used for debugging 'TerminalLog' event.");
100
101 let _ = app
102 .run(&get_info!("App demo starting...", "Demo"), "")
103 .await;
104}Sourcepub fn subscribe_events(&self) -> Option<Receiver<DaemonConsoleEvent>>
pub fn subscribe_events(&self) -> Option<Receiver<DaemonConsoleEvent>>
Subscribes to daemon console events
Examples found in repository?
83async fn main() {
84 let mut app = TerminalApp::new();
85
86 app.set_unknown_command_handler(|_| {
87 get_warn!("The command system disabled for developing the event system.")
88 });
89
90 let action_tx = app
91 .get_action_sender()
92 .expect("Failed to get action sender");
93 let event_rx = app
94 .subscribe_events()
95 .expect("Failed to subscribe to events");
96
97 start_event_listener(event_rx, action_tx);
98
99 app.info("This message used for debugging 'TerminalLog' event.");
100
101 let _ = app
102 .run(&get_info!("App demo starting...", "Demo"), "")
103 .await;
104}Sourcepub fn register_command<S: Into<String>>(
&mut self,
name: S,
handler: Box<dyn CommandHandler>,
)
pub fn register_command<S: Into<String>>( &mut self, name: S, handler: Box<dyn CommandHandler>, )
Registers a synchronous command with the terminal application
Sourcepub fn register_async_command<S: Into<String>>(
&mut self,
name: S,
handler: Box<dyn AsyncCommandHandler>,
)
pub fn register_async_command<S: Into<String>>( &mut self, name: S, handler: Box<dyn AsyncCommandHandler>, )
Registers an asynchronous command with the terminal application
Sourcepub fn set_unknown_command_handler<F>(&mut self, handler: F)
pub fn set_unknown_command_handler<F>(&mut self, handler: F)
Sets a custom handler for unknown commands (synchronous).
§Arguments
handler- Closure that takes the full command string and returns a response
Examples found in repository?
83async fn main() {
84 let mut app = TerminalApp::new();
85
86 app.set_unknown_command_handler(|_| {
87 get_warn!("The command system disabled for developing the event system.")
88 });
89
90 let action_tx = app
91 .get_action_sender()
92 .expect("Failed to get action sender");
93 let event_rx = app
94 .subscribe_events()
95 .expect("Failed to subscribe to events");
96
97 start_event_listener(event_rx, action_tx);
98
99 app.info("This message used for debugging 'TerminalLog' event.");
100
101 let _ = app
102 .run(&get_info!("App demo starting...", "Demo"), "")
103 .await;
104}Sourcepub fn set_async_unknown_command_handler<F>(&mut self, handler: F)
pub fn set_async_unknown_command_handler<F>(&mut self, handler: F)
Sets a custom handler for unknown commands (asynchronous).
§Arguments
handler- Closure that takes the full command string and returns a future
Sourcepub fn clear_unknown_command_handler(&mut self)
pub fn clear_unknown_command_handler(&mut self)
Removes the custom unknown command handler.
Sourcepub async fn shutdown_terminal(
&mut self,
exit_message: &str,
) -> Result<(), Box<dyn Error>>
pub async fn shutdown_terminal( &mut self, exit_message: &str, ) -> Result<(), Box<dyn Error>>
Sourcepub async fn run(
&mut self,
startup_message: &str,
exit_message: &str,
) -> Result<(), Box<dyn Error>>
pub async fn run( &mut self, startup_message: &str, exit_message: &str, ) -> Result<(), Box<dyn Error>>
Main application loop that handles terminal input and command execution.
Initializes terminal event handling, processes keyboard input, and manages command execution until exit is requested. Handles special key combinations like Ctrl+C for graceful shutdown.
§Arguments
startup_message- Optional message to display on startupexit_message- Optional message to display on exit
§Errors
Returns an error if terminal initialization or event handling fails.
Examples found in repository?
83async fn main() {
84 let mut app = TerminalApp::new();
85
86 app.set_unknown_command_handler(|_| {
87 get_warn!("The command system disabled for developing the event system.")
88 });
89
90 let action_tx = app
91 .get_action_sender()
92 .expect("Failed to get action sender");
93 let event_rx = app
94 .subscribe_events()
95 .expect("Failed to subscribe to events");
96
97 start_event_listener(event_rx, action_tx);
98
99 app.info("This message used for debugging 'TerminalLog' event.");
100
101 let _ = app
102 .run(&get_info!("App demo starting...", "Demo"), "")
103 .await;
104}Sourcepub fn clear_input_line(&mut self)
pub fn clear_input_line(&mut self)
Clear the current input line and re-renders it.
Sourcepub fn print_log_entry(&mut self, log_line: &str)
pub fn print_log_entry(&mut self, log_line: &str)
Sourcepub async fn handle_ctrl_d(&mut self) -> Result<bool, Box<dyn Error>>
pub async fn handle_ctrl_d(&mut self) -> Result<bool, Box<dyn Error>>
Handles Ctrl+D key press, signaling application exit.
§Returns
Ok(true) to signal the application should quit.
Sourcepub async fn handle_ctrl_c(&mut self) -> Result<(bool, String), Box<dyn Error>>
pub async fn handle_ctrl_c(&mut self) -> Result<(bool, String), Box<dyn Error>>
Handles Ctrl+C key press with double-press confirmation.
The first press clears input, the second press within 5 seconds exits.
§Returns
Tuple of (should_quit, message_to_display)
Sourcepub async fn handle_enter_key(
&mut self,
input_prefix: &str,
) -> Result<bool, Box<dyn Error>>
pub async fn handle_enter_key( &mut self, input_prefix: &str, ) -> Result<bool, Box<dyn Error>>
Handles the enter key press event, executing commands and managing input history
Sourcepub fn info(&mut self, message: &str)
pub fn info(&mut self, message: &str)
Log info-level messages.
This method ensures proper terminal line management by clearing the current input line, printing the log message, and then re-rendering the input line.
§Arguments
message- The message content to be logged.
§Examples
use daemon_console::TerminalApp;
fn needless_main() {
let mut app = TerminalApp::new();
app.info("Application started successfully!");
app.info("Running tasks...");
}Examples found in repository?
83async fn main() {
84 let mut app = TerminalApp::new();
85
86 app.set_unknown_command_handler(|_| {
87 get_warn!("The command system disabled for developing the event system.")
88 });
89
90 let action_tx = app
91 .get_action_sender()
92 .expect("Failed to get action sender");
93 let event_rx = app
94 .subscribe_events()
95 .expect("Failed to subscribe to events");
96
97 start_event_listener(event_rx, action_tx);
98
99 app.info("This message used for debugging 'TerminalLog' event.");
100
101 let _ = app
102 .run(&get_info!("App demo starting...", "Demo"), "")
103 .await;
104}Sourcepub fn debug(&mut self, message: &str)
pub fn debug(&mut self, message: &str)
Log debug-level messages.
§Examples
use daemon_console::TerminalApp;
fn needless_main() {
let mut app = TerminalApp::new();
app.debug("Debugging information...");
app.debug("Debugging more...");
}Sourcepub fn warn(&mut self, message: &str)
pub fn warn(&mut self, message: &str)
Log warn-level messages.
§Examples
use daemon_console::TerminalApp;
fn needless_main() {
let mut app = TerminalApp::new();
app.warn("You get a warning!");
app.warn("Continue running...");
}Sourcepub fn error(&mut self, message: &str)
pub fn error(&mut self, message: &str)
Log error-level messages.
§Examples
use daemon_console::TerminalApp;
fn needless_main() {
let mut app = TerminalApp::new();
app.error("An error occurred!");
app.error("Failed to run tasks.");
}Sourcepub fn critical(&mut self, message: &str)
pub fn critical(&mut self, message: &str)
Log critical-level messages.
§Examples
use daemon_console::TerminalApp;
fn needless_main() {
let mut app = TerminalApp::new();
app.critical("Application crashed!");
app.critical("Exception: unknown.");
}Sourcepub fn logger(
&mut self,
level: LogLevel,
message: &str,
module_name: Option<&str>,
dp_evt: Option<bool>,
)
pub fn logger( &mut self, level: LogLevel, message: &str, module_name: Option<&str>, dp_evt: Option<bool>, )
Unified logger method that allows specifying a custom module name for the log message.
§Arguments
level- The log level (Info, Warn, Error, Debug, Critical)message- The message content to be loggedmodule_name- The name of the module to associate with the log message (optional)dispatch_event- Whether to dispatch log events (optional, defaults to true)
§Examples
use daemon_console::{TerminalApp, logger::LogLevel};
fn example() {
let mut app = TerminalApp::new();
app.logger(LogLevel::Info, "Application started", Some("Main"), None);
app.logger(LogLevel::Error, "Database connection failed", None, Some(true));
}