From e379656333caefe28914c6bc0cdfe9a80b1ecd0e Mon Sep 17 00:00:00 2001 From: Andrew Lee Date: Sun, 15 Nov 2020 13:15:34 -0500 Subject: Updated MBS --- .mbs/bin/lua.lua | 39 ++++++++++++++++++++++++++++++++++++++- .mbs/bin/shell.lua | 38 +++++++++++++++++++------------------- 2 files changed, 57 insertions(+), 20 deletions(-) (limited to '.mbs/bin') diff --git a/.mbs/bin/lua.lua b/.mbs/bin/lua.lua index eed7aa9..cdbdbed 100644 --- a/.mbs/bin/lua.lua +++ b/.mbs/bin/lua.lua @@ -263,7 +263,7 @@ local function pretty(t, n) local width, height = term.getSize() local fit_height = settings.get("mbs.lua.pretty_height", true) if type(fit_height) == "number" then height = fit_height - elseif fit_height == false then height = 1/0 end + elseif fit_height == false then height = 1 / 0 end return pretty_impl(t, {}, width, height - 2, "", n) end @@ -272,6 +272,7 @@ local history = {} local counter = 1 local output = {} + local environment = setmetatable({ exit = setmetatable({}, { __tostring = function() return "Call exit() to exit" end, @@ -283,6 +284,42 @@ local environment = setmetatable({ out = output, }, { __index = _ENV }) + +local function process_auto_run_file(folderPath, file) + if string.sub( file, 1, 1 ) == "." then return end + + local path = fs.combine(folderPath, file) + if fs.isDir( path ) then return end + + local func, err = loadfile(path, nil, _ENV) + if not func then + printError(err) + return + end + + local ok, result + if settings.get("mbs.lua.traceback", true) then + ok, result = stack_trace.xpcall_with(func) + else + ok, result = pcall(func) + end + if not ok then + printError(result) + end +end + +local function load_auto_run_folder(folderPath) + if fs.exists( folderPath ) and fs.isDir( folderPath ) then + local files = fs.list( folderPath ) + for _, file in ipairs( files ) do + process_auto_run_file(folderPath, file) + end + end +end + +load_auto_run_folder("/rom/lua_autorun") +load_auto_run_folder("/lua_autorun") + local autocomplete = nil if not settings or settings.get("lua.autocomplete") then autocomplete = function(line) diff --git a/.mbs/bin/shell.lua b/.mbs/bin/shell.lua index 6dd7d9c..2fbb53a 100644 --- a/.mbs/bin/shell.lua +++ b/.mbs/bin/shell.lua @@ -7,10 +7,10 @@ if multishell then end local bExit = false -local sDir = (parentShell and parentShell.dir()) or "" -local sPath = (parentShell and parentShell.path()) or ".:/rom/programs" -local tAliases = (parentShell and parentShell.aliases()) or {} -local tCompletionInfo = (parentShell and parentShell.getCompletionInfo()) or {} +local sDir = parentShell and parentShell.dir() or "" +local sPath = parentShell and parentShell.path() or ".:/rom/programs" +local tAliases = parentShell and parentShell.aliases() or {} +local tCompletionInfo = parentShell and parentShell.getCompletionInfo() or {} local tProgramStack = {} local history = parentShell and type(parentShell.history) == "function" and parentShell.history() local fWrapper = nil @@ -36,12 +36,11 @@ local function createShellEnv(sDir) string = string, table = table, } - package.path = settings.get('mbs.shell.require_path') or - "?;?.lua;?/init.lua;/rom/modules/main/?;/rom/modules/main/?.lua;/rom/modules/main/?/init.lua" + package.path = settings.get('mbs.shell.require_path', "?;?.lua;?/init.lua;/rom/modules/main/?;/rom/modules/main/?.lua;/rom/modules/main/?/init.lua") if turtle then - package.path = package.path..";/rom/modules/turtle/?;/rom/modules/turtle/?.lua;/rom/modules/turtle/?/init.lua" + package.path = package.path .. ";/rom/modules/turtle/?;/rom/modules/turtle/?.lua;/rom/modules/turtle/?/init.lua" elseif command then - package.path = package.path..";/rom/modules/command/?;/rom/modules/command/?.lua;/rom/modules/command/?/init.lua" + package.path = package.path .. ";/rom/modules/command/?;/rom/modules/command/?.lua;/rom/modules/command/?/init.lua" end package.config = "/\n;\n?\n!\n-" package.preload = {} @@ -58,7 +57,7 @@ local function createShellEnv(sDir) local sError = "" for pattern in string.gmatch(package.path, "[^;]+") do local sPath = string.gsub(pattern, "%?", fname) - if sPath:sub(1,1) ~= "/" then + if sPath:sub(1, 1) ~= "/" then sPath = fs.combine(sDir, sPath) end if fs.exists(sPath) and not fs.isDir(sPath) then @@ -76,7 +75,7 @@ local function createShellEnv(sDir) end end return nil, sError - end + end, } local sentinel = {} @@ -133,7 +132,7 @@ local function run(_sCommand, ...) if multishell then local sTitle = fs.getName(sPath) if sTitle:sub(-4) == ".lua" then - sTitle = sTitle:sub(1,-5) + sTitle = sTitle:sub(1, -5) end multishell.setTitle(multishell.getCurrent(), sTitle) end @@ -173,7 +172,7 @@ local function run(_sCommand, ...) if #tProgramStack > 0 then local sTitle = fs.getName(tProgramStack[#tProgramStack]) if sTitle:sub(-4) == ".lua" then - sTitle = sTitle:sub(1,-5) + sTitle = sTitle:sub(1, -5) end multishell.setTitle(multishell.getCurrent(), sTitle) else @@ -275,7 +274,6 @@ function shell.resolveProgram(_sCommand) end -- If the path is a global path, use it directly - local sStartChar = string.sub(_sCommand, 1, 1) if _sCommand:find("/") or _sCommand:find("\\") then local sPath = shell.resolve(_sCommand) if fs.exists(sPath) and not fs.isDir(sPath) then @@ -314,12 +312,12 @@ function shell.programs(_bIncludeHidden) sPath = shell.resolve(sPath) if fs.isDir(sPath) then local tList = fs.list(sPath) - for n=1,#tList do + for n = 1, #tList do local sFile = tList[n] if not fs.isDir(fs.combine(sPath, sFile)) and (_bIncludeHidden or string.sub(sFile, 1, 1) ~= ".") then if #sFile > 4 and sFile:sub(-4) == ".lua" then - sFile = sFile:sub(1,-5) + sFile = sFile:sub(1, -5) end tItems[sFile] = true end @@ -368,7 +366,7 @@ local function completeProgram(sLine) -- Add programs from the path local tPrograms = shell.programs() - for n=1,#tPrograms do + for n = 1, #tPrograms do local sProgram = tPrograms[n] if #sProgram > #sLine and string.sub(sProgram, 1, #sLine) == sLine then local sResult = string.sub(sProgram, #sLine + 1) @@ -410,7 +408,7 @@ function shell.complete(sLine) return { " " } else local tResults = completeProgram(sBit) - for n=1,#tResults do + for n = 1, #tResults do local sResult = tResults[n] local sPath = shell.resolveProgram(sBit .. sResult) if tCompletionInfo[sPath] then @@ -447,7 +445,7 @@ function shell.setCompletionFunction(sProgram, fnComplete) error("bad argument #2 (expected function, got " .. type(fnComplete) .. ")", 2) end tCompletionInfo[sProgram] = { - fnComplete = fnComplete + fnComplete = fnComplete, } end @@ -542,7 +540,9 @@ local parent = term.current() local redirect = scroll_window.create(parent) local function get_first_startup() - if fs.exists("startup.lua") then return "startup.lua" end + if fs.exists("startup.lua") and not fs.isDir("startup.lua") then + return "startup.lua" + end if fs.isDir("startup") then local first = fs.list("startup")[1] if first then return fs.combine("startup", first) end -- cgit v1.2.3