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);
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
openmsx::KeyEvent
Definition: InputEvents.hh:26
openmsx::CircularBuffer
Definition: CircularBuffer.hh:9
IntegerSetting.hh
InterpreterOutput.hh
openmsx::CommandConsole::getColumns
unsigned getColumns() const
Definition: CommandConsole.hh:80
openmsx::CommandConsole::~CommandConsole
~CommandConsole()
Definition: CommandConsole.cc:151
EventListener.hh
openmsx::ConsoleLine::substr
ConsoleLine substr(size_t pos, size_t len) const
Get a part of total line.
Definition: CommandConsole.cc:66
openmsx::ConsoleLine::numChunks
size_t numChunks() const
Get the number of different chunks.
Definition: CommandConsole.hh:43
openmsx::ConsoleLine::str
const std::string & str() const
Get the total string, ignoring color differences.
Definition: CommandConsole.hh:39
openmsx::EventDistributor
Definition: EventDistributor.hh:16
BooleanSetting.hh
openmsx::CommandConsole::setRows
void setRows(unsigned rows_)
Definition: CommandConsole.hh:81
openmsx::ConsoleLine::chunkColor
uint32_t chunkColor(size_t i) const
Get the color for the i-th chunk.
Definition: CommandConsole.cc:50
openmsx::CommandConsole::getLine
ConsoleLine getLine(unsigned line) const
Definition: CommandConsole.cc:200
openmsx::ConsoleLine::addChunk
void addChunk(std::string_view text, uint32_t rgb)
Append a chunk with a (different) color.
Definition: CommandConsole.cc:39
openmsx::EventListener
Definition: EventListener.hh:10
openmsx::CommandConsole::getConsoleSetting
BooleanSetting & getConsoleSetting()
Definition: CommandConsole.hh:73
openmsx::CommandConsole::setColumns
void setColumns(unsigned columns_)
Definition: CommandConsole.hh:79
CircularBuffer.hh
openmsx::CommandConsole::CommandConsole
CommandConsole(GlobalCommandController &commandController, EventDistributor &eventDistributor, Display &display)
Definition: CommandConsole.cc:104
openmsx::ConsoleLine
This class represents a single text line in the console.
Definition: CommandConsole.hh:22
openmsx::Display
Represents the output window/screen of openMSX.
Definition: Display.hh:31
openmsx::BooleanSetting
Definition: BooleanSetting.hh:8
openmsx::IntegerSetting
A Setting with an integer value.
Definition: IntegerSetting.hh:10
openmsx::ConsoleLine::numChars
size_t numChars() const
Get the number of UTF8 characters in this line.
Definition: CommandConsole.cc:45
openmsx::InterpreterOutput
Definition: InterpreterOutput.hh:8
circular_buffer< std::string >
openmsx::CommandConsole::getCursorPosition
void getCursorPosition(unsigned &xPosition, unsigned &yPosition) const
Definition: CommandConsole.cc:193
circular_buffer.hh
openmsx::GlobalCommandController
Definition: GlobalCommandController.hh:35
openmsx::ConsoleLine::ConsoleLine
ConsoleLine()=default
Construct empty line.
openmsx
Thanks to enen for testing this on a real cartridge:
Definition: Autofire.cc:5
openmsx::CommandConsole::getRows
unsigned getRows() const
Definition: CommandConsole.hh:82
openmsx::ConsoleLine::chunkText
std::string_view chunkText(size_t i) const
Get the text for the i-th chunk.
Definition: CommandConsole.cc:56
openmsx::CommandConsole::getScrollBack
unsigned getScrollBack() const
Definition: CommandConsole.hh:75
openmsx::CommandConsole
Definition: CommandConsole.hh:64