35 _historySizeMax(historySize), _historySizeCurrent(0),
36 _historyIgnoreSpace(false), _historyIgnoreDups(false), _historyEraseDups(false),
37 _cursorPosition(0), _overwrite(false), _maxHintSize(0) {
67 std::pair<ArgumentSets::iterator, bool> result =
70 result.first->second.clear();
71 std::copy(arguments.begin(), arguments.end(),
72 std::inserter(result.first->second, result.first->second.end()));
76 ArgumentSets::iterator args =
_arguments.find(command);
134 SDL_Keycode key =
event.key.keysym.sym;
135 SDL_Keymod mod = (SDL_Keymod) (((
int) event.key.keysym.mod) & (KMOD_CTRL | KMOD_SHIFT | KMOD_ALT));
139 if ((key == SDLK_RETURN) || (key == SDLK_KP_ENTER)) {
146 if ((key == SDLK_BACKSPACE) && (mod == KMOD_NONE)) {
156 if ((key == SDLK_DELETE) || ((key == SDLK_d) && (mod & KMOD_CTRL))) {
164 if ((key == SDLK_u) && (mod & KMOD_CTRL)) {
172 if ((key == SDLK_k) && (mod & KMOD_CTRL)) {
179 if (key == SDLK_INSERT) {
185 if (((key == SDLK_LEFT) && (mod & KMOD_CTRL)) || ((key == SDLK_b) && (mod & KMOD_ALT))) {
191 if (((key == SDLK_RIGHT) && (mod & KMOD_CTRL)) || ((key == SDLK_f) && (mod & KMOD_ALT))) {
197 if ((key == SDLK_BACKSPACE) && (mod & KMOD_ALT)) {
207 if ((key == SDLK_d) && (mod & KMOD_ALT)) {
215 if ((key == SDLK_w) && (mod & KMOD_CTRL)) {
225 if ((key == SDLK_LEFT) || ((key == SDLK_b) && (mod & KMOD_CTRL))) {
232 if ((key == SDLK_RIGHT) || ((key == SDLK_f) && (mod & KMOD_CTRL))) {
239 if ((key == SDLK_HOME) || ((key == SDLK_a) && (mod & KMOD_CTRL))) {
245 if ((key == SDLK_END) || ((key == SDLK_e) && (mod & KMOD_CTRL))) {
251 if ((key == SDLK_UP) || ((key == SDLK_p) && (mod & KMOD_CTRL))) {
257 if ((key == SDLK_DOWN) || ((key == SDLK_n) && (mod & KMOD_CTRL))) {
263 if ((key == SDLK_LESS) && (mod & KMOD_ALT) && (mod & KMOD_SHIFT)) {
269 if ((key == SDLK_LESS) && (mod & KMOD_ALT)) {
275 if (key == SDLK_TAB) {
306 for (std::list<HistorySave>::iterator save =
_historySave.begin();
337 *save->position = save->line;
341 for (std::list<UString>::iterator h =
_history.begin(); h !=
_history.end(); ) {
350 bool shouldSave =
true;
469 ArgumentSets::iterator args =
_arguments.find(command);
473 tabComplete(command +
" ", arguments, args->second);
480 CommandSet::const_iterator lower = commands.lower_bound(input);
481 if (lower == commands.end())
488 std::list<UString> candidates;
489 for (CommandSet::const_iterator it = lower; it != commands.end(); ++it) {
490 if (!it->beginsWith(input))
494 candidates.push_back(*it);
495 maxSize =
MAX(maxSize, candidates.back().size());
500 if (candidates.empty())
504 if (&candidates.front() == &candidates.back()) {
518 std::copy(candidates.begin(), candidates.end(), std::back_inserter(
_completeHint));
522 if (substring != input) {
532 size_t minSize = strings.front().size();
535 std::list<UString::iterator> positions;
536 for (std::list<UString>::const_iterator s = strings.begin(); s != strings.end(); ++s) {
537 minSize =
MIN(minSize, s->size());
541 positions.push_back(s->begin());
546 while (minSize-- > 0) {
547 uint32 c = *positions.front();
550 std::list<UString::iterator>::iterator p;
551 for (p = positions.begin(); p != positions.end(); ++(*p), ++p)
size_t _historySizeCurrent
Current size of the history.
ReadLine(size_t historySize)
void insert(iterator pos, uint32 c)
Insert character c in front of this position.
bool _overwrite
Overwrite instead of insert?
std::list< UString >::iterator _historyPosition
The current browsing position within the history.
ArgumentSets _arguments
All know tab-completable command arguments.
A class holding an UTF-8 string.
bool _historyIgnoreSpace
Should we not remember input beginning with spaces?
iterator getPosition(size_t n) const
Convert a numerical position into an iterator.
size_t _cursorPosition
The current cursor position.
size_t findLastWordStart(bool onlySpace=false) const
iterator findFirst(uint32 c) const
void addInput(uint32 c)
Add that character to the current input.
std::set< UString > CommandSet
UString _currentLineBak
The backupped input line while we're browsing the history.
bool processKeyDown(const Events::Event &event, UString &command)
A class providing (limited) readline-like capabilities.
Keyboard key was pressed.
utf8::iterator< std::string::const_iterator > iterator
CommandSet _commands
All known tab-completable commands.
Utility templates and functions.
bool processEvent(const Events::Event &event, UString &command)
Process that given events.
const std::vector< UString > & getCompleteHint(size_t &maxSize) const
Return the current tab-completion hints.
The global events manager.
static bool isWordCharacter(uint32 c, bool onlySpace=false)
void historyEraseDups(bool eraseDups)
Erase all lines matching the line to be saved.
UString::iterator getCurrentPosition() const
bool empty() const
Is the string empty?
void setArguments(const UString &command, const std::vector< UString > &arguments)
Set the tab-completable arguments for a command.
bool _historyEraseDups
Should we actively remove duplicate lines?
void historyIgnoreDups(bool ignoreDups)
Don't save lines matching the bottom of the history.
void replace(iterator pos, uint32 c)
Replace the character at this position with c.
std::list< UString > _history
The history of previous input lines.
size_t _historySizeMax
Max size of the history.
#define EventMan
Shortcut for accessing the events manager.
void historyIgnoreSpace(bool ignoreSpace)
Don't save lines starting with a space.
void addCommand(const UString &command)
Add a command that can be tab-completed.
size_t getCursorPosition() const
Return the current cursor position within the input line.
bool getOverwrite() const
Return whether we're current in overwrite mode.
void clearHistory()
Clear the input history.
static bool isASCII(uint32 c)
Is the character an ASCII character?
size_t size() const
Return the size of the string, in characters.
void addCurrentLineToHistory()
size_t _maxHintSize
Max size of a current command candidates.
void erase(iterator from, iterator to)
Erase the character within this range.
std::vector< UString > _completeHint
Current possible command candidates for the input line.
void split(iterator splitPoint, UString &left, UString &right, bool remove=false) const
std::list< HistorySave > _historySave
Saved copies of modified history lines.
bool _historyIgnoreDups
Should we not remember duplicate lines?
size_t findNextWordEnd(bool onlySpace=false) const
static UString findCommonSubstring(const std::list< UString > &strings)
void clear()
Clear the string's contents.
UString _currentLine
The current input line.
std::list< HistorySave >::iterator findHistorySave()
static bool isAlNum(uint32 c)
Is the character an ASCII alphanumeric character?
bool processTextInput(const Events::Event &event, UString &command)
const UString & getCurrentLine() const
Return the current input line.