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  std::vector<std::pair<uint32_t, std::string_view::size_type>> chunks; // [rgb, pos]
55 };
56 
57 
58 class CommandConsole final : private EventListener
59  , private InterpreterOutput
60 {
61 public:
62  CommandConsole(GlobalCommandController& commandController,
63  EventDistributor& eventDistributor,
64  Display& display);
66 
67  [[nodiscard]] BooleanSetting& getConsoleSetting() { return consoleSetting; }
68 
69  [[nodiscard]] unsigned getScrollBack() const { return consoleScrollBack; }
70  [[nodiscard]] gl::ivec2 getCursorPosition() const;
71 
72  void setColumns(unsigned columns_) { columns = columns_; }
73  [[nodiscard]] unsigned getColumns() const { return columns; }
74  void setRows(unsigned rows_) { rows = rows_; }
75  [[nodiscard]] unsigned getRows() const { return rows; }
76 
77  [[nodiscard]] const auto& getLines() const { return lines; }
78 
79 private:
80  // InterpreterOutput
81  void output(std::string_view text) override;
82  [[nodiscard]] unsigned getOutputColumns() const override;
83 
84  // EventListener
85  int signalEvent(const std::shared_ptr<const Event>& event) override;
86 
87  bool handleEvent(const KeyEvent& keyEvent);
88  void tabCompletion();
89  void commandExecute();
90  void scroll(int delta);
91  void gotoStartOfWord();
92  void deleteToStartOfWord();
93  void gotoEndOfWord();
94  void deleteToEndOfWord();
95  void prevCommand();
96  void nextCommand();
97  void clearCommand();
98  void clearHistory();
99  void backspace();
100  void delete_key();
101  void normalKey(uint32_t chr);
102  void putCommandHistory(const std::string& command);
103  void newLineConsole(std::string line);
104  void newLineConsole(ConsoleLine line);
105  void putPrompt();
106  void resetScrollBack();
107  void paste();
108  [[nodiscard]] ConsoleLine highLight(std::string_view line);
109 
112  void print(std::string_view text, unsigned rgb = 0xffffff);
113 
114  void loadHistory();
115  void saveHistory();
116 
117 private:
118  GlobalCommandController& commandController;
119  EventDistributor& eventDistributor;
120  Display& display;
121  BooleanSetting consoleSetting;
122  IntegerSetting historySizeSetting;
123  BooleanSetting removeDoublesSetting;
124 
125  static constexpr int LINESHISTORY = 1000;
127  std::string commandBuffer;
128  std::string prompt;
130  std::string currentLine;
132  unsigned commandScrollBack;
133  unsigned columns;
134  unsigned rows;
135  int consoleScrollBack;
137  unsigned cursorPosition;
138  bool executingCommand;
139 };
140 
141 } // namespace openmsx
142 
143 #endif
openmsx::KeyEvent
Definition: InputEvents.hh:27
openmsx::CircularBuffer
Definition: CircularBuffer.hh:12
IntegerSetting.hh
InterpreterOutput.hh
openmsx::CommandConsole::getColumns
unsigned getColumns() const
Definition: CommandConsole.hh:73
gl_vec.hh
openmsx::CommandConsole::~CommandConsole
~CommandConsole()
Definition: CommandConsole.cc:122
EventListener.hh
openmsx::ConsoleLine::numChunks
size_t numChunks() const
Get the number of different chunks.
Definition: CommandConsole.hh:44
openmsx::ConsoleLine::str
const std::string & str() const
Get the total string, ignoring color differences.
Definition: CommandConsole.hh:40
openmsx::CommandConsole::getLines
const auto & getLines() const
Definition: CommandConsole.hh:77
openmsx::EventDistributor
Definition: EventDistributor.hh:17
BooleanSetting.hh
openmsx::CommandConsole::setRows
void setRows(unsigned rows_)
Definition: CommandConsole.hh:74
gl::vecN
Definition: gl_vec.hh:36
openmsx::ConsoleLine::chunkColor
uint32_t chunkColor(size_t i) const
Get the color for the i-th chunk.
Definition: CommandConsole.cc:53
openmsx::ConsoleLine::addChunk
void addChunk(std::string_view text, uint32_t rgb)
Append a chunk with a (different) color.
Definition: CommandConsole.cc:42
openmsx::EventListener
Definition: EventListener.hh:11
openmsx::ConsoleLine::getChunks
const auto & getChunks() const
Definition: CommandConsole.hh:50
openmsx::CommandConsole::getConsoleSetting
BooleanSetting & getConsoleSetting()
Definition: CommandConsole.hh:67
openmsx::CommandConsole::setColumns
void setColumns(unsigned columns_)
Definition: CommandConsole.hh:72
CircularBuffer.hh
openmsx::CommandConsole::CommandConsole
CommandConsole(GlobalCommandController &commandController, EventDistributor &eventDistributor, Display &display)
Definition: CommandConsole.cc:75
openmsx::ConsoleLine
This class represents a single text line in the console.
Definition: CommandConsole.hh:24
openmsx::Display
Represents the output window/screen of openMSX.
Definition: Display.hh:33
openmsx::BooleanSetting
Definition: BooleanSetting.hh:9
openmsx::IntegerSetting
A Setting with an integer value.
Definition: IntegerSetting.hh:11
openmsx::ConsoleLine::numChars
size_t numChars() const
Get the number of UTF8 characters in this line.
Definition: CommandConsole.cc:48
openmsx::InterpreterOutput
Definition: InterpreterOutput.hh:9
circular_buffer< std::string >
circular_buffer.hh
openmsx::GlobalCommandController
Definition: GlobalCommandController.hh:37
openmsx::CommandConsole::getCursorPosition
gl::ivec2 getCursorPosition() const
Definition: CommandConsole.cc:164
openmsx::ConsoleLine::ConsoleLine
ConsoleLine()=default
Construct empty line.
openmsx
This file implemented 3 utility functions:
Definition: Autofire.cc:5
openmsx::CommandConsole::getRows
unsigned getRows() const
Definition: CommandConsole.hh:75
openmsx::ConsoleLine::chunkText
std::string_view chunkText(size_t i) const
Get the text for the i-th chunk.
Definition: CommandConsole.cc:59
openmsx::CommandConsole::getScrollBack
unsigned getScrollBack() const
Definition: CommandConsole.hh:69
openmsx::CommandConsole
Definition: CommandConsole.hh:60