Skip to content

Commit

Permalink
Merge pull request #97 from MafiaHub/improvement/console-history
Browse files Browse the repository at this point in the history
console history
  • Loading branch information
zpl-zak committed Jan 29, 2024
2 parents d402194 + aa39641 commit 05245c2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
36 changes: 35 additions & 1 deletion code/framework/src/external/imgui/widgets/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,42 @@ namespace Framework::External::ImGUI::Widgets {
ImGui::SetKeyboardFocusHere(-1);
_focusOnInput = true;
}
else if (data->EventFlag == ImGuiInputTextFlags_CallbackHistory && data->EventKey == ImGuiKey_UpArrow) {
if (_history.empty())
return 0;
if (_historyPos == -1) {
_tempInputText = data->Buf;
}
else if (_historyPos + 1 == _history.size()) {
return 0;
}
++_historyPos;
data->DeleteChars(0, data->BufTextLen);
data->InsertChars(0, _history.at(_historyPos).c_str());

ImGui::SetKeyboardFocusHere(-1);
_focusOnInput = true;
}
else if (data->EventFlag == ImGuiInputTextFlags_CallbackHistory && data->EventKey == ImGuiKey_DownArrow) {
if (_historyPos > 0) {
--_historyPos;
data->DeleteChars(0, data->BufTextLen);
data->InsertChars(0, _history.at(_historyPos).c_str());
}
else if (!_tempInputText.empty()) {
_historyPos = -1;
data->DeleteChars(0, data->BufTextLen);
data->InsertChars(0, _tempInputText.c_str());
_tempInputText = "";
}

ImGui::SetKeyboardFocusHere(-1);
_focusOnInput = true;
}
return 0;
};

auto flags = ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_CallbackCompletion | ImGuiInputTextFlags_CallbackResize;
auto flags = ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_CallbackCompletion | ImGuiInputTextFlags_CallbackResize | ImGuiInputTextFlags_CallbackHistory;

if (!_consoleControl) {
// Block input if console is unfocused
Expand All @@ -180,6 +212,8 @@ namespace Framework::External::ImGUI::Widgets {
}

if (wasInputProcessed && !_updateInputText) {
_history.emplace(_history.begin(), consoleText);
_historyPos = -1;
SendCommand(consoleText);
consoleText[0] = '\0';
ImGui::SetKeyboardFocusHere(-1);
Expand Down
3 changes: 3 additions & 0 deletions code/framework/src/external/imgui/widgets/console.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ namespace Framework::External::ImGUI::Widgets {
std::shared_ptr<Framework::Utils::CommandProcessor> _commandProcessor;
std::shared_ptr<Framework::Input::IInput> _input;
std::vector<MenuBarProc> _menuBarDrawers;
std::vector<std::string> _history;
std::string _tempInputText;
int _historyPos = -1;
spdlog::logger *_logger;
static void FormatLog(std::string log);
void SendCommand(const std::string &command);
Expand Down

0 comments on commit 05245c2

Please sign in to comment.