Move clear, shutdown, and reboot to OS

* Add `system.shutdown()`
* Add `system.reboot()`
* Add `clear` command
* Add `shutdown [-r]` command
This commit is contained in:
octacian 2018-11-24 22:45:30 -08:00
parent 12415cc700
commit ca6beff464
No known key found for this signature in database
GPG Key ID: E84291D11A3509B5
9 changed files with 81 additions and 33 deletions

View File

@ -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. 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:** **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. * `prefix` - `string`: prefix printed at the beginning of a new line.
* `input` - `string`: contents of the input field. * `input` - `string`: contents of the input field.
* `output` - `string`: contents of the output buffer. * `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:** **Example:**
```lua ```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 ## Main
This contains a set of functions mainly for the purpose of interacting with the computer's displays. This contains a set of functions mainly for the purpose of interacting with the computer's displays.

View File

@ -16,6 +16,30 @@ local function create_env_table(meta, pos)
system = {}, -- Limited system table 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 --- --- General Functions ---
-- [function] Print to computer console -- [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 -- Ensure value is allowed and save to meta as well
__newindex = function(table, key, value) __newindex = function(table, key, value)
local allowed_keys = { local allowed_keys = {
clear = "string",
off = "string",
reboot = "string",
prefix = "string", prefix = "string",
input = "string", input = "string",
output = "string", output = "string",

View File

@ -143,28 +143,19 @@ digicompute.c.forms = {
if digicompute.c:handle_tabs(pos, player, fields) then return end if digicompute.c:handle_tabs(pos, player, fields) then return end
local meta = minetest.get_meta(pos) -- get meta 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 or fields.key_enter_field == "name" then
if fields.input == os.clear then -- Set meta value(s)
meta:set_string("output", prefix) meta:set_string("input", fields.input)
meta:set_string("input", "") if fields.output then
digicompute.c:open(pos, player) meta:set_string("output", fields.output)
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)
end 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
end, end,
}, },

3
octos/bin/clear Normal file
View File

@ -0,0 +1,3 @@
name = clear
description = Clear console output
exec = os/exec/clear.lua

4
octos/bin/shutdown Normal file
View File

@ -0,0 +1,4 @@
name = shutdown
description = Turn off or reboot computer
params = [-r]
exec = os/exec/shutdown.lua

4
octos/exec/clear.lua Normal file
View File

@ -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.

12
octos/exec/shutdown.lua Normal file
View File

@ -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

View File

@ -19,7 +19,12 @@ if input[1] ~= "" then
print(input[1]..": command not found") print(input[1]..": command not found")
end 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 -- Clear input
system.input = "" system.input = ""

View File

@ -1,7 +1,4 @@
-- Set OS values -- Set OS values
system.clear = "clear"
system.off = "shutdown"
system.reboot = "shutdown -r"
system.prefix = get_attr("name")..":~$ " system.prefix = get_attr("name")..":~$ "
-- Initialize bin table -- Initialize bin table
@ -22,9 +19,9 @@ for _,f in ipairs(bin_contents.files) do
end end
-- Add additional commands to bin -- Add additional commands to bin
bin[system.clear] = { description = "Clear the shell output" } -- Clear shell output --bin[system.clear] = { description = "Clear the shell output" } -- Clear shell output
bin[system.off] = { description = "Turn off computer" } -- Turn off computer --bin[system.off] = { description = "Turn off computer" } -- Turn off computer
bin[system.reboot] = { description = "Reboot computer" } -- Reboot computer --bin[system.reboot] = { description = "Reboot computer" } -- Reboot computer
-- Save bin table -- Save bin table
ram.bin = bin ram.bin = bin