Ensure require is usable within the REPL

As 'require' operates relative to the current program's directory,
rather than the current directory, it meant we were trying to load files
from /rom/programs.

This is never a good idea, so we add the current directory to the
package path, allowing you to use require as one'd expect.
This commit is contained in:
SquidDev 2019-05-30 19:52:55 +01:00
parent 9048deeb95
commit 0cb659d78c
1 changed files with 17 additions and 0 deletions

View File

@ -18,6 +18,23 @@ local tEnv = {
}
setmetatable( tEnv, { __index = _ENV } )
-- Replace our package.path, so that it loads from the current directory, rather
-- than from /rom/programs. This makes it a little more friendly to use and
-- closer to what you'd expect.
do
local dir = shell.dir()
if dir:sub(1, 1) ~= "/" then dir = "/" .. dir end
if dir:sub(-1) ~= "/" then dir = dir .. "/" end
local strip_path = "?;?.lua;?/init.lua;"
local path = package.path
if path:sub(1, #strip_path) == strip_path then
path = path:sub(#strip_path + 1)
end
package.path = dir .. "?;" .. dir .. "?.lua;" .. dir .. "?/init.lua;" .. path
end
if term.isColour() then
term.setTextColour( colours.yellow )
end