19 #include <uuid/console.h>
30 #include <uuid/common.h>
33 #if !defined(__cpp_lib_make_unique) && !defined(DOXYGEN)
36 template<
typename _Tp,
typename... _Args>
37 inline unique_ptr<_Tp> make_unique(_Args&&... __args) {
38 return unique_ptr<_Tp>(
new _Tp(std::forward<_Args>(__args)...));
49 Shell::Shell(Stream &stream, std::shared_ptr<Commands> commands,
unsigned int context,
unsigned int flags)
50 : stream_(stream), commands_(std::move(commands)), flags_(flags) {
76 blocking_data->
stop_ =
true;
99 return commands_->available_commands(*
this);
130 const int input =
stream_.read();
137 const unsigned char c = input;
203 if (c >=
'\x20' && c <=
'\x7E') {
223 : password_prompt_(password_prompt), password_function_(std::move(
password_function)) {
228 const int input =
stream_.read();
235 const unsigned char c = input;
282 if (c >=
'\x20' && c <=
'\x7E') {
301 : delay_time_(delay_time), delay_function_(std::move(
delay_function)) {
314 function_copy(*
this);
337 if (blocking_data->blocking_function_(*
this, blocking_data->stop_)) {
338 bool stop_pending = blocking_data->
stop_;
358 mode_data_ = std::make_unique<Shell::PasswordData>(prompt, std::move(
function));
369 mode_data_ = std::make_unique<Shell::DelayData>(ms, std::move(
function));
376 mode_data_ = std::make_unique<Shell::BlockingData>(std::move(
function));
383 if (pos == std::string::npos) {
414 if (!command_line->empty()) {
416 auto execution =
commands_->execute_command(*
this, std::move(command_line));
418 if (execution.error !=
nullptr) {
422 println(F(
"No commands configured"));
435 if (!command_line->empty() &&
commands_) {
436 auto completion =
commands_->complete_command(*
this, command_line);
437 bool redisplay =
false;
439 if (!completion.help.empty()) {
443 for (
auto &help : completion.help) {
450 if (!completion.replacement->empty()) {
Representation of a command line, with parameters separated by spaces and an optional trailing space.
Available commands in the shell's context with the current flags.
Data for the Mode::BLOCKING shell mode.
BlockingData(blocking_function &&blocking_function)
Create Mode::DELAY shell mode data.
Data for the Mode::DELAY shell mode.
DelayData(uint64_t delay_time, delay_function &&delay_function)
Create Mode::DELAY shell mode data.
delay_function delay_function_
Data for the Mode::PASSWORD shell mode.
password_function password_function_
PasswordData(const __FlashStringHelper *password_prompt, password_function &&password_function)
Create Mode::PASSWORD shell mode data.
void delay_for(unsigned long ms, delay_function function)
Stop executing anything on this shell for a period of time.
size_t println(const std::string &data)
Output a string followed by CRLF end of line characters.
size_t maximum_command_line_length_
size_t maximum_command_line_length() const
Get the maximum length of a command line.
void loop_password()
Perform one execution step in Mode::PASSWORD mode.
virtual void end_of_transmission()
The end of transmission character has been received.
virtual bool exit_context()
Pop a context off the stack.
void loop_one()
Perform one execution step of this shell.
void start()
Perform startup process for this shell.
void output_logs()
Output queued log messages for this shell.
size_t print(const std::string &data)
Output a string.
virtual void stopped()
Stop request event.
void block_with(blocking_function function)
Execute a blocking function on this shell until it returns true.
void loop_delay()
Perform one execution step in Mode::DELAY mode.
std::function< void(Shell &shell, bool completed, const std::string &password)> password_function
Function to handle the response to a password entry prompt.
unsigned int context() const
Get the context at the top of the stack.
void enter_context(unsigned int context)
Push a new context onto the stack.
void loop_blocking()
Perform one execution step in Mode::BLOCKING mode.
bool running() const
Determine if this shell is still running.
void enter_password(const __FlashStringHelper *prompt, password_function function)
Prompt for a password to be entered on this shell.
Commands::AvailableCommands available_commands() const
Get the available commands in the current context and with the current flags.
void loop_normal()
Perform one execution step in Mode::NORMAL mode.
void invoke_command(const std::string &line)
Invoke a command on the shell.
virtual void started()
Startup complete event.
std::function< void(Shell &shell)> delay_function
Function to handle the end of an execution delay.
virtual void erase_characters(size_t count)
Output ANSI escape sequence to erase characters.
void process_command()
Try to execute a command with the current command line.
void process_password(bool completed)
Finish password entry.
Shell(Stream &stream, std::shared_ptr< Commands > commands, unsigned int context=0, unsigned int flags=0)
Create a new Shell operating on a Stream with the given commands, default context and initial flags.
void process_completion()
Try to complete a command from the current command line.
virtual void display_banner()
Output the startup banner.
size_t write(uint8_t data) final override
Write one byte to the output stream.
virtual void erase_current_line()
Output ANSI escape sequence to erase the current line.
void display_prompt()
Output a prompt on the shell.
void delete_buffer_word(bool display)
Delete a word from the command line buffer.
void check_idle_timeout()
Check idle timeout expiry.
std::unique_ptr< ModeData > mode_data_
std::shared_ptr< Commands > commands_
std::function< bool(Shell &shell, bool stop)> blocking_function
Function to handle a blocking operation.
static std::set< std::shared_ptr< Shell > > & registered_shells()
Get registered running shells to be executed.
void delay_until(uint64_t ms, delay_function function)
Stop executing anything on this shell until a future time is reached.
std::deque< unsigned int > context_
void stop()
Stop this shell from running.
unsigned long idle_timeout() const
Get the idle timeout.
static void register_handler(Handler *handler, Level level)
Register a log handler.
uint64_t get_uptime_ms()
Get the current uptime as a 64-bit milliseconds value.