Skip to content

Commit

Permalink
Improve Lua console behaviour (daid#2145)
Browse files Browse the repository at this point in the history
- Print input lines prepended with '> ' before running them.
- Store history to allow easily returning to previous inputs.
  • Loading branch information
GinjaNinja32 authored and Pithlit committed Oct 8, 2024
1 parent 01f2202 commit 31e6332
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/gui/gui2_textentry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ void GuiTextEntry::onTextInput(sp::TextInputEvent e)
break;
case sp::TextInputEvent::Up:
case sp::TextInputEvent::UpWithSelection:{
if (up_func)
{
up_func(text);
return;
}
int end_of_line = text.substr(0, selection_end).rfind("\n");
if (end_of_line < 0)
return;
Expand All @@ -182,6 +187,11 @@ void GuiTextEntry::onTextInput(sp::TextInputEvent e)
}break;
case sp::TextInputEvent::Down:
case sp::TextInputEvent::DownWithSelection:{
if (down_func)
{
down_func(text);
return;
}
int start_of_current_line = text.substr(0, selection_end).rfind("\n") + 1;
int end_of_current_line = text.find("\n", selection_end);
if (end_of_current_line < 0)
Expand Down Expand Up @@ -413,6 +423,18 @@ GuiTextEntry* GuiTextEntry::enterCallback(func_t func)
return this;
}

GuiTextEntry* GuiTextEntry::upCallback(func_t func)
{
this->up_func = func;
return this;
}

GuiTextEntry* GuiTextEntry::downCallback(func_t func)
{
this->down_func = func;
return this;
}

void GuiTextEntry::setCursorPosition(int offset)
{
selection_start = selection_end = std::clamp(offset, 0, int(text.size()));
Expand Down
4 changes: 4 additions & 0 deletions src/gui/gui2_textentry.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class GuiTextEntry : public GuiElement
const GuiThemeStyle* back_style;
func_t func;
func_t enter_func;
func_t up_func;
func_t down_func;

const float blink_rate = 0.530f;
sp::SystemTimer blink_timer;
Expand Down Expand Up @@ -52,6 +54,8 @@ class GuiTextEntry : public GuiElement
GuiTextEntry* setHidePassword(bool enabled=true);
GuiTextEntry* callback(func_t func);
GuiTextEntry* enterCallback(func_t func);
GuiTextEntry* upCallback(func_t func);
GuiTextEntry* downCallback(func_t func);

void setCursorPosition(int offset);
protected:
Expand Down
54 changes: 54 additions & 0 deletions src/menus/luaConsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,22 @@ LuaConsole::LuaConsole()
entry->enterCallback([this](string s) {
P<ScriptObject> script = engine->getObject("scenario");
if (script)
{
LuaConsole::addLog("> " + s);
script->runCode(s);
history.append(s);
entry->setText("");
}
});
entry->upCallback([this](string s) {
string text = history.movePrevious(s);
entry->setText(text);
entry->setCursorPosition(text.size());
});
entry->downCallback([this](string s) {
string text = history.moveNext(s);
entry->setText(text);
entry->setCursorPosition(text.size());
});

top->hide();
Expand Down Expand Up @@ -102,3 +117,42 @@ void LuaConsole::update(float delta)
}
}
}

string ConsoleHistory::movePrevious(string s) {
if (position == 0)
// beginning of history, nothing to go up to. keep the line the same.
return s;

if (position == entries.size())
// previous from a line not in history; set it pending so we can go back down to it later
pending = s;
else
entries[position] = s;

return entries[--position];
}

string ConsoleHistory::moveNext(string s) {
if (position == entries.size())
return s;

if (position + 1 == entries.size())
{
// end of history, nothing to do down to.
// if we had a pending entry, put it back
position++;
string wasPending = pending;
pending = "";
return wasPending;
}

entries[position] = s;
return entries[++position];
}

void ConsoleHistory::append(string s)
{
entries.push_back(s);
position = entries.size();
pending = "";
}
14 changes: 14 additions & 0 deletions src/menus/luaConsole.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@
#include "timer.h"


class ConsoleHistory
{
public:
string movePrevious(string);
string moveNext(string);
void append(string);

private:
std::vector<string> entries;
unsigned int position = 0;
string pending = "";
};

class GuiTextEntry;
class LuaConsole : public GuiCanvas, public Updatable
{
Expand All @@ -17,6 +30,7 @@ class LuaConsole : public GuiCanvas, public Updatable
void update(float delta) override;
private:
std::vector<string> log_messages;
ConsoleHistory history;
GuiElement* top;
GuiTextEntry* log;
GuiTextEntry* entry;
Expand Down

0 comments on commit 31e6332

Please sign in to comment.