Orxonox  0.0.5 Codename: Arcturus
Shell.h
Go to the documentation of this file.
1 /*
2  * ORXONOX - the hottest 3D action shooter ever to exist
3  * > www.orxonox.net <
4  *
5  *
6  * License notice:
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  * Author:
23  * Fabian 'x3n' Landau
24  * Co-authors:
25  * Reto Grieder
26  *
27  */
28 
40 #ifndef _Shell_H__
41 #define _Shell_H__
42 
43 #include "core/CorePrereqs.h"
44 
45 #include <list>
46 #include <sstream>
47 #include <string>
48 #include <vector>
49 
50 #include "util/output/BaseWriter.h"
51 #include "core/CoreConfig.h"
52 
53 namespace orxonox
54 {
59  {
60  friend class Shell;
61 
62  public:
63  ShellListener() = default;
64  virtual ~ShellListener() = default;
65 
66  private:
67  virtual void linesChanged() {}
68  virtual void lineAdded() {}
69  virtual void inputChanged() {}
70  virtual void cursorChanged() {}
71  virtual void executed() {}
72  virtual void exit() {}
73  };
74 
75 
87  {
88  public:
90  enum class LineType
91  {
92  DebugOutput = debug_output,
93  Message = message,
94  UserError = user_error,
95  UserWarning = user_warning,
96  UserStatus = user_status,
97  UserInfo = user_info,
98  InternalError = internal_error,
99  InternalWarning = internal_warning,
100  InternalStatus = internal_status,
101  InternalInfo = internal_info,
102  Verbose = verbose,
103  VerboseMore = verbose_more,
104  VerboseUltra = verbose_ultra,
105  Cout,
106  Input,
107  Command,
108  Result,
109  Hint
110  };
111 
112  Shell(const std::string& consoleName = "", bool bScrollable = true);
113  ~Shell();
114 
115  void setConfigValues();
116  void commandHistoryOffsetChanged();
117  void commandHistoryLengthChanged();
118 
119  void registerListener(ShellListener* listener);
120  void unregisterListener(ShellListener* listener);
121 
124  { return this->inputBuffer_; }
125 
126  void setCursorPosition(unsigned int cursor);
127  unsigned int getCursorPosition() const;
128 
129  const std::string& getInput() const;
130 
131  typedef std::list<std::pair<std::string, LineType>> LineList;
132  LineList::const_iterator getNewestLineIterator() const;
133  LineList::const_iterator getEndIterator() const;
134 
135  void addOutput(const std::string& text, LineType type = LineType::DebugOutput);
136  void addLine(const std::string& line, LineType type = LineType::DebugOutput);
137  void clearOutput();
138 
140  inline unsigned int getNumLines() const
141  { return this->outputLines_.size(); }
143  inline unsigned int getScrollPosition() const
144  { return this->scrollPosition_; }
145 
147  static inline unsigned int getCacheSize()
148  { return Shell::cacheSize_s; }
149 
150  private:
151  // non-copyable:
152  Shell(const Shell&) = delete;
153  Shell& operator=(const Shell&) = delete;
154 
155  // DevModeListener
156  virtual void devModeChanged(bool value) override;
157 
158  void addToHistory(const std::string& command);
159  const std::string& getFromHistory() const;
160  void clearInput();
161  // BaseWriter
162  virtual void printLine(const std::string& line, OutputLevel level) override;
163 
164  void configureInputBuffer();
165 
166  // InputBuffer callbacks
167  void inputChanged();
168  void execute();
169  void hintAndComplete();
170  void backspace();
171  void deleteChar();
172  void cursorRight();
173  void cursorLeft();
174  void cursorEnd();
175  void cursorHome();
176  void historyUp();
177  void historyDown();
178  void historySearchUp();
179  void historySearchDown();
180  void scrollUp();
181  void scrollDown();
182  void exit();
183 
185  template <void (ShellListener::*F)()>
187  {
188  for (std::list<ShellListener*>::const_iterator it = this->listeners_.begin(); it != this->listeners_.end(); )
189  ((*(it++))->*F)();
190  }
191 
192  std::list<ShellListener*> listeners_;
194  LineList outputLines_;
195  LineList::const_iterator scrollIterator_;
196  unsigned int scrollPosition_;
197  unsigned int historyPosition_;
198  const bool bScrollable_;
199 
200  // Config values
201  unsigned int maxHistoryLength_;
202  unsigned int historyOffset_;
203  std::vector<std::string> commandHistory_;
204  static unsigned int cacheSize_s;
205  };
206 }
207 
208 #endif /* _Shell_H__ */
unsigned int historyPosition_
If the user scrolls through the history of entered commands (stored in commandHistory_), this contains the currently viewed history entry.
Definition: Shell.h:197
Output level, used to notify the user about the program&#39;s state.
Definition: OutputDefinitions.h:93
static unsigned int cacheSize_s
The maximum cache size of the CommandExecutor - this is stored here for better readability of the con...
Definition: Shell.h:204
LineList::const_iterator scrollIterator_
An iterator to an entry of the list of output-lines, changes if the user scrolls through the output i...
Definition: Shell.h:195
unsigned int scrollPosition_
The number of the line that is currently being referenced by scrollIterator_.
Definition: Shell.h:196
std::list< ShellListener * > listeners_
The registered shell listeners.
Definition: Shell.h:192
std::vector< std::string > commandHistory_
The history of commands that were entered by the user.
Definition: Shell.h:203
std::list< std::pair< std::string, LineType > > LineList
Definition: Shell.h:131
virtual void linesChanged()
Called if all output-lines have changed.
Definition: Shell.h:67
virtual void cursorChanged()
Called if the cursor in the input line has changed.
Definition: Shell.h:70
Shared library macros, enums, constants and forward declarations for the core library ...
::std::string string
Definition: gtest-port.h:756
Output level, usually not visible, used for unimportant debug information.
Definition: OutputDefinitions.h:99
Output level, used for warnings which are important for developers.
Definition: OutputDefinitions.h:96
InputBuffer * getInputBuffer()
Returns the input buffer which is needed by the user to enter text into the shell.
Definition: Shell.h:123
virtual void exit()
Called if the console should be closed.
Definition: Shell.h:72
Informs about changes in the Development Mode.
Definition: CoreConfig.h:74
Output level, used for messages directed to the user (e.g. "Press any key to continue") ...
Definition: OutputDefinitions.h:89
The Shell is the logical component of the console that displays output to the user and allows him to ...
Definition: Shell.h:86
Output level, usually not visible, used for unimportant debug information (even less important than v...
Definition: OutputDefinitions.h:101
OutputLevel
Output levels define type and importance of an output message.
Definition: OutputDefinitions.h:84
Output level, used to log the program&#39;s internal state in the log file.
Definition: OutputDefinitions.h:97
Output level, used for error messages which are important for developers.
Definition: OutputDefinitions.h:95
Definition: InputPrereqs.h:91
static unsigned int getCacheSize()
Returns the cache size that is actually used in CommandExecutor, but placed here for better readabili...
Definition: Shell.h:147
Output level, used for error messages which are important for the user.
Definition: OutputDefinitions.h:91
virtual void inputChanged()
Called if the input has changed.
Definition: Shell.h:69
unsigned int maxHistoryLength_
The maximum number of saved commands.
Definition: Shell.h:201
Output level, used for temporary debug output while writing code.
Definition: OutputDefinitions.h:90
Die Wagnis Klasse hat die folgenden Aufgaben:
Definition: ApplicationPaths.cc:66
ARGUMENT_COMPLETION_FUNCTION_IMPLEMENTATION_MULTI() command(const std::string &fragment)
Returns a list of commands and groups and also supports auto-completion of the arguments of these com...
Definition: ArgumentCompletionFunctions.cc:178
void updateListeners()
Iterates through all registered shell listeners and calls the function F.
Definition: Shell.h:186
#define _CoreExport
Definition: CorePrereqs.h:61
const bool bScrollable_
If true, the user can scroll through the output-lines.
Definition: Shell.h:198
Declaration of the BaseWriter class, the base of all output writers.
unsigned int getScrollPosition() const
Returns the line which is currently viewed if the user scrolls through the older output-lines in the ...
Definition: Shell.h:143
Output level, usually not visible, used for unimportant debug information (less important than verbos...
Definition: OutputDefinitions.h:100
BaseWriter is an output listener and makes the accepted output levels and contexts configurable...
Definition: BaseWriter.h:65
unsigned int getNumLines() const
Returns the number of output-lines that are displayed in the shell.
Definition: Shell.h:140
InputBuffer * inputBuffer_
The input buffer that is needed by the user to enter text.
Definition: Shell.h:193
LineType
Defines the type of a line of text in the Shell - some types depend on the output level...
Definition: Shell.h:90
virtual void lineAdded()
Called if a new line was added to the output.
Definition: Shell.h:68
An interface, used to get a notification if the state of the Shell changes.
Definition: Shell.h:58
Definition: InputBuffer.h:76
Output level, used to log information about the program&#39;s progress in the log file.
Definition: OutputDefinitions.h:98
Output level, used for warnings which are important for the user.
Definition: OutputDefinitions.h:92
unsigned int historyOffset_
The command history is a circular buffer, this variable defines the current write-offset.
Definition: Shell.h:202
virtual void executed()
Called if a command from the input line was executed.
Definition: Shell.h:71
LineList outputLines_
A list of all output-lines that were displayed in the shell so far.
Definition: Shell.h:194
Output level, used to provide the user with additional progress information.
Definition: OutputDefinitions.h:94