add filesystem

This commit is contained in:
unknown 2022-02-13 00:31:20 -05:00
parent ef2ceed755
commit 4cfacb1abc
19 changed files with 148 additions and 16 deletions

View File

@ -1,11 +1,15 @@
computers.registered_commands = {}
computers.registered_confs = {}
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
if func and computers.os.version >= conf.engine then
computers.registered_commands[conf.name] = func
computers.registered_confs[conf.name] = conf
end
end
--load default commands

View File

@ -0,0 +1,7 @@
{
"name": "cd",
"license": "MIT",
"version": 0.1,
"engine": 0.4,
"help": "move in and out of folders"
}

View File

@ -0,0 +1,25 @@
function cd(pos, input, data)
local path = computers.devicepath .. "/" .. minetest.hash_node_position(pos) .. data.element.pwd
if input and input ~= "" and not input:find("/") then
if input == ".." then
local uri = data.element.pwd:split("/")
uri[#uri] = nil
data.element.pwd = "/" .. table.concat(uri, "/")
if data.element.pwd == "/" then data.element.pwd = "" end
return "sucess"
else
for _, folder in pairs(minetest.get_dir_list(path, true)) do
if folder == input then
data.element.pwd = data.element.pwd .. "/" .. input
return "sucess"
end
end
return "ERROR: path not found"
end
else
return "invalid or missing input"
end
end
return cd

View File

@ -2,5 +2,6 @@
"name": "echo",
"license": "MIT",
"version": 0.1,
"engine": 0.3
"engine": 0.3,
"help": "prints out input to the terminal"
}

View File

@ -1,4 +1,4 @@
function echo(input)
function echo(pos, input, data)
return input
end

View File

@ -0,0 +1,7 @@
{
"name": "help",
"license": "MIT",
"version": 0.1,
"engine": 0.4,
"help": "prints out help"
}

View File

@ -0,0 +1,9 @@
function help(pos, input, data)
local output = ""
for command, def in pairs(computers.registered_confs) do
output = output .. "\n" .. command .. ": " .. def.help
end
return output
end
return help

View File

@ -0,0 +1,7 @@
{
"name": "ls",
"license": "MIT",
"version": 0.1,
"engine": 0.4,
"help": "lists out all files and folders in the current path"
}

View File

@ -0,0 +1,9 @@
function ls(pos, input, data)
local path = computers.devicepath .. "/" .. minetest.hash_node_position(pos) .. data.element.pwd
--make dir to be safe
minetest.mkdir(path)
return table.concat(minetest.get_dir_list(path), " ")
end
return ls

View File

@ -0,0 +1,7 @@
{
"name": "mkdir",
"license": "MIT",
"version": 0.1,
"engine": 0.4,
"help": "makes a dir in the current path"
}

View File

@ -0,0 +1,12 @@
function mkdir(pos, input, data)
local path = computers.devicepath .. "/" .. minetest.hash_node_position(pos) .. data.element.pwd
if input and input ~= "" and not input:find("/") then
minetest.mkdir(path .. "/" .. input)
return "folder " .. input .. " created"
else
return "invalid or missing input"
end
end
return mkdir

View File

@ -0,0 +1,7 @@
{
"name": "pwd",
"license": "MIT",
"version": 0.1,
"engine": 0.4,
"help": "list the current path"
}

View File

@ -0,0 +1,7 @@
function pwd(pos, input, data)
local output = data.element.pwd
if output == "" then output = "/" end
return output
end
return pwd

View File

@ -2,5 +2,6 @@
"name": "test",
"license": "MIT",
"version": 0.1,
"engine": 100000000
"engine": 100000000,
"help": "literally worthless"
}

View File

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

View File

@ -0,0 +1,7 @@
{
"name": "touch",
"license": "MIT",
"version": 0.1,
"engine": 0.4,
"help": "creates a file in the current path"
}

View File

@ -0,0 +1,14 @@
function touch(pos, input, data)
local path = computers.devicepath .. "/" .. minetest.hash_node_position(pos) .. data.element.pwd
--make dir to be safe
minetest.mkdir(path)
if input and input ~= "" and not input:find("/") then
minetest.safe_file_write(path .. "/" .. input, "")
return "file " .. input .. " created"
else
return "invalid or missing input"
end
end
return touch

View File

@ -100,7 +100,7 @@ function computers.load_gui(pos, node, clicker)
name = "terminal_output",
read_only = 1,
--label = "test",
default = "user:~$ test command\ntest output\n",
default = "welcome to kuto\nversion " .. computers.os.version .. "\n\nuser:~$ ",
},
{
type = "box",
@ -118,6 +118,7 @@ function computers.load_gui(pos, node, clicker)
h = 1,
name = "terminal_input",
close_on_enter = false,
pwd = "",
props = {
border = false,
},
@ -125,27 +126,30 @@ function computers.load_gui(pos, node, clicker)
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"
local pass_table = {
element = element
}
if value == "clear" then
form[cindex][eindex].default = "user:~$\n"
form[cindex][eindex].default = "user:~" .. element.pwd .."$" .."\n"
end
local cdata = value:split(" ", false, 1)
if computers.registered_commands[cdata[1]] then
form[cindex][eindex].default = text .. "user:~$ " .. cdata[1] .. "\n"
form[cindex][eindex].default = text .. value .. "\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"
local output = computers.registered_commands[cdata[1]](pos, cdata[2], pass_table)
if output and type(output) == "string" then
form[cindex][eindex].default = text .. output .. "\n" .. "user:~" .. element.pwd .."$" .." "
end
elseif value == "clear" then
form[cindex][eindex].default = "user:~$\n"
form[cindex][eindex].default = "user:~$ "
elseif value == "" then
form[cindex][eindex].default = text .. "user:~$\n"
form[cindex][eindex].default = text .. "user:~" .. element.pwd .."$" .. "\n"
else
form[cindex][eindex].default = text .. "user:~$ " .. value .. "\nERROR: command not found\n"
form[cindex][eindex].default = text .. value ..
"\nERROR: command not found\n" .. "user:~" .. element.pwd .."$" .. " "
end
return form

View File

@ -1,8 +1,12 @@
computers = {}
computers.modpath = minetest.get_modpath("computers")
computers.storagepath = minetest.get_worldpath() .. "/computers"
computers.devicepath = computers.storagepath .. "/devices"
computers.networkpath = computers.storagepath .. "/networks"
minetest.mkdir(computers.storagepath) --make sure it exists
computers.os = {
version = 0.3,
version = 0.4,
name = "kuto",
authors = {"wsor", "luk3yx"},
license = "MIT",