From 557db51ffd8bbbe407c84bcab23ddf40e7b4ba58 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Tue, 17 Oct 2023 01:23:01 +0300 Subject: [PATCH] terminal: error output --- code/espurna/terminal_commands.cpp | 34 ++++++++++++++++++++---------- code/espurna/terminal_commands.h | 7 ++++-- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/code/espurna/terminal_commands.cpp b/code/espurna/terminal_commands.cpp index 81b9e76c..ffd33f5c 100644 --- a/code/espurna/terminal_commands.cpp +++ b/code/espurna/terminal_commands.cpp @@ -100,29 +100,33 @@ void error(Print& print, const String& message) { } void error(const espurna::terminal::CommandContext& ctx, const String& message) { - error(ctx.output, message); + error(ctx.error, message); } -bool find_and_call(CommandLine cmd, Print& out) { +bool find_and_call(CommandLine cmd, Print& output, Print& error_output) { const auto* command = find(cmd.argv[0]); if (command) { - (*command).func(CommandContext{ - .argv = std::move(cmd.argv), - .output = out }); + (*command).func( + CommandContext{ + .argv = std::move(cmd.argv), + .output = output, + .error = error_output, + }); + return true; } - error(out, F("Command not found")); + error(output, F("Command not found")); return false; } -bool find_and_call(StringView cmd, Print& out) { +bool find_and_call(StringView cmd, Print& output, Print& error_output) { auto result = parse_line(cmd); if (result.error != parser::Error::Ok) { String message; message += STRING_VIEW("TERMINAL: "); message += parser::error(result.error); - error(out, message); + error(error_output, message); return false; } @@ -130,10 +134,14 @@ bool find_and_call(StringView cmd, Print& out) { return false; } - return find_and_call(std::move(result), out); + return find_and_call(std::move(result), output, error_output); +} + +bool find_and_call(StringView cmd, Print& output) { + return find_and_call(cmd, output, output); } -bool api_find_and_call(StringView cmd, Print& out) { +bool api_find_and_call(StringView cmd, Print& output, Print& error_output) { bool result { true }; LineView lines(cmd); @@ -144,7 +152,7 @@ bool api_find_and_call(StringView cmd, Print& out) { } // prefer to break early when commands are missing - if (!find_and_call(line, out)) { + if (!find_and_call(line, output, error_output)) { result = false; break; } @@ -153,5 +161,9 @@ bool api_find_and_call(StringView cmd, Print& out) { return result; } +bool api_find_and_call(StringView cmd, Print& output) { + return api_find_and_call(cmd, output, output); +} + } // namespace terminal } // namespace espurna diff --git a/code/espurna/terminal_commands.h b/code/espurna/terminal_commands.h index 099f672b..5a5ef417 100644 --- a/code/espurna/terminal_commands.h +++ b/code/espurna/terminal_commands.h @@ -24,6 +24,7 @@ namespace terminal { struct CommandContext { Argv argv; Print& output; + Print& error; }; using CommandFunc = void(*)(CommandContext&&); @@ -66,10 +67,12 @@ bool find_and_call(StringView, Print& output); bool find_and_call(CommandLine, Print& output); // search the given string for valid commands and call them in sequence -// stops on first 'not found' command, does not take into an account -// error output of any commands (TODO ?) bool api_find_and_call(StringView, Print& output); +// search the given string for valid commands and call them in sequence +// separate outputs for command and errors +bool api_find_and_call(StringView, Print& output, Print& error); + // helper functions for most common success output void ok(Print&); void ok(const espurna::terminal::CommandContext&);