From c7cc137da9d4faa32272d11eee15270e88c97dab Mon Sep 17 00:00:00 2001 From: Alexander Bock Date: Sun, 3 Jan 2016 11:27:50 -0500 Subject: [PATCH] Started cleanup of LuaConsole --- ext/ghoul | 2 +- include/openspace/interaction/luaconsole.h | 1 - src/interaction/luaconsole.cpp | 80 ++++++++++++---------- 3 files changed, 45 insertions(+), 38 deletions(-) diff --git a/ext/ghoul b/ext/ghoul index badf68dbce..cad2dc26ae 160000 --- a/ext/ghoul +++ b/ext/ghoul @@ -1 +1 @@ -Subproject commit badf68dbcef1c19c9e1ca33d0b4d2e4e8e161ea4 +Subproject commit cad2dc26ae987f8bf1c5264c7fbc8591d8ab4ff1 diff --git a/include/openspace/interaction/luaconsole.h b/include/openspace/interaction/luaconsole.h index b4c3d0406d..97cc665033 100644 --- a/include/openspace/interaction/luaconsole.h +++ b/include/openspace/interaction/luaconsole.h @@ -37,7 +37,6 @@ namespace openspace { class LuaConsole { public: LuaConsole(); - ~LuaConsole(); void initialize(); void deinitialize(); diff --git a/src/interaction/luaconsole.cpp b/src/interaction/luaconsole.cpp index 0cfdd6e6d2..e93ae7a445 100644 --- a/src/interaction/luaconsole.cpp +++ b/src/interaction/luaconsole.cpp @@ -23,6 +23,7 @@ ****************************************************************************************/ #include + #include #include @@ -59,31 +60,26 @@ LuaConsole::LuaConsole() , _autoCompleteInfo({NoAutoComplete, false, ""}) , _isVisible(false) { - _commands.push_back(""); - _activeCommand = _commands.size() - 1; +// _commands.push_back(""); +// _activeCommand = _commands.size() - 1; } -LuaConsole::~LuaConsole() { - -} - - void LuaConsole::initialize() { _filename = FileSys.cacheManager()->cachedFilename(historyFile, "", true); - std::ifstream file(absPath(_filename), std::ios::binary | std::ios::in); + std::ifstream file(_filename, std::ios::binary | std::ios::in); if (file.good()) { int64_t nCommands; file.read(reinterpret_cast(&nCommands), sizeof(int64_t)); + std::vector tmp; for (int64_t i = 0; i < nCommands; ++i) { int64_t length; file.read(reinterpret_cast(&length), sizeof(int64_t)); - char* tmp = new char[length + 1]; - file.read(tmp, sizeof(char)*length); + tmp.resize(length + 1); + file.read(tmp.data(), length); tmp[length] = '\0'; - _commandsHistory.emplace_back(tmp); - delete[] tmp; + _commandsHistory.emplace_back(std::string(tmp.begin(), tmp.end())); } file.close(); _commands = _commandsHistory; @@ -93,7 +89,7 @@ void LuaConsole::initialize() { } void LuaConsole::deinitialize() { - std::ofstream file(absPath(_filename), std::ios::binary | std::ios::out); + std::ofstream file(_filename, std::ios::binary | std::ios::out); if (file.good()) { int64_t nCommands = _commandsHistory.size(); file.write(reinterpret_cast(&nCommands), sizeof(int64_t)); @@ -102,7 +98,6 @@ void LuaConsole::deinitialize() { file.write(reinterpret_cast(&length), sizeof(int64_t)); file.write(s.c_str(), length); } - file.close(); } } @@ -150,9 +145,11 @@ void LuaConsole::keyboardCallback(Key key, KeyModifier modifier, KeyAction actio } // Remove character after _inputPosition - if ((key == Key::Delete) && (_inputPosition <= _commands.at(_activeCommand).size())) - _commands.at(_activeCommand).erase(_inputPosition, 1); - + if (key == Key::Delete) { + if (_inputPosition <= _commands.at(_activeCommand).size()) + _commands.at(_activeCommand).erase(_inputPosition, 1); + } + // Go to the beginning of command string if (key == Key::Home) _inputPosition = 0; @@ -165,21 +162,19 @@ void LuaConsole::keyboardCallback(Key key, KeyModifier modifier, KeyAction actio // SHIFT+ENTER == new line if (modifierShift) addToCommand("\n"); - // CTRL+ENTER == Debug print the command - else if (modifierControl) { - LDEBUG("Active command from next line:\n" << _commands.at(_activeCommand)); - } // ENTER == run lua script else { - if (_commands.at(_activeCommand) != "") { - //OsEng.scriptEngine()->runScript(_commands.at(_activeCommand)); - OsEng.scriptEngine().queueScript(_commands.at(_activeCommand)); - if (!_commandsHistory.empty() && - _commands.at(_activeCommand) != _commandsHistory.at(_commandsHistory.size() - 1)) - _commandsHistory.push_back(_commands.at(_activeCommand)); - else if (_commandsHistory.empty()) + std::string cmd = _commands.at(_activeCommand); + if (cmd != "") { + OsEng.scriptEngine().queueScript(cmd); + + // Only add the current command to the history if it hasn't been + // executed before. We don't want two of the same commands in a row + if (_commandsHistory.empty() || (cmd != _commandsHistory.back())) _commandsHistory.push_back(_commands.at(_activeCommand)); } + + // Some clean up after the execution of the command _commands = _commandsHistory; _commands.push_back(""); _activeCommand = _commands.size() - 1; @@ -221,10 +216,27 @@ void LuaConsole::keyboardCallback(Key key, KeyModifier modifier, KeyAction actio // then check if we need to skip the first found values as the user has // pressed TAB repeatedly size_t fullLength = _autoCompleteInfo.initialValue.length(); - if (command.length() >= fullLength && - (command.substr(0, fullLength) == _autoCompleteInfo.initialValue) && - (i > _autoCompleteInfo.lastIndex)) - { + bool correctLength = command.length() >= fullLength; + + std::string commandLowerCase; + std::transform( + command.begin(), command.end(), + commandLowerCase.begin(), + ::tolower + ); + + std::string initialValueLowerCase; + std::transform( + _autoCompleteInfo.initialValue.begin(), + _autoCompleteInfo.initialValue.end(), + initialValueLowerCase.begin(), + ::tolower + ); + + bool correctCommand = + commandLowerCase.substr(0, fullLength) == initialValueLowerCase; + + if (correctLength && correctCommand && (i > _autoCompleteInfo.lastIndex)){ // We found our index, so store it _autoCompleteInfo.lastIndex = i; @@ -297,16 +309,12 @@ void LuaConsole::render() { const glm::vec4 green(0, 1, 0, 1); const glm::vec4 white(1, 1, 1, 1); std::shared_ptr font = OsEng.fontManager().font("Mono", font_size); -// const sgct_text::Font* font = sgct_text::FontManager::instance()->getFont(constants::fonts::keyMono, static_cast(font_size)); using ghoul::fontrendering::RenderFont; RenderFont(*font, glm::vec2(15.f, startY), red, "$"); RenderFont(*font, glm::vec2(15.f + font_size, startY), white, "%s", _commands.at(_activeCommand).c_str()); -// sgct_text::print(font, 15.0f, startY, red, "$"); -// sgct_text::print(font, 15.0f + font_size, startY, white, "%s", _commands.at(_activeCommand).c_str()); - size_t n = std::count(_commands.at(_activeCommand).begin(), _commands.at(_activeCommand).begin() + _inputPosition, '\n'); size_t p = _commands.at(_activeCommand).find_last_of('\n', _inputPosition); size_t linepos = _inputPosition;