Branch data Line data Source code
1 : : // Copyright 2026 HPActor Contributors
2 : : // Licensed under the Apache License, Version 2.0
3 : : #pragma once
4 : :
5 : : #include <cstdint>
6 : : #include <string>
7 : : #include <vector>
8 : :
9 : : // Forward-declare linenoise C types to avoid pulling linenoise.h into public headers.
10 : : struct linenoiseCompletions;
11 : :
12 : : namespace hpactor::cli {
13 : :
14 : : struct CommandNode;
15 : :
16 : : struct LineEditorConfig {
17 : : std::string history_path;
18 : : uint32_t history_max = 1000;
19 : : bool multiline = false;
20 : : };
21 : :
22 : : class LineEditor {
23 : : public:
24 : : LineEditor(const LineEditorConfig& cfg, const CommandNode* root);
25 : : ~LineEditor();
26 : :
27 : : LineEditor(const LineEditor&) = delete;
28 : : LineEditor& operator=(const LineEditor&) = delete;
29 : :
30 : : std::string readline(const std::string& prompt);
31 : : void add_history(const std::string& line) const;
32 : : void load_history() const;
33 : : void save_history() const;
34 : 1 : void set_root(const CommandNode* root) { root_ = root; }
35 : :
36 : : private:
37 : : // linenoise completion callback (global, no ctx — uses current_ editor pointer).
38 : : // Called on Tab to populate completions.
39 : : static void on_completion(const char* buf,
40 : : struct linenoiseCompletions* lc);
41 : :
42 : : // linenoise hints callback (global, no ctx — uses current_ editor pointer).
43 : : // Called on each keystroke; returns gray hint string.
44 : : static char* on_hints(const char* buf,
45 : : int* color,
46 : : int* bold);
47 : :
48 : : // linenoise free-hints callback (global). Frees the string returned by on_hints.
49 : : static void on_free_hints(void* hint);
50 : :
51 : : void install_callbacks();
52 : : static std::vector<std::string> tokenize_partial(const std::string& buf);
53 : :
54 : : const CommandNode* root_;
55 : : LineEditorConfig config_;
56 : : bool callbacks_installed_ = false;
57 : :
58 : : // Global pointer to the one active LineEditor (CLI is single-instance).
59 : : // Set by constructor, cleared by destructor.
60 : : static LineEditor* current_;
61 : : };
62 : :
63 : : } // namespace hpactor::cli
|