openMSX
CommandConsole.hh
Go to the documentation of this file.
1 #ifndef COMMANDCONSOLE_HH
2 #define COMMANDCONSOLE_HH
3 
4 #include "EventListener.hh"
5 #include "InterpreterOutput.hh"
6 #include "BooleanSetting.hh"
7 #include "IntegerSetting.hh"
8 #include "gl_vec.hh"
9 #include "CircularBuffer.hh"
10 #include "circular_buffer.hh"
11 #include <string_view>
12 #include <vector>
13 
14 namespace openmsx {
15 
16 class GlobalCommandController;
17 class EventDistributor;
18 class KeyEvent;
19 class Display;
20 
24 {
25 public:
27  ConsoleLine() = default;
28 
30  explicit ConsoleLine(std::string line, uint32_t rgb = 0xffffff);
31 
34  void addChunk(std::string_view text, uint32_t rgb);
35 
38  [[nodiscard]] size_t numChars() const;
40  [[nodiscard]] const std::string& str() const { return line; }
41 
44  [[nodiscard]] size_t numChunks() const { return chunks.size(); }
46  [[nodiscard]] uint32_t chunkColor(size_t i) const;
48  [[nodiscard]] std::string_view chunkText(size_t i) const;
49 
50  [[nodiscard]] const auto& getChunks() const { return chunks; }
51 
52 private:
53  std::string line;
54  struct Chunk {
55  uint32_t rgb;
56  std::string_view::size_type pos;
57  };
58  std::vector<Chunk> chunks;
59 };
60 
61 
62 class CommandConsole final : private EventListener
63  , private InterpreterOutput
64 {
65 public:
66  CommandConsole(GlobalCommandController& commandController,
67  EventDistributor& eventDistributor,
68  Display& display);
70 
71  [[nodiscard]] BooleanSetting& getConsoleSetting() { return consoleSetting; }
72 
73  [[nodiscard]] unsigned getScrollBack() const { return consoleScrollBack; }
74  [[nodiscard]] gl::ivec2 getCursorPosition() const;
75 
76  void setColumns(unsigned columns_) { columns = columns_; }
77  [[nodiscard]] unsigned getColumns() const { return columns; }
78  void setRows(unsigned rows_) { rows = rows_; }
79  [[nodiscard]] unsigned getRows() const { return rows; }
80 
81  [[nodiscard]] const auto& getLines() const { return lines; }
82 
83 private:
84  // InterpreterOutput
85  void output(std::string_view text) override;
86  [[nodiscard]] unsigned getOutputColumns() const override;
87 
88  // EventListener
89  int signalEvent(const Event& event) noexcept override;
90 
91  bool handleEvent(const KeyEvent& keyEvent);
92  void tabCompletion();
93  void commandExecute();
94  void scroll(int delta);
95  void gotoStartOfWord();
96  void deleteToStartOfWord();
97  void gotoEndOfWord();
98  void deleteToEndOfWord();
99  void prevCommand();
100  void nextCommand();
101  void clearCommand();
102  void clearHistory();
103  void backspace();
104  void delete_key();
105  void normalKey(uint32_t chr);
106  void putCommandHistory(const std::string& command);
107  void newLineConsole(std::string line);
108  void newLineConsole(ConsoleLine line);
109  void putPrompt();
110  void resetScrollBack();
111  void paste();
112  [[nodiscard]] ConsoleLine highLight(std::string_view line);
113 
116  void print(std::string_view text, unsigned rgb = 0xffffff);
117 
118  void loadHistory();
119  void saveHistory();
120 
121 private:
122  GlobalCommandController& commandController;
123  EventDistributor& eventDistributor;
124  Display& display;
125  BooleanSetting consoleSetting;
126  IntegerSetting historySizeSetting;
127  BooleanSetting removeDoublesSetting;
128 
129  static constexpr int LINESHISTORY = 1000;
131  std::string commandBuffer;
132  std::string prompt;
134  std::string currentLine;
136  unsigned commandScrollBack;
137  unsigned columns;
138  unsigned rows;
139  int consoleScrollBack;
141  unsigned cursorPosition;
142  bool executingCommand;
143 };
144 
145 } // namespace openmsx
146 
147 #endif
CommandConsole(GlobalCommandController &commandController, EventDistributor &eventDistributor, Display &display)
unsigned getScrollBack() const
gl::ivec2 getCursorPosition() const
void setRows(unsigned rows_)
unsigned getColumns() const
unsigned getRows() const
BooleanSetting & getConsoleSetting()
void setColumns(unsigned columns_)
const auto & getLines() const
This class represents a single text line in the console.
const std::string & str() const
Get the total string, ignoring color differences.
std::string_view chunkText(size_t i) const
Get the text for the i-th chunk.
ConsoleLine()=default
Construct empty line.
size_t numChars() const
Get the number of UTF8 characters in this line.
size_t numChunks() const
Get the number of different chunks.
uint32_t chunkColor(size_t i) const
Get the color for the i-th chunk.
void addChunk(std::string_view text, uint32_t rgb)
Append a chunk with a (different) color.
const auto & getChunks() const
Represents the output window/screen of openMSX.
Definition: Display.hh:33
A Setting with an integer value.
This file implemented 3 utility functions:
Definition: Autofire.cc:9