Add debug console tab

master
octacian 2018-07-23 17:16:34 -07:00
parent c836b569f5
commit 4c6884fd6f
No known key found for this signature in database
GPG Key ID: E84291D11A3509B5
5 changed files with 72 additions and 5 deletions

View File

@ -169,6 +169,11 @@ This section defines several functions that handle computer operations, such as
Updates the infotext of the computer. This is called after the computer is named or when its state changes (off/bios/on).
#### `c:print_debug(pos, msg)`
**Usage:** `digicompute.c:print_debug(<computer position (table)>, <message (*)>)`
Prints a message to the debug buffer of the computer. If contents is not a string, it will be converted to a string with `dump`.
#### `c:init(pos)`
**Usage:** `digicompute.c:init(<computer position (table)>)`

View File

@ -13,6 +13,11 @@ This contains a set of functions mainly for the purpose of interacting with the
Prints to the output buffer. If contents is not a string, it will be converted to a string with `dump`. The second parameter, if false, prevents print from inserting a newline before printing the provided contents.
#### `print_debug(msg)`
**Usage:** `print_debug(<message (*)>)`
Prints a message to the debug buffer in the Debug Console tab. If contents is not a string, it will be converted to a string with `dump`.
#### `set_help(value)`
**Usage:** `set_help(<value (string)>)`

View File

@ -57,6 +57,16 @@ function digicompute.c:infotext(pos)
end
end
-- [function] print to computer debug buffer
function digicompute.c:print_debug(pos, msg)
if type(msg) ~= "string" then msg = dump(msg) end
local meta = minetest.get_meta(pos)
local debug = minetest.deserialize(meta:get_string("debug"))
table.insert(debug, os.date("[%d/%m/%Y @ %H:%M] ")..msg)
meta:set_string("debug", minetest.serialize(debug))
return true
end
-- [function] initialize computer
function digicompute.c:init(pos)
local meta = minetest.get_meta(pos)
@ -69,6 +79,7 @@ function digicompute.c:init(pos)
digicompute.log("Initialized computer "..meta:get_string("id").." owned by "..
meta:get_string("owner").." at "..minetest.pos_to_string(pos))
digicompute.c:infotext(pos)
digicompute.c:print_debug(pos, "Initialized")
end
end
@ -83,9 +94,9 @@ function digicompute.c:deinit(pos, clear_entry)
digicompute.log("Deinitialized computer "..meta:get_string("id").." owned by "..
meta:get_string("owner").." at "..minetest.pos_to_string(pos))
if digicompute.builtin.list(main_path..owner).subdirs then
digicompute.builtin.rmdir(main_path..owner)
end
if digicompute.builtin.list(main_path..owner).subdirs then
digicompute.builtin.rmdir(main_path..owner)
end
end
local id = meta:get_string("id")
@ -115,6 +126,8 @@ function digicompute.c:complete_boot(pos, index, name, param2)
-- Set last boot to the current time for later use on_rightclick to
-- check if os/start.lua should be run
minetest.get_meta(pos):set_int("last_boot", os.time())
-- Log boot in debug buffer
digicompute.c:print_debug(pos, "Booted")
end
-- [function] turn computer on
@ -155,6 +168,8 @@ function digicompute.c:off(pos, player)
if player and player.get_player_name then
minetest.close_formspec(player:get_player_name(), "")
end
-- Log action in debug buffer
digicompute.c:print_debug(pos, "Shut down")
-- Clear output buffer
meta:set_string("output", "")
-- Reset environment
@ -192,6 +207,7 @@ function digicompute.register_computer(itemstring, def)
meta:set_string("owner", player:get_player_name())
meta:set_string("input", "") -- Initialize input buffer
meta:set_string("output", "") -- Initialize output buffer
meta:set_string("debug", minetest.serialize({})) -- Initialize debug buffer
meta:set_string("os", "") -- Initialize OS table
meta:set_string("help", "Type a command and press enter.") -- Initialize help
meta:set_string("output_editable", "false") -- Initialize uneditable output

View File

@ -32,6 +32,11 @@ local function create_env_table(meta, pos)
contents)
end
-- [function] Print to the computer debug buffer
function env.print_debug(msg)
return digicompute.c:print_debug(pos, msg)
end
-- [function] Set help text shown when hovering over question mark button
function env.set_help(value)
if not value or type(value) ~= "string" then
@ -238,6 +243,8 @@ end
function digicompute.c:run_code(pos, code, ...)
local env = digicompute.c:make_env(pos)
local ok, res = digicompute.run_code(code, env, ...)
digicompute.c:print_debug(pos, "Run Code, Success: "..dump(ok)..
", Message: "..dump(res))
return ok, res
end
@ -246,5 +253,7 @@ function digicompute.c:run_file(pos, internal_path, ...)
local complete_path = minetest.get_meta(pos):get_string("path")..internal_path
local env = digicompute.c:make_env(pos)
local ok, res = digicompute.run_file(complete_path, env, ...)
digicompute.c:print_debug(pos, "Run File ("..internal_path.."), Success: "..
dump(ok)..", Message: "..dump(res))
return ok, res
end

View File

@ -13,6 +13,7 @@ local computer_contexts = {}
local tabs = {
"main",
"debug",
"settings",
}
@ -90,7 +91,7 @@ digicompute.c.forms = {
return
"size[10,11]"..
"tabheader[0,0;tabs;Command Line,Settings;1]"..
"tabheader[0,0;tabs;Command Line,Debug Console,Settings;1]"..
"bgcolor[#000000FF;]"..
output..
"button[9.56,10.22;0.8,2;help;?]"..
@ -127,11 +128,42 @@ digicompute.c.forms = {
end
end,
},
debug = {
get = function(pos)
local meta = minetest.get_meta(pos)
local debug = minetest.deserialize(meta:get_string("debug"))
local length = 0
-- Escape lines
for _, l in pairs(debug) do
debug[_] = minetest.formspec_escape(l)
length = length + 1
end
-- Concatenate
debug = table.concat(debug, ",")
return
"size[10,11]"..
"tabheader[0,0;tabs;Command Line,Debug Console,Settings;2]"..
default.gui_bg_img..
"tableoptions[background=#000000FF;highlight=#00000000;border=false]"..
"table[-0.25,-0.38;10.38,11.17;debug;"..debug..";"..length.."]"..
"button[-0.28,10.22;10.6,2;clear;Clear Debug Console]"
end,
handle = function(pos, player, fields)
if digicompute.c:handle_tabs(pos, player, fields) then return end
if fields.clear then
local meta = minetest.get_meta(pos)
meta:set_string("debug", minetest.serialize({})) -- Clear debug buffer
digicompute.c:open(pos, player) -- Refresh formspec
end
end,
},
settings = {
get = function(pos)
return
"size[10,11]"..
"tabheader[0,0;tabs;Command Line,Settings;2]"..
"tabheader[0,0;tabs;Command Line,Debug Console,Settings;3]"..
default.gui_bg_img..
"button[0.5,0.25;9,1;reset;Reset Filesystem]"..
"tooltip[reset;Wipes all files and OS data replacing it with the basic octOS.]"..