Files
ObsidianDragon/src/ui/windows/console_command_executor.h
DanS 5c570613c8 feat(console): lite backend command-reference modal
Give the lite console the parity analog of the full-node RPC command
reference: the same searchable two-pane modal (browse/search, detail
pane, examples, Insert / Insert & run, destructive confirm), driven by
the lite backend's own command set instead of daemon RPC.

- ConsoleCommandExecutor::commandReference() returns the category table
  (full node = consoleCommandCategories(); lite = new
  liteConsoleCommandCategories() -- 25 backend verbs in 5 categories).
  The shared renderCommandsPopup reads the table from the executor.
- The Commands button now shows for both variants (gated on
  commandReference()!=nullptr); title/tooltip/arg-quoting branch on
  hasRpcReference() (clarified to mean "speaks JSON-RPC / full node").
- Lite args are bare tokens (the backend takes one unsplit arg string
  and does not strip quotes), so the param-builder's JSON string
  auto-quoting is disabled for lite -- Insert & run emits runnable bare
  commands. send uses the JSON-array form (its positional form is
  unreachable via the single-arg transport); new uses zs/R; import
  takes just the key (the backend hardcodes birthday=0).

Adds 7 i18n keys across all 8 languages (additive). Full-node behavior
is unchanged. Adversarially reviewed (data vs the backend registry,
plumbing/regression, i18n) with all findings fixed.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-19 12:27:51 -05:00

143 lines
6.5 KiB
C++

// DragonX Wallet - ImGui Edition
// Copyright 2024-2026 The Hush Developers
// Released under the GPLv3
//
// ConsoleCommandExecutor — the pluggable backend behind the shared ConsoleTab UI. It
// abstracts the only real differences between the full-node console (dragonxd log + RPC
// command execution) and the lite console (lite diagnostics ring + backend command),
// so both variants use one rich terminal widget instead of two implementations.
#pragma once
#include "console_channel.h"
#include "imgui.h"
#include <atomic>
#include <cstdint>
#include <deque>
#include <functional>
#include <mutex>
#include <string>
#include <utility>
#include <vector>
namespace dragonx {
class App;
namespace ui {
struct ConsoleCommandCategory; // console_command_reference.h — command-reference table
using ConsoleAddLineFn = std::function<void(const std::string&, ConsoleChannel)>;
struct ConsoleStatusLine {
std::string text;
ImU32 color = IM_COL32(200, 200, 200, 255);
bool pulse = false; // animate the toolbar dot (starting/connecting)
};
// Which log-filter toggles the toolbar should show for a given backend. The full node has a
// daemon/xmrig log + an RPC trace; the lite backend has neither (its diagnostics ring maps to
// the App/Error channels), so it only wants the errors-only + app-messages toggles.
struct ConsoleLogFilterCaps {
bool daemon = false; // daemon + xmrig output toggle
bool errorsOnly = false; // errors-only toggle
bool rpcTrace = false; // RPC method/source trace toggle
bool appMessages = false; // app / diagnostics log toggle
bool any() const { return daemon || errorsOnly || rpcTrace || appMessages; }
};
class ConsoleCommandExecutor {
public:
virtual ~ConsoleCommandExecutor() = default;
// True when commands can be run (RPC connected / lite wallet open).
virtual bool isReady() const = 0;
// A command is currently in flight (input is disabled while true).
virtual bool busy() const { return false; }
// Submit a user command for async execution. The UI has already echoed "> cmd" and
// handled the built-ins (clear/help/quit). Results arrive later via pollResult().
virtual void submit(const std::string& cmd) = 0;
// Pop one completed command result (raw text + error flag) if available; the UI
// formats/colors it via FormatConsoleRpcResultLines. Returns false if none pending.
virtual bool pollResult(std::string& result, bool& isError) = 0;
// Pull any new passive log lines (daemon/xmrig output, or the lite diagnostics ring)
// and hand each to `add`. Called once per frame.
virtual void pollLogLines(const ConsoleAddLineFn& add) { (void)add; }
// UI-chrome capabilities.
// True when this backend speaks JSON-RPC to a daemon (the full node). Gates daemon-specific
// console behavior (the 'stop' shutdown confirm, "not connected to daemon" wording) and the
// command-reference modal's JSON string-arg quoting — the lite backend takes bare tokens.
virtual bool hasRpcReference() const { return false; }
// The command-reference table this backend offers (browsed by the console's reference modal),
// or nullptr for none. Full node = the JSON-RPC reference; lite = its own backend verbs.
virtual const std::vector<ConsoleCommandCategory>* commandReference() const { return nullptr; }
// Which log-filter toggles the toolbar should show (default: none).
virtual ConsoleLogFilterCaps logFilterCaps() const { return {}; }
// Print the backend-appropriate `help` output via `add`.
virtual void printHelp(const ConsoleAddLineFn& add) = 0;
// Extra status lines drawn above the toolbar (lite shows sync + last error).
virtual std::vector<ConsoleStatusLine> statusLines() const { return {}; }
// Short status shown in the toolbar (daemon state / lite connection). Empty text =>
// the toolbar shows its generic "no daemon" label.
virtual ConsoleStatusLine toolbarStatus() const { return {}; }
};
// ── Full-node: dragonxd log ingestion + RPC command execution ────────────────
class FullNodeConsoleExecutor : public ConsoleCommandExecutor {
public:
explicit FullNodeConsoleExecutor(App* app) : app_(app) {}
bool isReady() const override;
void submit(const std::string& cmd) override;
bool pollResult(std::string& result, bool& isError) override;
void pollLogLines(const ConsoleAddLineFn& add) override;
bool hasRpcReference() const override { return true; }
const std::vector<ConsoleCommandCategory>* commandReference() const override;
// Full node: daemon/xmrig log, errors-only, RPC trace, and app messages.
ConsoleLogFilterCaps logFilterCaps() const override { return {true, true, true, true}; }
void printHelp(const ConsoleAddLineFn& add) override;
ConsoleStatusLine toolbarStatus() const override;
// A command is in flight on the RPC worker — lets the UI disable the input (no queued pile-up).
bool busy() const override { return in_flight_.load() > 0; }
private:
App* app_;
std::atomic<int> in_flight_{0};
size_t last_daemon_output_size_ = 0;
size_t last_xmrig_output_size_ = 0;
int last_daemon_state_ = -1; // daemon::EmbeddedDaemon::State as int
bool last_rpc_connected_ = false;
std::deque<std::pair<std::string, bool>> results_; // {text, isError}
std::mutex results_mutex_;
};
// ── Lite: diagnostics ring + backend console command ─────────────────────────
class LiteConsoleExecutor : public ConsoleCommandExecutor {
public:
explicit LiteConsoleExecutor(App* app) : app_(app) {}
bool isReady() const override;
bool busy() const override;
void submit(const std::string& cmd) override;
bool pollResult(std::string& result, bool& isError) override;
void pollLogLines(const ConsoleAddLineFn& add) override;
// Lite: no daemon log / RPC trace — its diagnostics ring maps to the App + Error
// channels, so offer errors-only + app-messages (plus the always-shown text filter).
ConsoleLogFilterCaps logFilterCaps() const override { return {false, true, false, true}; }
const std::vector<ConsoleCommandCategory>* commandReference() const override;
void printHelp(const ConsoleAddLineFn& add) override;
std::vector<ConsoleStatusLine> statusLines() const override;
ConsoleStatusLine toolbarStatus() const override;
private:
App* app_;
std::uint64_t diag_gen_ = static_cast<std::uint64_t>(-1); // last consumed diagnostics generation
};
} // namespace ui
} // namespace dragonx