1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-05-05 08:54:14 +00:00

Make io handles opaque to the docs

Yep, this is kinda gross. But also a nice refactor
This commit is contained in:
Jonathan Coates 2021-06-28 22:47:56 +01:00
parent 0b65d56ab0
commit 88f41314c7
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06

View File

@ -173,16 +173,15 @@ handleMetatable = {
}, },
} }
local defaultInput = setmetatable({ local function make_file(handle)
_handle = { readLine = _G.read }, return setmetatable({ _handle = handle }, handleMetatable)
}, handleMetatable) end
local defaultOutput = setmetatable({ local defaultInput = make_file({ readLine = _G.read })
_handle = { write = _G.write },
}, handleMetatable)
local defaultError = setmetatable({ local defaultOutput = make_file({ write = _G.write })
_handle = {
local defaultError = make_file({
write = function(...) write = function(...)
local oldColour local oldColour
if term.isColour() then if term.isColour() then
@ -192,8 +191,7 @@ local defaultError = setmetatable({
_G.write(...) _G.write(...)
if term.isColour() then term.setTextColour(oldColour) end if term.isColour() then term.setTextColour(oldColour) end
end, end,
}, })
}, handleMetatable)
local currentInput = defaultInput local currentInput = defaultInput
local currentOutput = defaultOutput local currentOutput = defaultOutput
@ -316,7 +314,7 @@ function open(filename, mode)
local file, err = fs.open(filename, sMode) local file, err = fs.open(filename, sMode)
if not file then return nil, err end if not file then return nil, err end
return setmetatable({ _handle = file }, handleMetatable) return make_file(file)
end end
--- Get or set the current output file. --- Get or set the current output file.