add commands to terminal

This commit is contained in:
unknown 2022-02-12 21:56:17 -05:00
parent d0a4ace94e
commit ef2ceed755
7 changed files with 66 additions and 2 deletions

16
computers/commands.lua Normal file
View File

@ -0,0 +1,16 @@
computers.registered_commands = {}
function computers.register_command(modpath)
local func = dofile(modpath .. "/init.lua")
local f = io.open(modpath .. "/conf.json")
local conf = minetest.parse_json(f:read("*all"))
f:close()
if func and computers.os.version >= conf.engine then computers.registered_commands[conf.name] = func end
end
--load default commands
local dirs = minetest.get_dir_list(computers.modpath .. "/commands", true)
for _, dir in pairs(dirs) do
computers.register_command(computers.modpath .. "/commands/" .. dir)
end

View File

@ -0,0 +1,6 @@
{
"name": "echo",
"license": "MIT",
"version": 0.1,
"engine": 0.3
}

View File

@ -0,0 +1,5 @@
function echo(input)
return input
end
return echo

View File

@ -0,0 +1,6 @@
{
"name": "test",
"license": "MIT",
"version": 0.1,
"engine": 100000000
}

View File

@ -0,0 +1,6 @@
function test()
--minetest.chat_send_all("test")
return 0
end
return test

View File

@ -122,16 +122,32 @@ function computers.load_gui(pos, node, clicker)
border = false,
},
on_event = function(form, player, element, value)
minetest.chat_send_all("value: " .. value)
local cindex = futil.get_index_by_name(form, "terminal_ctn")
local eindex = futil.get_index_by_name(form[cindex], "terminal_output")
local text = form[cindex][eindex].default
form[cindex][eindex].default = text .. "user:~$ " .. value .. "\n"
--form[cindex][eindex].default = text .. "user:~$ " .. value .. "\n"
if value == "clear" then
form[cindex][eindex].default = "user:~$\n"
end
local cdata = value:split(" ", false, 1)
if computers.registered_commands[cdata[1]] then
form[cindex][eindex].default = text .. "user:~$ " .. cdata[1] .. "\n"
text = form[cindex][eindex].default
local output = computers.registered_commands[cdata[1]](cdata[2])
if output and output ~= "" and type(output) == "string" then
form[cindex][eindex].default = text .. output .. "\n"
end
elseif value == "clear" then
form[cindex][eindex].default = "user:~$\n"
elseif value == "" then
form[cindex][eindex].default = text .. "user:~$\n"
else
form[cindex][eindex].default = text .. "user:~$ " .. value .. "\nERROR: command not found\n"
end
return form
end,
},

View File

@ -1,7 +1,16 @@
computers = {}
computers.modpath = minetest.get_modpath("computers")
computers.os = {
version = 0.3,
name = "kuto",
authors = {"wsor", "luk3yx"},
license = "MIT",
}
dofile(computers.modpath .. "/old_stuff/init.lua")
dofile(computers.modpath .. "/formspec.lua")
dofile(computers.modpath .. "/commands.lua")
dofile(computers.modpath .. "/gui.lua")
dofile(computers.modpath .. "/demo.lua")