Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
--[[
Usage:
When running wesnoth, type:
:lua shell()
This will open the shell. Type any lua code you like in the shell. Variables will be stored in a shell-specific environment. The shell defines some additional functions:
clear() :: clears the output area.
close() :: closes the shell.
]]
local helper = wesnoth.require "lua/helper.lua"
local T = helper.set_wml_tag_metatable {}
local _ = wesnoth.textdomain "wesnoth"
shell = {meta={}}
setmetatable(shell, shell.meta)
shell.output = nil
shell.input = nil
shell.prompt = "> "
shell.print = function (...)
local line = ""
for i, v in ipairs(arg) do
if line == "" then
line = tostring(v)
else
line = line .. "\t" .. tostring(v)
end
end
if shell.output == nil then
shell.output = tostring(line)
else
shell.output = shell.output .. "\n" .. tostring(line)
end
end
shell.env = {
print = shell.print,
clear = function()
shell.output = nil
end
}
setmetatable(shell.env, {__index = _G})
shell.history = {
index = nil,
next = function()
i = shell.history.index
if i == nil or i == 0 then return end
i = i - 1
shell.history.index = i
end,
prev = function()
i = shell.history.index
if i == nil and shell.history[0] == nil then
shell.history[0] = shell.input
end
i = (i or 0) + 1
if shell.history[i] == nil then return end
shell.history.index = i
end
}
local function postshow()
shell.input = wesnoth.get_dialog_value "shell"
end
local function preshow()
if shell.output ~= nil then
wesnoth.set_dialog_value(shell.output, "shell_output")
end
if shell.history.index ~= nil then
wesnoth.message("setting shell default value...")
wesnoth.message(tostring(shell.history.index))
wesnoth.message(tostring(shell.history[shell.history.index]))
wesnoth.set_dialog_value(shell.history[shell.history.index], "shell")
end
end
function shell:display()
repeat
local rval = wesnoth.show_dialog(shell.window, preshow, postshow)
if rval == -1 then
local input = shell.input
shell.input = nil
if input ~= "" then
table.insert(shell.history, 1, input)
shell.history[0] = nil
shell.history.index = nil
end
shell.print(string.format("%s%s", shell.prompt, input))
if input ~= "close()" and input ~= nil and input ~= "" then
local func, err = loadstring(input, "stdin")
if err then
shell.print(err)
else
setfenv(func, shell.env)
local success, rval = pcall(func)
if rval ~= nil then
shell.print(rval)
end
end
end
elseif rval == shell.rnext then
shell.history.next()
elseif rval == shell.rprev then
shell.history.prev()
end
until input == "close()" or rval == -2
end
shell.meta.__call = shell.display
shell.rnext = 1
shell.rprev = 2
shell.rhist = 3
shell.window = {
vertical_placement = "bottom",
T.tooltip { id = "tooltip_large" },
T.helptip { id = "tooltip_large" },
T.grid{
T.row{
T.column{
horizontal_alignment = "left",
T.scroll_label{
id = "shell_output",
}
}
},
T.row{
T.column{
horizontal_grow = true,
T.text_box{
id = "shell",
--history = "shell_history",
}
}
},
T.row{
T.column{
T.grid{
T.row{
T.column{
T.button{
id = "shell_next",
return_value = shell.rnext,
label = _ "Next"
}
},
T.column{
T.button{
id = "shell_prev",
return_value = shell.rprev,
label = _ "Previous"
}
},
T.column{
T.button{
id = "shell_view_history",
return_value = shell.rhist,
label = _ "History"
}
}
}
}
}
},
T.row{
T.column{
horizontal_alignment = "left",
T.label{
label = "Run lua or type close() to quit this shell.",
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.