Replace input, output, and output_editable APIs for system.* keys

* [set/get]_input -> system.input (string)
* [set/get]_output -> system.output (string)
* set_output_editable -> system.output_editable (bool)
master
octacian 2018-11-24 21:25:27 -08:00
parent 0326648b0d
commit 12415cc700
No known key found for this signature in database
GPG Key ID: E84291D11A3509B5
6 changed files with 45 additions and 84 deletions

View File

@ -22,7 +22,6 @@ read_globals = {
"default", "sfinv", "creative",
-- OctOS Internals (custom environment functions)
"set_help", "get_attr", "get_output", "set_output",
"set_output_editable", "get_input", "set_input",
"refresh", "run", "loadstring", "set_run", "fs",
"set_help", "get_attr", "refresh", "run",
"loadstring", "set_run", "fs",
}

View File

@ -14,6 +14,9 @@ Earlier versions of digicompute had an array of functions to get and set arbitra
* `off` - `string`: command to turn the computer off.
* `reboot` - `string`: command to reboot the computer.
* `prefix` - `string`: prefix printed at the beginning of a new line.
* `input` - `string`: contents of the input field.
* `output` - `string`: contents of the output buffer.
* `output_editable` - `boolean`: whether or not the output is editable.
**Example:**
```lua
@ -51,31 +54,6 @@ Gets a piece of global information from the node meta (storage). Several common
* `id`: computer id.
* `output_editable`: whether the output buffer is editable.
#### `get_output()`
**Usage:** `get_output()`
Returns the value of the output buffer. Shorthand for `get_attr("output")`.
#### `set_output(value)`
**Usage:** `set_output(<value (string)>)`
Set the output buffer to any string. This is the write method for the output attribute.
#### `set_output_editable(bool)`
**Usage:** `set_output_editable(<true/false (bool)>)`
Makes the output buffer editable. Useful for programs that would like a larger input field. However, it can be used in any way wanted as `get_output` and `set_output` are still functional.
#### `get_input()`
**Usage:** `get_input()`
Returns the value of the input field. Shorthand for `get_attr("input")`.
#### `set_input(value)`
**Usage:** `set_input(<value (string)>)`
Set the input field to any string. This is the write method for the input attribute.
#### `refresh()`
**Usage:** `refresh()`

View File

@ -52,35 +52,6 @@ local function create_env_table(meta, pos)
return meta:to_table().fields[key] or nil
end
-- [function] Get the value of the output formspec field
function env.get_output()
return meta:get_string("output") or nil
end
-- [function] Set the value of the output formspec field
function env.set_output(value)
return meta:set_string("output", value)
end
-- [function] Toggle whether the output area can be edited
function env.set_output_editable(bool)
if bool == true then
meta:set_string("output_editable", "true")
else
meta:set_string("output_editable", "false")
end
end
-- [function] Get the value of the input formspec field
function env.get_input()
return meta:get_string("input") or nil
end
-- [function] Set the value of the input formspec field
function env.set_input(value)
return meta:set_string("input", value)
end
-- [function] Refresh the computer formspec
function env.refresh()
local current_user = meta:get_string("current_user")
@ -202,37 +173,50 @@ local function create_env_table(meta, pos)
}
setmetatable(env.ram, ram_mt)
local system_shadow = minetest.deserialize(meta:get_string("os"))
local system_shadow = minetest.deserialize(meta:get_string("system"))
-- Define OS metatable
local system_mt = {
-- Ensure value is allowed and save to meta as well
__newindex = function(table, key, value)
local allowed_keys = {
clear = true,
off = true,
reboot = true,
prefix = true,
clear = "string",
off = "string",
reboot = "string",
prefix = "string",
input = "string",
output = "string",
output_editable = "boolean",
}
-- Ensure strings
if type(value) ~= "string" then
local msg = "Error: All values in the system table must be strings."
env.print(msg)
env.print_debug(msg)
elseif not allowed_keys[key] then
-- Ensure allowed
if not allowed_keys[key] then
local msg = "Error: "..key.. " is not an allowed key in the system table."
env.print(msg)
env.print_debug(msg)
-- Ensure type
elseif type(value) ~= allowed_keys[key] then
local msg = "Error: "..key.." must be a "..allowed_keys[key]
env.print(msg)
env.print_debug(msg)
else -- else, save
rawset(system_shadow, key, value) -- Save to table
-- Save to metadata
meta:set_string("system", minetest.serialize(system_shadow))
-- Set input, output, and output editable separately
if key == "input" or key == "output" or key == "output_editable" then
rawset(system_shadow, key, value) -- Save to table
local t = allowed_keys[key]
-- if type is boolean, convert to string
if t == "boolean" then t = "string" end
-- Save separately to metadata
meta["set_"..t](meta, key, _G["to"..t](value))
else
rawset(system_shadow, key, value) -- Save to table
-- Save to metadata
meta:set_string("system", minetest.serialize(system_shadow))
end
end
end,
-- Always fetch values from the shadow table
__index = function(table, key)
return system_shadow[key]
end,
__index = system_shadow,
}
setmetatable(env.system, system_mt)

View File

@ -1,28 +1,28 @@
if get_attr("run") == "os/exec/nano.lua" then
local input = get_attr("input")
if input == "save" then
fs.write(ram.nano_path, get_output(), "w")
fs.write(ram.nano_path, system.output, "w")
elseif input == "discard" or input == "exit" then
if input == "exit" then
fs.write(ram.nano_path, get_output(), "w")
fs.write(ram.nano_path, system.output, "w")
end
ram.nano_path = nil -- Clear nano path
system.prefix = ram.nano_prefix -- Restore prefix
ram.nano_prefix = nil -- Clear stored prefix
set_output(ram.nano_output.."\n"..system.prefix) -- Restore output
system.output = ram.nano_output.."\n"..system.prefix -- Restore output
ram.nano_output = nil -- Clear stored output
set_run() -- Clear custom run file
-- Restore "output editable" to previous state
if not ram.nano_output_was_editable then
set_output_editable(false)
system.output_editable = false
else
ram.nano_output_was_editable = nil
end
end
set_input("") -- Clear input
system.input = "" -- Clear input
refresh() -- Refresh formspec
else
local path = ...
@ -32,8 +32,8 @@ else
local contents = fs.read(path) or ""
ram.nano_path = path -- Store editing path for later
ram.nano_output = get_output() -- Store old output for later
set_output(contents) -- Set output to contents of file or blank
ram.nano_output = system.output -- Store old output for later
system.output = contents -- Set output to contents of file or blank
ram.nano_prefix = system.prefix -- Store OS prefix for later
system.prefix = "" -- Set OS prefix to a blank string
set_help("\"save\" to save, \"discard\" to discard and exit, \"exit\" to save and exit") -- Add information to help
@ -41,7 +41,7 @@ else
-- Ensure output is editable
if get_attr("output_editable") == "false" then
set_output_editable(true)
system.output_editable = true
else
ram.nano_output_was_editable = true
end

View File

@ -22,7 +22,7 @@ if input[1] ~= "" then
print(system.prefix)
-- Clear input
set_input("")
system.input = ""
-- Refresh view
refresh()

View File

@ -30,7 +30,7 @@ bin[system.reboot] = { description = "Reboot computer" } -- Reboot computer
ram.bin = bin
-- Set initial output value
set_output("Welcome to octOS version 0.2.\n\n"..system.prefix) -- print welcome
system.output = "Welcome to octOS version 0.2.\n\n"..system.prefix -- print welcome
-- Refresh view
refresh()