Make edit display errors/results of execution and handle require. (#723)

This commit is contained in:
Wojbie 2021-02-23 21:50:19 +01:00 committed by GitHub
parent 8f3ea60c74
commit 1f70ed6985
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 44 additions and 6 deletions

View File

@ -47,6 +47,30 @@ else
stringColour = colours.white
end
local runHandler = [[multishell.setTitle(multishell.getCurrent(), %q)
local current = term.current()
local ok, err = load(%q, %q, nil, _ENV)
if ok then ok, err = pcall(ok, ...) end
term.redirect(current)
term.setTextColor(term.isColour() and colours.yellow or colours.white)
term.setBackgroundColor(colours.black)
term.setCursorBlink(false)
local _, y = term.getCursorPos()
local _, h = term.getSize()
if not ok then
printError(err)
end
if ok and y >= h then
term.scroll(1)
end
term.setCursorPos(1, h)
if ok then
write("Program finished. ")
end
write("Press any key to continue")
os.pullEvent('key')
]]
-- Menus
local bMenu = false
local nMenuItem = 1
@ -89,7 +113,7 @@ local function load(_sPath)
end
end
local function save(_sPath)
local function save(_sPath, fWrite)
-- Create intervening folder
local sDir = _sPath:sub(1, _sPath:len() - fs.getName(_sPath):len())
if not fs.exists(sDir) then
@ -101,8 +125,8 @@ local function save(_sPath)
local function innerSave()
file, fileerr = fs.open(_sPath, "w")
if file then
for _, sLine in ipairs(tLines) do
file.write(sLine .. "\n")
if file then
fWrite(file)
end
else
error("Failed to open " .. _sPath)
@ -293,7 +317,11 @@ local tMenuFuncs = {
if bReadOnly then
sStatus = "Access denied"
else
local ok, _, fileerr = save(sPath)
local ok, _, fileerr = save(sPath, function(file)
for _, sLine in ipairs(tLines) do
file.write(sLine .. "\n")
end
end)
if ok then
sStatus = "Saved to " .. sPath
else
@ -390,8 +418,18 @@ local tMenuFuncs = {
bRunning = false
end,
Run = function()
local sTempPath = "/.temp"
local ok = save(sTempPath)
local sTitle = fs.getName(sPath)
if sTitle:sub(-4) == ".lua" then
sTitle = sTitle:sub(1, -5)
end
local sTempPath = bReadOnly and ".temp." .. sTitle or fs.combine(fs.getDir(sPath), ".temp." .. sTitle)
if fs.exists(sTempPath) then
sStatus = "Error saving to " .. sTempPath
return
end
local ok = save(sTempPath, function(file)
file.write(runHandler:format(sTitle, table.concat(tLines, "\n"), "@" .. fs.getName(sPath)))
end)
if ok then
local nTask = shell.openTab(sTempPath)
if nTask then