diff --git a/doc/computers/os_api.md b/doc/computers/os_api.md index b738279..389b580 100644 --- a/doc/computers/os_api.md +++ b/doc/computers/os_api.md @@ -10,9 +10,6 @@ In earlier versions of digicompute, a RAM-like storage mechanism could be access Earlier versions of digicompute had an array of functions to get and set arbitrary customizable data regarding the system. This data includes the prefix, clear command, input buffer, and more. However, these APIs have been replaced with a single `system` table, available globally and saved with the help of metatables. There are a very limited amount of keys that can be written to in the system table, as documented below. **Valid Keys:** -* `clear` - `string`: command to clear the output and input. -* `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. @@ -20,9 +17,23 @@ Earlier versions of digicompute had an array of functions to get and set arbitra **Example:** ```lua -system.reboot = "shutdown -r" +system.prefix = get_attr("name")..":~$ " ``` +## System API + +The system API is part of the `system` table and is used to control low-level aspects of a computer. + +#### `shutdown()` +**Usage:** `system.shutdown()` + +Turns the computer off and closes the formspec. + +#### `reboot()` +**Usage:** `system.reboot()` + +Reboots the computer and closes the formspec. + ## Main This contains a set of functions mainly for the purpose of interacting with the computer's displays. diff --git a/modules/computers/env.lua b/modules/computers/env.lua index 7410cb8..c1550ca 100644 --- a/modules/computers/env.lua +++ b/modules/computers/env.lua @@ -16,6 +16,30 @@ local function create_env_table(meta, pos) system = {}, -- Limited system table } + --- System Functions --- + + -- [function] Turn computer off + function env.system.shutdown() + local current_user = meta:get_string("current_user") + if current_user ~= "" then + local player = minetest.get_player_by_name(current_user) + if player then + return digicompute.c:off(pos, player) + end + end + end + + -- [function] Reboot computer + function env.system.reboot() + local current_user = meta:get_string("current_user") + if current_user ~= "" then + local player = minetest.get_player_by_name(current_user) + if player then + return digicompute.c:reboot(pos, player) + end + end + end + --- General Functions --- -- [function] Print to computer console @@ -179,9 +203,6 @@ local function create_env_table(meta, pos) -- Ensure value is allowed and save to meta as well __newindex = function(table, key, value) local allowed_keys = { - clear = "string", - off = "string", - reboot = "string", prefix = "string", input = "string", output = "string", diff --git a/modules/computers/gui.lua b/modules/computers/gui.lua index 64e45f9..435d841 100644 --- a/modules/computers/gui.lua +++ b/modules/computers/gui.lua @@ -143,28 +143,19 @@ digicompute.c.forms = { if digicompute.c:handle_tabs(pos, player, fields) then return end local meta = minetest.get_meta(pos) -- get meta - local os = minetest.deserialize(meta:get_string("os")) or {} - local prefix = os.prefix or "" + -- Hand values over to operating system if fields.input or fields.key_enter_field == "name" then - if fields.input == os.clear then - meta:set_string("output", prefix) - meta:set_string("input", "") - digicompute.c:open(pos, player) - elseif fields.input == os.off then digicompute.c:off(pos, player) - elseif fields.input == os.reboot then digicompute.c:reboot(pos, player) - else -- else, turn over to os - -- Set meta value(s) - meta:set_string("input", fields.input) - if fields.output then - meta:set_string("output", fields.output) - end - - local run = meta:get_string("run") - if run == "" then run = "os/main.lua" end - -- Get and run current "run file" (default: os/main.lua) - digicompute.c:run_file(pos, run) + -- Set meta value(s) + meta:set_string("input", fields.input) + if fields.output then + meta:set_string("output", fields.output) end + + local run = meta:get_string("run") + if run == "" then run = "os/main.lua" end + -- Get and run current "run file" (default: os/main.lua) + digicompute.c:run_file(pos, run) end end, }, diff --git a/octos/bin/clear b/octos/bin/clear new file mode 100644 index 0000000..850a265 --- /dev/null +++ b/octos/bin/clear @@ -0,0 +1,3 @@ +name = clear +description = Clear console output +exec = os/exec/clear.lua diff --git a/octos/bin/shutdown b/octos/bin/shutdown new file mode 100644 index 0000000..156a08d --- /dev/null +++ b/octos/bin/shutdown @@ -0,0 +1,4 @@ +name = shutdown +description = Turn off or reboot computer +params = [-r] +exec = os/exec/shutdown.lua diff --git a/octos/exec/clear.lua b/octos/exec/clear.lua new file mode 100644 index 0000000..622131c --- /dev/null +++ b/octos/exec/clear.lua @@ -0,0 +1,4 @@ +system.output = "" +system.input = "" +ram.newline_before_prefix = false +-- No need to refresh because main.lua already does so after executing the command. diff --git a/octos/exec/shutdown.lua b/octos/exec/shutdown.lua new file mode 100644 index 0000000..f77ad56 --- /dev/null +++ b/octos/exec/shutdown.lua @@ -0,0 +1,12 @@ +local option = ... +option = option[1] + +if option then + if option == "-r" then + system.reboot() + else + print("Invalid option (see help shutdown)") + end +else + system.shutdown() +end diff --git a/octos/main.lua b/octos/main.lua index 1f10bc2..cbcb9ba 100644 --- a/octos/main.lua +++ b/octos/main.lua @@ -19,7 +19,12 @@ if input[1] ~= "" then print(input[1]..": command not found") end - print(system.prefix) + if ram.newline_before_prefix ~= false then + print(system.prefix) + else + ram.newline_before_prefix = nil + print(system.prefix, false) + end -- Clear input system.input = "" diff --git a/octos/start.lua b/octos/start.lua index 2b7de45..6110b5c 100644 --- a/octos/start.lua +++ b/octos/start.lua @@ -1,7 +1,4 @@ -- Set OS values -system.clear = "clear" -system.off = "shutdown" -system.reboot = "shutdown -r" system.prefix = get_attr("name")..":~$ " -- Initialize bin table @@ -22,9 +19,9 @@ for _,f in ipairs(bin_contents.files) do end -- Add additional commands to bin -bin[system.clear] = { description = "Clear the shell output" } -- Clear shell output -bin[system.off] = { description = "Turn off computer" } -- Turn off computer -bin[system.reboot] = { description = "Reboot computer" } -- Reboot computer +--bin[system.clear] = { description = "Clear the shell output" } -- Clear shell output +--bin[system.off] = { description = "Turn off computer" } -- Turn off computer +--bin[system.reboot] = { description = "Reboot computer" } -- Reboot computer -- Save bin table ram.bin = bin