mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-11-04 15:43:00 +00:00
Merge branch 'mc-1.15.x' into mc-1.16.x
This commit is contained in:
@@ -262,41 +262,48 @@ local g_tLuaKeywords = {
|
||||
["while"] = true,
|
||||
}
|
||||
|
||||
local function serializeImpl(t, tTracking, sIndent)
|
||||
local function serialize_impl(t, tracking, indent, opts)
|
||||
local sType = type(t)
|
||||
if sType == "table" then
|
||||
if tTracking[t] ~= nil then
|
||||
if tracking[t] ~= nil then
|
||||
error("Cannot serialize table with recursive entries", 0)
|
||||
end
|
||||
tTracking[t] = true
|
||||
tracking[t] = true
|
||||
|
||||
local result
|
||||
if next(t) == nil then
|
||||
-- Empty tables are simple
|
||||
return "{}"
|
||||
result = "{}"
|
||||
else
|
||||
-- Other tables take more work
|
||||
local sResult = "{\n"
|
||||
local sSubIndent = sIndent .. " "
|
||||
local tSeen = {}
|
||||
local open, sub_indent, open_key, close_key, equal, comma = "{\n", indent .. " ", "[ ", " ] = ", " = ", ",\n"
|
||||
if opts.compact then
|
||||
open, sub_indent, open_key, close_key, equal, comma = "{", "", "[", "]=", "=", ","
|
||||
end
|
||||
|
||||
result = open
|
||||
local seen_keys = {}
|
||||
for k, v in ipairs(t) do
|
||||
tSeen[k] = true
|
||||
sResult = sResult .. sSubIndent .. serializeImpl(v, tTracking, sSubIndent) .. ",\n"
|
||||
seen_keys[k] = true
|
||||
result = result .. sub_indent .. serialize_impl(v, tracking, sub_indent, opts) .. comma
|
||||
end
|
||||
for k, v in pairs(t) do
|
||||
if not tSeen[k] then
|
||||
if not seen_keys[k] then
|
||||
local sEntry
|
||||
if type(k) == "string" and not g_tLuaKeywords[k] and string.match(k, "^[%a_][%a%d_]*$") then
|
||||
sEntry = k .. " = " .. serializeImpl(v, tTracking, sSubIndent) .. ",\n"
|
||||
sEntry = k .. equal .. serialize_impl(v, tracking, sub_indent, opts) .. comma
|
||||
else
|
||||
sEntry = "[ " .. serializeImpl(k, tTracking, sSubIndent) .. " ] = " .. serializeImpl(v, tTracking, sSubIndent) .. ",\n"
|
||||
sEntry = open_key .. serialize_impl(k, tracking, sub_indent, opts) .. close_key .. serialize_impl(v, tracking, sub_indent, opts) .. comma
|
||||
end
|
||||
sResult = sResult .. sSubIndent .. sEntry
|
||||
result = result .. sub_indent .. sEntry
|
||||
end
|
||||
end
|
||||
sResult = sResult .. sIndent .. "}"
|
||||
return sResult
|
||||
result = result .. indent .. "}"
|
||||
end
|
||||
|
||||
if opts.allow_repetitions then tracking[t] = nil end
|
||||
return result
|
||||
|
||||
elseif sType == "string" then
|
||||
return string.format("%q", t)
|
||||
|
||||
@@ -645,17 +652,43 @@ do
|
||||
end
|
||||
end
|
||||
|
||||
--- Convert a Lua object into a textual representation, suitable for
|
||||
-- saving in a file or pretty-printing.
|
||||
--
|
||||
-- @param t The object to serialise
|
||||
-- @treturn string The serialised representation
|
||||
-- @throws If the object contains a value which cannot be
|
||||
-- serialised. This includes functions and tables which appear multiple
|
||||
-- times.
|
||||
function serialize(t)
|
||||
--[[- Convert a Lua object into a textual representation, suitable for
|
||||
saving in a file or pretty-printing.
|
||||
|
||||
@param t The object to serialise
|
||||
@tparam { compact? = boolean, allow_repetitions? = boolean } opts Options for serialisation.
|
||||
- `compact`: Do not emit indentation and other whitespace between terms.
|
||||
- `allow_repetitions`: Relax the check for recursive tables, allowing them to appear multiple
|
||||
times (as long as tables do not appear inside themselves).
|
||||
|
||||
@treturn string The serialised representation
|
||||
@throws If the object contains a value which cannot be
|
||||
serialised. This includes functions and tables which appear multiple
|
||||
times.
|
||||
@see cc.pretty.pretty An alternative way to display a table, often more suitable for
|
||||
pretty printing.
|
||||
@usage Pretty print a basic table.
|
||||
|
||||
textutils.serialise({ 1, 2, 3, a = 1, ["another key"] = { true } })
|
||||
|
||||
@usage Demonstrates some of the other options
|
||||
|
||||
local tbl = { 1, 2, 3 }
|
||||
print(textutils.serialize({ tbl, tbl }, { allow_repetitions = true }))
|
||||
|
||||
print(textutils.serialize(tbl, { compact = true }))
|
||||
]]
|
||||
function serialize(t, opts)
|
||||
local tTracking = {}
|
||||
return serializeImpl(t, tTracking, "")
|
||||
expect(2, opts, "table", "nil")
|
||||
|
||||
if opts then
|
||||
field(opts, "compact", "boolean", "nil")
|
||||
field(opts, "allow_repetitions", "boolean", "nil")
|
||||
else
|
||||
opts = {}
|
||||
end
|
||||
return serialize_impl(t, tTracking, "", opts)
|
||||
end
|
||||
|
||||
serialise = serialize -- GB version
|
||||
|
||||
@@ -151,6 +151,15 @@ local vector = {
|
||||
tostring = function(self)
|
||||
return self.x .. "," .. self.y .. "," .. self.z
|
||||
end,
|
||||
|
||||
--- Check for equality between two vectors.
|
||||
--
|
||||
-- @tparam Vector self The first vector to compare.
|
||||
-- @tparam Vector other The second vector to compare to.
|
||||
-- @treturn boolean Whether or not the vectors are equal.
|
||||
equals = function(self, other)
|
||||
return self.x == other.x and self.y == other.y and self.z == other.z
|
||||
end,
|
||||
}
|
||||
|
||||
local vmetatable = {
|
||||
@@ -161,6 +170,7 @@ local vmetatable = {
|
||||
__div = vector.div,
|
||||
__unm = vector.unm,
|
||||
__tostring = vector.tostring,
|
||||
__eq = vector.equals,
|
||||
}
|
||||
|
||||
--- Construct a new @{Vector} with the given coordinates.
|
||||
|
||||
@@ -45,17 +45,19 @@ end
|
||||
-- @type Doc
|
||||
local Doc = { }
|
||||
|
||||
local function mk_doc(tbl) return setmetatable(tbl, Doc) end
|
||||
|
||||
--- An empty document.
|
||||
local empty = setmetatable({ tag = "nil" }, Doc)
|
||||
local empty = mk_doc({ tag = "nil" })
|
||||
|
||||
--- A document with a single space in it.
|
||||
local space = setmetatable({ tag = "text", text = " " }, Doc)
|
||||
local space = mk_doc({ tag = "text", text = " " })
|
||||
|
||||
--- A line break. When collapsed with @{group}, this will be replaced with @{empty}.
|
||||
local line = setmetatable({ tag = "line", flat = empty }, Doc)
|
||||
local line = mk_doc({ tag = "line", flat = empty })
|
||||
|
||||
--- A line break. When collapsed with @{group}, this will be replaced with @{space}.
|
||||
local space_line = setmetatable({ tag = "line", flat = space }, Doc)
|
||||
local space_line = mk_doc({ tag = "line", flat = space })
|
||||
|
||||
local text_cache = { [""] = empty, [" "] = space, ["\n"] = space_line }
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
local function printUsage()
|
||||
local programName = arg[0] or fs.getName(shell.getRunningProgram())
|
||||
print("Usage: " .. programName .. " <name> <program> <arguments>")
|
||||
print("Usage:")
|
||||
print(" " .. programName .. " <name> <program> <arguments>")
|
||||
print(" " .. programName .. " scale <name> <scale>")
|
||||
return
|
||||
end
|
||||
|
||||
@@ -10,6 +12,23 @@ if #tArgs < 2 then
|
||||
return
|
||||
end
|
||||
|
||||
if tArgs[1] == "scale" then
|
||||
local sName = tArgs[2]
|
||||
if peripheral.getType(sName) ~= "monitor" then
|
||||
print("No monitor named " .. sName)
|
||||
return
|
||||
end
|
||||
|
||||
local nRes = tonumber(tArgs[3])
|
||||
if nRes == nil or nRes < 0.5 or nRes > 5 then
|
||||
print("Invalid scale: " .. nRes)
|
||||
return
|
||||
end
|
||||
|
||||
peripheral.call(sName, "setTextScale", nRes)
|
||||
return
|
||||
end
|
||||
|
||||
local sName = tArgs[1]
|
||||
if peripheral.getType(sName) ~= "monitor" then
|
||||
print("No monitor named " .. sName)
|
||||
|
||||
@@ -67,10 +67,25 @@ shell.setCompletionFunction("rom/programs/label.lua", completion.build(
|
||||
))
|
||||
shell.setCompletionFunction("rom/programs/list.lua", completion.build(completion.dir))
|
||||
shell.setCompletionFunction("rom/programs/mkdir.lua", completion.build({ completion.dir, many = true }))
|
||||
|
||||
local complete_monitor_extra = { "scale" }
|
||||
shell.setCompletionFunction("rom/programs/monitor.lua", completion.build(
|
||||
{ completion.peripheral, true },
|
||||
completion.program
|
||||
function(shell, text, previous)
|
||||
local choices = completion.peripheral(shell, text, previous, true)
|
||||
for _, option in pairs(completion.choice(shell, text, previous, complete_monitor_extra, true)) do
|
||||
choices[#choices + 1] = option
|
||||
end
|
||||
return choices
|
||||
end,
|
||||
function(shell, text, previous)
|
||||
if previous[2] == "scale" then
|
||||
return completion.peripheral(shell, text, previous, true)
|
||||
else
|
||||
return completion.program(shell, text, previous)
|
||||
end
|
||||
end
|
||||
))
|
||||
|
||||
shell.setCompletionFunction("rom/programs/move.lua", completion.build(
|
||||
{ completion.dirOrFile, true },
|
||||
completion.dirOrFile
|
||||
|
||||
Reference in New Issue
Block a user