xoreos  0.0.5
readline.h
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #ifndef COMMON_READLINE_H
26 #define COMMON_READLINE_H
27 
28 #include <vector>
29 #include <list>
30 #include <set>
31 #include <map>
32 
33 #include "src/common/types.h"
34 #include "src/common/ustring.h"
35 
36 #include "src/events/types.h"
37 
38 namespace Common {
39 
40 class ReadLine {
41 public:
42  ReadLine(size_t historySize);
43  ~ReadLine();
44 
46  void historyIgnoreSpace(bool ignoreSpace);
48  void historyIgnoreDups (bool ignoreDups);
50  void historyEraseDups (bool eraseDups);
51 
53  void clearHistory();
54 
56  void addCommand(const UString &command);
57 
59  void setArguments(const UString &command, const std::vector<UString> &arguments);
61  void setArguments(const UString &command);
62 
64  const UString &getCurrentLine() const;
65 
67  size_t getCursorPosition() const;
68 
70  bool getOverwrite() const;
71 
73  const std::vector<UString> &getCompleteHint(size_t &maxSize) const;
74 
76  void addInput(uint32 c);
78  void addInput(const UString &str);
79 
89  bool processEvent(const Events::Event &event, UString &command);
90 
91 
92 private:
93  struct HistorySave {
94  std::list<UString>::iterator position;
96  };
97 
98  typedef std::set<UString> CommandSet;
99  typedef std::map<UString, CommandSet> ArgumentSets;
100 
101 
104 
108 
110 
111  bool _overwrite;
112 
115 
117  std::list<UString> _history;
119  std::list<UString>::iterator _historyPosition;
120 
122  std::list<HistorySave> _historySave;
123 
128 
130  std::vector<UString> _completeHint;
132  size_t _maxHintSize;
133 
134 
135  std::list<HistorySave>::iterator findHistorySave();
136 
138 
139  void updateHistory();
140 
141  void browseUp();
142  void browseDown();
143  void browseTop();
144  void browseBottom();
145 
146  bool processKeyDown(const Events::Event &event, UString &command);
147  bool processTextInput(const Events::Event &event, UString &command);
148 
149  void tabComplete();
150  void tabComplete(const UString &prefix, const UString &input,
151  const CommandSet &commands);
152 
154 
155  size_t findLastWordStart(bool onlySpace = false) const;
156  size_t findNextWordEnd(bool onlySpace = false) const;
157 
158  static bool isWordCharacter(uint32 c, bool onlySpace = false);
159  static UString findCommonSubstring(const std::list<UString> &strings);
160 };
161 
162 } // End of namespace Common
163 
164 #endif // COMMON_READLINE_H
std::list< UString >::iterator position
Definition: readline.h:94
size_t _historySizeCurrent
Current size of the history.
Definition: readline.h:103
Basic event types.
ReadLine(size_t historySize)
Definition: readline.cpp:34
void updateHistory()
Definition: readline.cpp:314
bool _overwrite
Overwrite instead of insert?
Definition: readline.h:111
Definition: 2dafile.h:39
std::list< UString >::iterator _historyPosition
The current browsing position within the history.
Definition: readline.h:119
ArgumentSets _arguments
All know tab-completable command arguments.
Definition: readline.h:127
A class holding an UTF-8 string.
Definition: ustring.h:48
bool _historyIgnoreSpace
Should we not remember input beginning with spaces?
Definition: readline.h:105
size_t _cursorPosition
The current cursor position.
Definition: readline.h:109
size_t findLastWordStart(bool onlySpace=false) const
Definition: readline.cpp:568
void addInput(uint32 c)
Add that character to the current input.
Definition: readline.cpp:99
std::set< UString > CommandSet
Definition: readline.h:98
SDL_Event Event
Definition: types.h:42
UString _currentLineBak
The backupped input line while we&#39;re browsing the history.
Definition: readline.h:114
bool processKeyDown(const Events::Event &event, UString &command)
Definition: readline.cpp:132
utf8::iterator< std::string::const_iterator > iterator
Definition: ustring.h:50
CommandSet _commands
All known tab-completable commands.
Definition: readline.h:125
bool processEvent(const Events::Event &event, UString &command)
Process that given events.
Definition: readline.cpp:118
const std::vector< UString > & getCompleteHint(size_t &maxSize) const
Return the current tab-completion hints.
Definition: readline.cpp:93
static bool isWordCharacter(uint32 c, bool onlySpace=false)
Definition: readline.cpp:561
void historyEraseDups(bool eraseDups)
Erase all lines matching the line to be saved.
Definition: readline.cpp:53
UString::iterator getCurrentPosition() const
Definition: readline.cpp:301
void setArguments(const UString &command, const std::vector< UString > &arguments)
Set the tab-completable arguments for a command.
Definition: readline.cpp:66
Low-level type definitions to handle fixed width types portably.
bool _historyEraseDups
Should we actively remove duplicate lines?
Definition: readline.h:107
void historyIgnoreDups(bool ignoreDups)
Don&#39;t save lines matching the bottom of the history.
Definition: readline.cpp:49
std::list< UString > _history
The history of previous input lines.
Definition: readline.h:117
size_t _historySizeMax
Max size of the history.
Definition: readline.h:102
void tabComplete()
Definition: readline.cpp:457
void historyIgnoreSpace(bool ignoreSpace)
Don&#39;t save lines starting with a space.
Definition: readline.cpp:45
void addCommand(const UString &command)
Add a command that can be tab-completed.
Definition: readline.cpp:62
size_t getCursorPosition() const
Return the current cursor position within the input line.
Definition: readline.cpp:85
Unicode string handling.
bool getOverwrite() const
Return whether we&#39;re current in overwrite mode.
Definition: readline.cpp:89
void clearHistory()
Clear the input history.
Definition: readline.cpp:57
uint32_t uint32
Definition: types.h:204
void addCurrentLineToHistory()
Definition: readline.cpp:330
size_t _maxHintSize
Max size of a current command candidates.
Definition: readline.h:132
void browseBottom()
Definition: readline.cpp:443
std::vector< UString > _completeHint
Current possible command candidates for the input line.
Definition: readline.h:130
std::list< HistorySave > _historySave
Saved copies of modified history lines.
Definition: readline.h:122
bool _historyIgnoreDups
Should we not remember duplicate lines?
Definition: readline.h:106
size_t findNextWordEnd(bool onlySpace=false) const
Definition: readline.cpp:590
static UString findCommonSubstring(const std::list< UString > &strings)
Definition: readline.cpp:528
std::map< UString, CommandSet > ArgumentSets
Definition: readline.h:99
UString _currentLine
The current input line.
Definition: readline.h:113
std::list< HistorySave >::iterator findHistorySave()
Definition: readline.cpp:305
bool processTextInput(const Events::Event &event, UString &command)
Definition: readline.cpp:284
const UString & getCurrentLine() const
Return the current input line.
Definition: readline.cpp:81