openMSX
CommandConsole.hh
Go to the documentation of this file.
1#ifndef COMMANDCONSOLE_HH
2#define COMMANDCONSOLE_HH
3
4#include "EventListener.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
14namespace openmsx {
15
16class GlobalCommandController;
17class EventDistributor;
18class KeyEvent;
19class Display;
20
24{
25public:
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
52private:
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
62class CommandConsole final : private EventListener
63 , private InterpreterOutput
64{
65public:
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
83private:
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) 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
121private:
122 GlobalCommandController& commandController;
123 EventDistributor& eventDistributor;
124 Display& display;
125 BooleanSetting consoleSetting;
126 IntegerSetting historySizeSetting;
127 BooleanSetting removeDoublesSetting;
128
129 static constexpr int LINES_HISTORY = 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 = false;
143};
144
145} // namespace openmsx
146
147#endif
CommandConsole(GlobalCommandController &commandController, EventDistributor &eventDistributor, Display &display)
unsigned getScrollBack() const
gl::ivec2 getCursorPosition() const
BooleanSetting & getConsoleSetting()
void setRows(unsigned rows_)
unsigned getColumns() const
unsigned getRows() const
const auto & getLines() const
void setColumns(unsigned columns_)
This class represents a single text line in the console.
std::string_view chunkText(size_t i) const
Get the text for the i-th chunk.
const std::string & str() const
Get the total string, ignoring color differences.
ConsoleLine()=default
Construct empty line.
const auto & getChunks() const
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.
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