Skip to content

Commit

Permalink
Added console history
Browse files Browse the repository at this point in the history
  • Loading branch information
maximegmd committed Feb 12, 2021
1 parent 0612276 commit 40fb9e6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
43 changes: 42 additions & 1 deletion src/overlay/widgets/Console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,42 @@ void Console::OnDisable()
{
}

int Console::HandleConsoleHistory(ImGuiInputTextCallbackData* apData)
{
auto* pConsole = static_cast<Console*>(apData->UserData);

std::string* pStr = nullptr;

if (pConsole->m_newConsoleHistory)
{
pStr = &pConsole->m_consoleHistory[pConsole->m_consoleHistoryIndex];
}
else if (apData->EventKey == ImGuiKey_UpArrow && pConsole->m_consoleHistoryIndex > 0)
{
pConsole->m_consoleHistoryIndex--;

pStr = &pConsole->m_consoleHistory[pConsole->m_consoleHistoryIndex];
}
else if (apData->EventKey == ImGuiKey_DownArrow && pConsole->m_consoleHistoryIndex + 1 < pConsole->m_consoleHistory.size())
{
pConsole->m_consoleHistoryIndex++;

pStr = &pConsole->m_consoleHistory[pConsole->m_consoleHistoryIndex];
}

pConsole->m_newConsoleHistory = false;

if (pStr)
{
std::memcpy(apData->Buf, pStr->c_str(), pStr->size());
apData->BufDirty = true;
apData->BufTextLen = pStr->size();
apData->CursorPos = apData->BufTextLen;
}

return 0;
}

void Console::Update()
{
ImGui::Checkbox("Clear Input", &m_inputClear);
Expand Down Expand Up @@ -82,13 +118,18 @@ void Console::Update()
m_focusConsoleInput = false;
}
ImGui::PushItemWidth(-1);
const auto execute = ImGui::InputText("##InputCommand", m_Command, std::size(m_Command), ImGuiInputTextFlags_EnterReturnsTrue);
const auto execute = ImGui::InputText("##InputCommand", m_Command, std::size(m_Command),
ImGuiInputTextFlags_EnterReturnsTrue | ImGuiInputTextFlags_CallbackHistory, &HandleConsoleHistory, this);
ImGui::PopItemWidth();
ImGui::SetItemDefaultFocus();
if (execute)
{
auto consoleLogger = spdlog::get("scripting");
consoleLogger->info("> {}", m_Command);

m_consoleHistoryIndex = m_consoleHistory.size();
m_consoleHistory.push_back(m_Command);
m_newConsoleHistory = true;

if (!m_vm.ExecuteLua(m_Command))
consoleLogger->info("Command failed to execute!");
Expand Down
6 changes: 6 additions & 0 deletions src/overlay/widgets/Console.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ struct Console : Widget
bool GameLogEnabled() const;

private:

static int HandleConsoleHistory(ImGuiInputTextCallbackData* apData);

std::recursive_mutex m_outputLock{ };
std::vector<std::string> m_outputLines{ };
std::vector<std::string> m_consoleHistory{};
int64_t m_consoleHistoryIndex{ 0 };
bool m_newConsoleHistory{true};
bool m_outputShouldScroll{ true };
bool m_outputScroll{ false };
bool m_inputClear{ true };
Expand Down

0 comments on commit 40fb9e6

Please sign in to comment.