Skip to content

Commit

Permalink
Lua console: Add a prelude file
Browse files Browse the repository at this point in the history
Runs lua/repl_prelude.lua at console initialization.

The default prelude contains global assignments for all devilutionx
modules. This should save us on typing.
  • Loading branch information
glebm committed Nov 3, 2023
1 parent 03893ea commit 4a7fa5d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
1 change: 1 addition & 0 deletions CMake/Assets.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ set(devilutionx_assets
levels/towndata/automap.amp
lua/init.lua
lua/inspect.lua
lua/repl_prelude.lua
nlevels/cutl5w.clx
nlevels/cutl6w.clx
nlevels/l5data/cornerstone.dun
Expand Down
5 changes: 5 additions & 0 deletions Packaging/resources/assets/lua/repl_prelude.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
log = require('devilutionx.log')
audio = require('devilutionx.audio')
render = require('devilutionx.render')
message = require('devilutionx.message')
inspect = require('inspect')
27 changes: 25 additions & 2 deletions Source/panels/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "DiabloUI/text_input.hpp"
#include "control.h"
#include "engine.h"
#include "engine/assets.hpp"
#include "engine/displacement.hpp"
#include "engine/dx.h"
#include "engine/palette.h"
Expand All @@ -39,6 +40,7 @@ constexpr std::string_view HelpText =
" Up/Down to fill the input from history,"
" Shift+Up/Down to fill the input from output history,"
" Ctrl+L to clear history, Esc to close.";
std::optional<tl::expected<AssetData, std::string>> ConsolePrelude;

bool IsConsoleVisible;
char ConsoleInputBuffer[4096];
Expand Down Expand Up @@ -313,12 +315,33 @@ void NextOutput()
NextHistoryItem(IsHistoryOutputLine);
}

void AddInitialConsoleLines()
{
if (ConsolePrelude->has_value()) {
std::string_view prelude { **ConsolePrelude };
if (!prelude.empty() && prelude.back() == '\n')
prelude.remove_suffix(1);
AddConsoleLine(ConsoleLine { .type = ConsoleLine::Help, .text = StrCat(HelpText, "\n", prelude) });
} else {
AddConsoleLine(ConsoleLine { .type = ConsoleLine::Help, .text = std::string(HelpText) });
AddConsoleLine(ConsoleLine { .type = ConsoleLine::Error, .text = ConsolePrelude->error() });
}
}

void ClearConsole()
{
ConsoleLines.clear();
HistoryIndex = -1;
ScrollOffset = 0;
AddConsoleLine(ConsoleLine { .type = ConsoleLine::Help, .text = std::string(HelpText) });
AddInitialConsoleLines();
}

void InitConsole()
{
ConsolePrelude = LoadAsset("lua\\repl_prelude.lua");
AddInitialConsoleLines();
if (ConsolePrelude->has_value())
RunLuaReplLine(std::string_view(**ConsolePrelude));
}

} // namespace
Expand Down Expand Up @@ -442,7 +465,7 @@ void DrawConsole(const Surface &out)
SDL_StartTextInput();
FirstRender = false;
if (ConsoleLines.empty()) {
AddConsoleLine(ConsoleLine { .type = ConsoleLine::Help, .text = std::string(HelpText) });
InitConsole();
}
}

Expand Down

0 comments on commit 4a7fa5d

Please sign in to comment.