1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//! Command execution and shell interaction primitives for building REPLs.
//!
//! This module provides the core traits and types needed to implement custom REPL (Read-Eval-Print Loop)
//! shells. The primary components are:
//!
//! - [`Execute`]: The main trait for implementing shell command execution
//! - [`CommandInput`]: Input data structure passed to command executors
//! - [`CommandOutput`]: Output data structure for command results
//! - [`OutputAction`]: Enum controlling shell behavior after command execution
//!
//! # Architecture
//!
//! The command execution flow follows these steps:
//!
//! 1. Shell displays a prompt (via [`Execute::prompt`])
//! 2. User enters a command
//! 3. Shell optionally handles tab completion (via [`Execute::completion`])
//! 4. Shell prepares command execution (via [`Execute::prepare`])
//! 5. Command is executed (via [`Execute::execute`])
//! 6. Output is rendered based on returned [`OutputAction`]
//!
//! # Example
//!
//! ```rust
//! use shelgon::command::{self, Execute, CommandInput, CommandOutput, OutputAction};
//!
//! struct MyExecutor {}
//!
//! impl Execute for MyExecutor {
//! type Context = ();
//!
//! fn prompt(&self, _: &Self::Context) -> String {
//! "$ ".to_string()
//! }
//!
//! fn prepare(&self, cmd: &str) -> command::Prepare {
//! command::Prepare {
//! command: cmd.to_string(),
//! stdin_required: false,
//! }
//! }
//!
//! fn execute(
//! &self,
//! _: &mut Self::Context,
//! input: CommandInput
//! ) -> anyhow::Result<OutputAction> {
//! Ok(OutputAction::Command(CommandOutput {
//! prompt: input.prompt,
//! command: input.command.clone(),
//! stdin: Vec::new(),
//! stdout: vec![format!("Executed: {}", input.command)],
//! stderr: Vec::new(),
//! }))
//! }
//! }
//! ```
//!
//! # Features
//!
//! - **tokio**: Enables async runtime support via [`tokio::runtime::Runtime`] in [`CommandInput`]
use Arc;
use Runtime;
///
/// [`CommandOutput`] is the output supplied to the renderer by the [`Execute`] trait.
///
/// `prompt` & `command` are the prompt and command that were executed.
/// `stdin` is the input that was supplied to the command. (optional)
/// `stdout` & `stderr` are the output of the command.
///
///
/// [`OutputAction`] is the action that the renderer should take after receiving the output from
/// the [`Execute`] trait.
///
/// This is to perform specialized actions on the shell, altering the rendered output from within
/// the [`Execute`] trait.
///
///
///
/// [`CommandInput`] is the input supplied to the [`Execute`] trait.
///
/// This is the input that is received by the renderer, and is passed to the [`Execute`] trait.
/// This happens when the user submits a command.
///
///
/// [`Prepare`] is the output of the [`Execute::prepare`] method.
///
/// This prepares the renderer, in case if some special actions are required before executing the
/// command.
///
/// Supported behaviors:
/// - `stdin_required`: If the command requires stdin, the renderer should prompt the user for input.
///
///
/// [`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.
///
///
///
/// [`New`] is the trait that is implemented by the commands that are to be executed. This is used
/// to quickly create a new instance of the command.
///