pub trait Execute {
type Context;
// Required methods
fn prompt(&self, ctx: &Self::Context) -> String;
fn prepare(&self, cmd: &str) -> Prepare;
fn execute(
&self,
ctx: &mut Self::Context,
cmd: CommandInput,
) -> Result<OutputAction>;
// Provided method
fn completion(
&self,
_ctx: &Self::Context,
_incomplete_command: &str,
) -> Result<(String, Vec<String>)> { ... }
}Expand description
Execute this is the heart of the shell. This is the trait that is implemented by the
commands that are to be executed.
This trait is responsible for:
- Prompting the user for input.
- Completing the command. (optional)
- Preparing the command for execution.
- Executing the command.
This is the only trait that is required by the user to implement to create a REPL.
Required Associated Types§
Sourcetype Context
type Context
This is the context that is maintained by the App struct. This is specific to your
Execute trait. This can contain any data that is required by the command.
The context is read only during the Execute::prompt, Execute::completion & Execute::prepare method,
and is mutable during the Execute::execute method.
Required Methods§
Sourcefn prompt(&self, ctx: &Self::Context) -> String
fn prompt(&self, ctx: &Self::Context) -> String
This is the prompt that is displayed to the user. This is the first thing that is displayed, and the user is expected to enter a command.
Sourcefn prepare(&self, cmd: &str) -> Prepare
fn prepare(&self, cmd: &str) -> Prepare
This is the prepare method. This is called before executing the command. This is used to prepare the command for execution.
This can be used to check if the command requires stdin, and prompt the user for input.
Sourcefn execute(
&self,
ctx: &mut Self::Context,
cmd: CommandInput,
) -> Result<OutputAction>
fn execute( &self, ctx: &mut Self::Context, cmd: CommandInput, ) -> Result<OutputAction>
This is the execute method. This is called to execute the command. This is where the command is executed. This is where the command is executed, and the output is returned.
Provided Methods§
Sourcefn completion(
&self,
_ctx: &Self::Context,
_incomplete_command: &str,
) -> Result<(String, Vec<String>)>
fn completion( &self, _ctx: &Self::Context, _incomplete_command: &str, ) -> Result<(String, Vec<String>)>
This is the completion that is displayed to the user. This is displayed when the user
presses the Tab key. This is optional, and can be left empty.
The completion works in the following way:
When the user presses the Tab key, the shell will call the Execute::completion
method. This method returns 2 things (deterministic completion and non-deterministic
completion).
The deterministic completion is applied to the command, and the non-deterministic is shown below the command.