2021-03-21 16:41:23 +00:00
|
|
|
-- The preload file initializes everything else for our shell
|
|
|
|
-- Currently it just adds our builtins
|
|
|
|
|
|
|
|
local fs = require 'fs'
|
|
|
|
local commander = require 'commander'
|
2021-03-31 03:58:12 +00:00
|
|
|
local bait = require 'bait'
|
2021-03-21 16:41:23 +00:00
|
|
|
|
2021-04-28 11:26:23 +00:00
|
|
|
commander.register('appendpath', function (args)
|
|
|
|
bait.throw('appendpath', args)
|
|
|
|
local path = ''
|
|
|
|
for i = 1, #args do
|
|
|
|
path = path .. tostring(args[i]) .. ':'
|
|
|
|
end
|
|
|
|
path = path:sub(1, -2)
|
|
|
|
|
|
|
|
local ok, err = pcall(function() fs.appendpath(path) end)
|
|
|
|
if not ok then
|
|
|
|
if err == 1 then
|
|
|
|
print('failed to set path')
|
|
|
|
end
|
|
|
|
bait.throw('command.exit', err)
|
|
|
|
else bait.throw('command.exit', 0) end
|
|
|
|
end)
|
|
|
|
|
2021-03-31 17:46:22 +00:00
|
|
|
commander.register('cd', function (args)
|
|
|
|
bait.throw('cd', args)
|
|
|
|
if #args > 0 then
|
|
|
|
local path = ''
|
|
|
|
for i = 1, #args do
|
|
|
|
path = path .. tostring(args[i]) .. ' '
|
|
|
|
end
|
|
|
|
|
|
|
|
local ok, err = pcall(function() fs.cd(path) end)
|
2021-03-31 03:58:12 +00:00
|
|
|
if not ok then
|
|
|
|
if err == 1 then
|
2021-03-31 03:59:59 +00:00
|
|
|
print('directory does not exist')
|
2021-03-31 03:58:12 +00:00
|
|
|
end
|
2021-04-05 22:31:59 +00:00
|
|
|
bait.throw('command.exit', err)
|
|
|
|
else bait.throw('command.exit', 0) end
|
2021-03-31 03:59:59 +00:00
|
|
|
return
|
2021-03-21 16:41:23 +00:00
|
|
|
end
|
2021-03-31 03:59:59 +00:00
|
|
|
fs.cd(os.getenv 'HOME')
|
2021-04-05 22:31:59 +00:00
|
|
|
bait.throw('command.exit', 0)
|
2021-03-21 16:41:23 +00:00
|
|
|
end)
|
2021-04-03 20:08:04 +00:00
|
|
|
|
2021-04-05 19:21:44 +00:00
|
|
|
commander.register('exit', function()
|
|
|
|
os.exit(0)
|
|
|
|
end)
|
|
|
|
|
2021-04-03 20:08:04 +00:00
|
|
|
do
|
|
|
|
local virt_G = { }
|
|
|
|
|
|
|
|
setmetatable(_G, {
|
|
|
|
__index = function (self, key)
|
|
|
|
local got_virt = virt_G[key]
|
|
|
|
if got_virt ~= nil then
|
|
|
|
return got_virt
|
|
|
|
end
|
|
|
|
|
|
|
|
virt_G[key] = os.getenv(key)
|
|
|
|
return virt_G[key]
|
|
|
|
end,
|
|
|
|
|
|
|
|
__newindex = function (self, key, value)
|
|
|
|
if type(value) == 'string' then
|
|
|
|
os.setenv(key, value)
|
|
|
|
virt_G[key] = value
|
|
|
|
else
|
|
|
|
if type(virt_G[key]) == 'string' then
|
|
|
|
os.setenv(key, '')
|
|
|
|
end
|
|
|
|
virt_G[key] = value
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
|
|
|
bait.catch('command.exit', function ()
|
|
|
|
for key, value in pairs(virt_G) do
|
|
|
|
if type(value) == 'string' then
|
|
|
|
virt_G[key] = os.getenv(key)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|