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>>,
/* 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>>Implementations§
Source§impl TerminalApp
impl TerminalApp
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 handler with the application.
§Arguments
name- Command name that users will typehandler- Boxed command handler implementingCommandHandler
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 handler with the application.
§Arguments
name- Command name that users will typehandler- Boxed async command handler implementingAsyncCommandHandler
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
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.
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>>
Sourcepub async fn execute_command(&mut self, command: &str) -> String
pub async fn execute_command(&mut self, command: &str) -> String
Executes a command by looking it up in the registered commands.
For sync commands, executes immediately and returns the result. For async commands, spawns them in the background and returns immediately.
§Arguments
command- Full command string including arguments
§Returns
String output from the command execution (empty for async commands)
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 main() {
let mut app = TerminalApp::new();
app.info("Application started successfully!");
app.info("Running tasks...");
}Sourcepub fn debug(&mut self, message: &str)
pub fn debug(&mut self, message: &str)
Log debug-level messages.
§Examples
use daemon_console::TerminalApp;
fn 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 main() {
let mut app = TerminalApp::new();
app.warn("You get a warning!");
app.warn("Continue running...");
}