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 "CircularBuffer.hh"
9 #include "circular_buffer.hh"
10 #include <string_view>
11 #include <vector>
12 
13 namespace openmsx {
14 
15 class GlobalCommandController;
16 class EventDistributor;
17 class KeyEvent;
18 class Display;
19 
23 {
24 public:
26  ConsoleLine() = default;
27 
29  explicit ConsoleLine(std::string line, uint32_t rgb = 0xffffff);
30 
33  void addChunk(std::string_view text, uint32_t rgb);
34 
37  size_t numChars() const;
39  const std::string& str() const { return line; }
40 
43  size_t numChunks() const { return chunks.size(); }
45  uint32_t chunkColor(size_t i) const;
47  std::string_view chunkText(size_t i) const;
48 
56  ConsoleLine substr(size_t pos, size_t len) const;
57 
58 private:
59  std::string line;
60  std::vector<std::pair<uint32_t, std::string_view::size_type>> chunks; // [rgb, pos]
61 };
62 
63 
64 class CommandConsole final : private EventListener
65  , private InterpreterOutput
66 {
67 public:
68  CommandConsole(GlobalCommandController& commandController,
69  EventDistributor& eventDistributor,
70  Display& display);
71  ~CommandConsole();
72 
73  BooleanSetting& getConsoleSetting() { return consoleSetting; }
74 
75  unsigned getScrollBack() const { return consoleScrollBack; }
76  ConsoleLine getLine(unsigned line) const;
77  void getCursorPosition(unsigned& xPosition, unsigned& yPosition) const;
78 
79  void setColumns(unsigned columns_) { columns = columns_; }
80  unsigned getColumns() const { return columns; }
81  void setRows(unsigned rows_) { rows = rows_; }
82  unsigned getRows() const { return rows; }
83 
84 private:
85  // InterpreterOutput
86  void output(std::string_view text) override;
87  unsigned getOutputColumns() const override;
88 
89  // EventListener
90  int signalEvent(const std::shared_ptr<const Event>& event) override;
91 
92  bool handleEvent(const KeyEvent& keyEvent);
93  void tabCompletion();
94  void commandExecute();
95  void scroll(int delta);
96  void prevCommand();
97  void nextCommand();
98  void clearCommand();
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  ConsoleLine highLight(std::string_view line);
108 
111  void print(std::string_view text, unsigned rgb = 0xffffff);
112 
113  void loadHistory();
114  void saveHistory();
115 
116  GlobalCommandController& commandController;
117  EventDistributor& eventDistributor;
118  Display& display;
119  BooleanSetting consoleSetting;
120  IntegerSetting historySizeSetting;
121  BooleanSetting removeDoublesSetting;
122 
123  static constexpr int LINESHISTORY = 1000;
125  std::string commandBuffer;
126  std::string prompt;
128  std::string currentLine;
130  unsigned commandScrollBack;
131  unsigned columns;
132  unsigned rows;
133  int consoleScrollBack;
135  unsigned cursorPosition;
136  bool executingCommand;
137 };
138 
139 } // namespace openmsx
140 
141 #endif
Represents the output window/screen of openMSX.
Definition: Display.hh:31
BooleanSetting & getConsoleSetting()
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.
uint32_t chunkColor(size_t i) const
Get the color for the i-th chunk.
This class represents a single text line in the console.
A Setting with an integer value.
void setRows(unsigned rows_)
unsigned getScrollBack() const
size_t numChunks() const
Get the number of different chunks.
ConsoleLine substr(size_t pos, size_t len) const
Get a part of total line.
unsigned getColumns() const
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
size_t numChars() const
Get the number of UTF8 characters in this line.
ConsoleLine()=default
Construct empty line.
void setColumns(unsigned columns_)
void addChunk(std::string_view text, uint32_t rgb)
Append a chunk with a (different) color.
unsigned getRows() const