VirtualC64 v5.0 beta
Commodore 64 Emulator
Loading...
Searching...
No Matches
RetroShell.h
1// -----------------------------------------------------------------------------
2// This file is part of VirtualC64
3//
4// Copyright (C) Dirk W. Hoffmann. www.dirkwhoffmann.de
5// This FILE is dual-licensed. You are free to choose between:
6//
7// - The GNU General Public License v3 (or any later version)
8// - The Mozilla Public License v2
9//
10// SPDX-License-Identifier: GPL-3.0-or-later OR MPL-2.0
11// -----------------------------------------------------------------------------
12
13#pragma once
14
15#include "RetroShellTypes.h"
16#include "SubComponent.h"
17#include "Interpreter.h"
18#include "TextStorage.h"
19#include <sstream>
20#include <fstream>
21
22namespace vc64 {
23
24class RetroShell : public SubComponent {
25
26 Descriptions descriptions = {{
27
28 .name = "RetroShell",
29 .description = "Command Console"
30 }};
31
32 friend class Interpreter;
33
34 // The command interpreter (parses commands typed into the console window)
35 Interpreter interpreter;
36
37
38 //
39 // Text storage
40 //
41
42 // The text storage
43 TextStorage storage;
44
45 // History buffer storing old input strings and cursor positions
46 std::vector<std::pair<string,isize>> history;
47
48 // The currently active input string
49 isize ipos = 0;
50
51
52 //
53 // User input
54 //
55
56 // Input line
57 string input;
58
59 // Command queue (stores all pending commands)
60 std::vector<std::pair<isize,string>> commands;
61
62 // Input prompt
63 string prompt = "vc64% ";
64
65 // Cursor position
66 isize cursor = 0;
67
68 // Indicates if TAB was the most recently pressed key
69 bool tabPressed = false;
70
71
72 //
73 // Scripts
74 //
75
76 // The currently processed script
77 std::stringstream script;
78
79 // The script line counter (first line = 1)
80 isize scriptLine = 0;
81
82
83 //
84 // Initializing
85 //
86
87public:
88
89 RetroShell(C64& ref);
90
91 RetroShell& operator= (const RetroShell& other) { return *this; }
92
93
94 //
95 // Methods from Serializable
96 //
97
98public:
99
100 template <class T> void serialize(T& worker) { } SERIALIZERS(serialize);
101
102
103 //
104 // Methods from CoreComponent
105 //
106
107public:
108
109 const Descriptions &getDescriptions() const override { return descriptions; }
110
111private:
112
113 void _dump(Category category, std::ostream& os) const override { }
114 void _initialize() override;
115 void _pause() override;
116
117
118 //
119 // Working with the text storage
120 //
121
122public:
123
124 // Prints a message
125 RetroShell &operator<<(char value);
126 RetroShell &operator<<(const string &value);
127 RetroShell &operator<<(int value);
128 RetroShell &operator<<(unsigned int value);
129 RetroShell &operator<<(long value);
130 RetroShell &operator<<(unsigned long value);
131 RetroShell &operator<<(long long value);
132 RetroShell &operator<<(unsigned long long value);
133 RetroShell &operator<<(std::stringstream &stream);
134
135 // Returns the prompt
136 const string &getPrompt();
137
138 // Updates the prompt according to the current shell mode
139 void updatePrompt();
140
141 // Returns the contents of the whole storage as a single C string
142 const char *text();
143
144 // Moves the cursor forward to a certain column
145 void tab(isize pos);
146
147 // Assigns an additional output stream
148 void setStream(std::ostream &os);
149
150private:
151
152 // Marks the text storage as dirty
153 void needsDisplay();
154
155 // Clears the console window
156 void clear();
157
158 // Prints the welcome message
159 void welcome();
160
161 // Prints the help line
162 void printHelp();
163
164 // Prints a state summary (used by the debug shell)
165 void printState();
166
167
168 //
169 // Managing user input
170 //
171
172public:
173
174 // Returns the size of the current user-input string
175 isize inputLength() { return (isize)input.length(); }
176
177 // Presses a key or a series of keys
178 void press(RetroShellKey key, bool shift = false);
179 void press(char c);
180 void press(const string &s);
181
182 // Returns the cursor position relative to the line end
183 isize cursorRel();
184
185
186 //
187 // Working with the history buffer
188 //
189
190public:
191
192 isize historyLength() { return (isize)history.size(); }
193
194
195 //
196 // Executing commands
197 //
198
199public:
200
201 // Main entry point for executing commands that were typed in by the user
202 void execUserCommand(const string &command);
203
204 // Executes all pending commands
205 void exec() throws;
206
207 // Executes a single command
208 void exec(const string &command, isize line = 0) throws;
209
210 // Executes a shell script
211 void execScript(std::stringstream &ss) throws;
212 void execScript(const std::ifstream &fs) throws;
213 void execScript(const string &contents) throws;
214 void execScript(const class MediaFile &script) throws;
215 void abortScript();
216
217private:
218
219 // Prints a textual description of an error in the console
220 void describe(const std::exception &exception, isize line = 0, const string &cmd = "");
221
222 // Prints help messages for a given command string
223 void help(const string &command);
224
225
226 //
227 // Command handlers
228 //
229
230public:
231
232 void dump(Dumpable &component, std::vector <Category> categories);
233 void dump(Dumpable &component, Category category);
234
235private:
236
237 void _dump(Dumpable &component, Category category);
238
239
240 //
241 // Servicing events
242 //
243
244public:
245
246 void serviceEvent();
247};
248
249}
VirtualC64 project namespace.
Definition CmdQueue.cpp:16