2017-10-11 20:31:48 +00:00
|
|
|
local parentShell = _ENV.shell
|
2016-12-11 19:24:52 +00:00
|
|
|
|
2017-10-11 20:31:48 +00:00
|
|
|
_ENV.shell = { }
|
|
|
|
|
|
|
|
local fs = _G.fs
|
|
|
|
local shell = _ENV.shell
|
2017-09-25 21:00:02 +00:00
|
|
|
|
|
|
|
local sandboxEnv = setmetatable({ }, { __index = _G })
|
2017-10-11 20:31:48 +00:00
|
|
|
for k,v in pairs(_ENV) do
|
2018-01-24 22:39:38 +00:00
|
|
|
sandboxEnv[k] = v
|
2017-09-05 06:09:31 +00:00
|
|
|
end
|
2017-09-25 21:00:02 +00:00
|
|
|
sandboxEnv.shell = shell
|
2016-12-11 19:24:52 +00:00
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
_G.requireInjector(_ENV)
|
2017-09-05 06:09:31 +00:00
|
|
|
|
|
|
|
local Util = require('util')
|
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
local DIR = (parentShell and parentShell.dir()) or ""
|
|
|
|
local PATH = (parentShell and parentShell.path()) or ".:/rom/programs"
|
2017-10-13 20:30:47 +00:00
|
|
|
local tAliases = (parentShell and parentShell.aliases()) or {}
|
2016-12-11 19:24:52 +00:00
|
|
|
local tCompletionInfo = (parentShell and parentShell.getCompletionInfo()) or {}
|
|
|
|
|
|
|
|
local bExit = false
|
|
|
|
local tProgramStack = {}
|
|
|
|
|
2017-10-13 20:30:47 +00:00
|
|
|
local function tokenise( ... )
|
2018-01-24 22:39:38 +00:00
|
|
|
local sLine = table.concat( { ... }, " " )
|
|
|
|
local tWords = {}
|
|
|
|
local bQuoted = false
|
|
|
|
for match in string.gmatch( sLine .. "\"", "(.-)\"" ) do
|
|
|
|
if bQuoted then
|
|
|
|
table.insert( tWords, match )
|
|
|
|
else
|
|
|
|
for m in string.gmatch( match, "[^ \t]+" ) do
|
|
|
|
table.insert( tWords, m )
|
|
|
|
end
|
|
|
|
end
|
|
|
|
bQuoted = not bQuoted
|
|
|
|
end
|
|
|
|
|
|
|
|
return tWords
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
2017-10-14 07:41:54 +00:00
|
|
|
local function run(env, ...)
|
2018-01-24 22:39:38 +00:00
|
|
|
local args = tokenise(...)
|
|
|
|
local command = table.remove(args, 1) or error('No such program')
|
|
|
|
local isUrl = not not command:match("^(https?:)")
|
2017-10-01 00:35:36 +00:00
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
local path, loadFn
|
|
|
|
if isUrl then
|
|
|
|
path = command
|
|
|
|
loadFn = Util.loadUrl
|
|
|
|
else
|
|
|
|
path = shell.resolveProgram(command) or error('No such program')
|
|
|
|
loadFn = loadfile
|
|
|
|
end
|
2017-10-13 20:30:47 +00:00
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
local fn, err = loadFn(path, env)
|
|
|
|
if not fn then
|
|
|
|
error(err)
|
|
|
|
end
|
2017-09-15 05:08:04 +00:00
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
if _ENV.multishell then
|
|
|
|
_ENV.multishell.setTitle(_ENV.multishell.getCurrent(), fs.getName(path):match('([^%.]+)'))
|
|
|
|
end
|
2016-12-11 19:24:52 +00:00
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
if isUrl then
|
|
|
|
tProgramStack[#tProgramStack + 1] = path:match("^https?://([^/:]+:?[0-9]*/?.*)$")
|
|
|
|
else
|
|
|
|
tProgramStack[#tProgramStack + 1] = path
|
|
|
|
end
|
2017-10-11 20:31:48 +00:00
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
local r = { fn(table.unpack(args)) }
|
2017-10-11 20:31:48 +00:00
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
tProgramStack[#tProgramStack] = nil
|
2017-10-11 20:31:48 +00:00
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
return table.unpack(r)
|
2017-10-11 20:31:48 +00:00
|
|
|
end
|
|
|
|
|
2018-12-17 03:17:19 +00:00
|
|
|
local function createShellEnv(sDir)
|
|
|
|
local tEnv = setmetatable(Util.shallowCopy(sandboxEnv), { __index = _G })
|
|
|
|
|
|
|
|
local package
|
|
|
|
|
|
|
|
package = {
|
|
|
|
loaded = {
|
|
|
|
_G = _G,
|
|
|
|
bit32 = bit32,
|
|
|
|
coroutine = coroutine,
|
|
|
|
math = math,
|
|
|
|
package = package,
|
|
|
|
string = string,
|
|
|
|
table = table,
|
|
|
|
},
|
|
|
|
path = _G.LUA_PATH,
|
|
|
|
config = "/\n;\n?\n!\n-",
|
|
|
|
preload = { },
|
|
|
|
loaders = {
|
|
|
|
function( name )
|
|
|
|
if package.preload[name] then
|
|
|
|
return package.preload[name]
|
|
|
|
else
|
|
|
|
return nil, "no field package.preload['" .. name .. "']"
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
function( name )
|
|
|
|
local fname = string.gsub(name, "%.", "/")
|
|
|
|
local sError = ""
|
|
|
|
for pattern in string.gmatch(package.path, "[^;]+") do
|
|
|
|
local sPath = string.gsub(pattern, "%?", fname)
|
|
|
|
if sPath:sub(1,1) ~= "/" then
|
|
|
|
sPath = fs.combine(sDir, sPath)
|
|
|
|
end
|
|
|
|
if fs.exists(sPath) and not fs.isDir(sPath) then
|
|
|
|
local fnFile, sError = loadfile( sPath, tEnv )
|
|
|
|
if fnFile then
|
|
|
|
return fnFile, sPath
|
|
|
|
else
|
|
|
|
return nil, sError
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if #sError > 0 then
|
|
|
|
sError = sError .. "\n"
|
|
|
|
end
|
|
|
|
sError = sError .. "no file '" .. sPath .. "'"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return nil, sError
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
local sentinel = {}
|
|
|
|
local function require( name )
|
|
|
|
if type( name ) ~= "string" then
|
|
|
|
error( "bad argument #1 (expected string, got " .. type( name ) .. ")", 2 )
|
|
|
|
end
|
|
|
|
if package.loaded[name] == sentinel then
|
|
|
|
error("Loop detected requiring '" .. name .. "'", 0)
|
|
|
|
end
|
|
|
|
if package.loaded[name] then
|
|
|
|
return package.loaded[name]
|
|
|
|
end
|
|
|
|
|
|
|
|
local sError = "Error loading module '" .. name .. "':"
|
|
|
|
for _,searcher in ipairs(package.loaders) do
|
|
|
|
local loader, err = searcher(name)
|
|
|
|
if loader then
|
|
|
|
package.loaded[name] = sentinel
|
|
|
|
local result = loader( err )
|
|
|
|
if result ~= nil then
|
|
|
|
package.loaded[name] = result
|
|
|
|
return result
|
|
|
|
else
|
|
|
|
package.loaded[name] = true
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
else
|
|
|
|
sError = sError .. "\n" .. err
|
|
|
|
end
|
|
|
|
end
|
|
|
|
error(sError, 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
tEnv["package"] = package
|
|
|
|
tEnv["require"] = require
|
|
|
|
|
|
|
|
return tEnv
|
|
|
|
end
|
|
|
|
|
2017-10-11 20:31:48 +00:00
|
|
|
-- Install shell API
|
|
|
|
function shell.run(...)
|
2018-01-24 22:39:38 +00:00
|
|
|
local oldTitle
|
2017-10-13 20:30:47 +00:00
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
if _ENV.multishell then
|
|
|
|
oldTitle = _ENV.multishell.getTitle(_ENV.multishell.getCurrent())
|
|
|
|
end
|
2017-10-13 20:30:47 +00:00
|
|
|
|
2018-12-17 03:17:19 +00:00
|
|
|
local env = createShellEnv(shell.dir())
|
|
|
|
-- local env = setmetatable(Util.shallowCopy(sandboxEnv), { __index = _G })
|
2018-01-24 22:39:38 +00:00
|
|
|
local r = { pcall(run, env, ...) }
|
2017-10-13 20:30:47 +00:00
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
if _ENV.multishell then
|
|
|
|
_ENV.multishell.setTitle(_ENV.multishell.getCurrent(), oldTitle or 'shell')
|
|
|
|
end
|
2017-10-13 20:30:47 +00:00
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
return table.unpack(r)
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function shell.exit()
|
2018-01-24 22:39:38 +00:00
|
|
|
bExit = true
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function shell.dir() return DIR end
|
|
|
|
function shell.setDir(d) DIR = d end
|
|
|
|
function shell.path() return PATH end
|
|
|
|
function shell.setPath(p) PATH = p end
|
|
|
|
|
|
|
|
function shell.resolve( _sPath )
|
2018-01-24 22:39:38 +00:00
|
|
|
local sStartChar = string.sub( _sPath, 1, 1 )
|
|
|
|
if sStartChar == "/" or sStartChar == "\\" then
|
|
|
|
return fs.combine( "", _sPath )
|
|
|
|
else
|
|
|
|
return fs.combine(DIR, _sPath )
|
|
|
|
end
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function shell.resolveProgram( _sCommand )
|
2018-01-24 22:39:38 +00:00
|
|
|
if tAliases[_sCommand] ~= nil then
|
|
|
|
_sCommand = tAliases[_sCommand]
|
|
|
|
end
|
|
|
|
|
|
|
|
if _sCommand:match("^(https?:)") then
|
|
|
|
return _sCommand
|
|
|
|
end
|
|
|
|
|
|
|
|
local path = shell.resolve(_sCommand)
|
|
|
|
if fs.exists(path) and not fs.isDir(path) then
|
|
|
|
return path
|
|
|
|
end
|
|
|
|
if fs.exists(path .. '.lua') then
|
|
|
|
return path .. '.lua'
|
|
|
|
end
|
|
|
|
|
|
|
|
-- If the path is a global path, use it directly
|
|
|
|
local sStartChar = string.sub( _sCommand, 1, 1 )
|
|
|
|
if sStartChar == "/" or sStartChar == "\\" then
|
|
|
|
local sPath = fs.combine( "", _sCommand )
|
|
|
|
if fs.exists( sPath ) and not fs.isDir( sPath ) then
|
|
|
|
return sPath
|
|
|
|
end
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Otherwise, look on the path variable
|
|
|
|
for sPath in string.gmatch(PATH or '', "[^:]+") do
|
|
|
|
sPath = fs.combine(sPath, _sCommand )
|
|
|
|
if fs.exists( sPath ) and not fs.isDir( sPath ) then
|
|
|
|
return sPath
|
|
|
|
end
|
|
|
|
if fs.exists(sPath .. '.lua') then
|
|
|
|
return sPath .. '.lua'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- Not found
|
|
|
|
return nil
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function shell.programs( _bIncludeHidden )
|
2018-01-24 22:39:38 +00:00
|
|
|
local tItems = {}
|
|
|
|
|
|
|
|
-- Add programs from the path
|
|
|
|
for sPath in string.gmatch(PATH, "[^:]+") do
|
|
|
|
sPath = shell.resolve(sPath)
|
|
|
|
if fs.isDir( sPath ) then
|
|
|
|
local tList = fs.list( sPath )
|
|
|
|
for _,sFile in pairs( tList ) do
|
|
|
|
if not fs.isDir( fs.combine( sPath, sFile ) ) and
|
|
|
|
(_bIncludeHidden or string.sub( sFile, 1, 1 ) ~= ".") then
|
|
|
|
tItems[ sFile ] = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Sort and return
|
|
|
|
local tItemList = {}
|
|
|
|
for sItem in pairs( tItems ) do
|
|
|
|
table.insert( tItemList, sItem )
|
|
|
|
end
|
|
|
|
table.sort( tItemList )
|
|
|
|
return tItemList
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
2017-10-13 20:30:47 +00:00
|
|
|
local function completeProgram( sLine )
|
2018-01-24 22:39:38 +00:00
|
|
|
if #sLine > 0 and string.sub( sLine, 1, 1 ) == "/" then
|
|
|
|
-- Add programs from the root
|
|
|
|
return fs.complete( sLine, "", true, false )
|
|
|
|
else
|
|
|
|
local tResults = {}
|
|
|
|
local tSeen = {}
|
|
|
|
|
|
|
|
-- Add aliases
|
|
|
|
for sAlias in pairs( tAliases ) do
|
|
|
|
if #sAlias > #sLine and string.sub( sAlias, 1, #sLine ) == sLine then
|
|
|
|
local sResult = string.sub( sAlias, #sLine + 1 )
|
|
|
|
if not tSeen[ sResult ] then
|
|
|
|
table.insert( tResults, sResult )
|
|
|
|
tSeen[ sResult ] = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Add programs from the path
|
|
|
|
local tPrograms = shell.programs()
|
|
|
|
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 )
|
|
|
|
if not tSeen[ sResult ] then
|
|
|
|
table.insert( tResults, sResult )
|
|
|
|
tSeen[ sResult ] = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Sort and return
|
|
|
|
table.sort( tResults )
|
|
|
|
return tResults
|
|
|
|
end
|
2017-10-13 20:30:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local function completeProgramArgument( sProgram, nArgument, sPart, tPreviousParts )
|
2018-01-24 22:39:38 +00:00
|
|
|
local tInfo = tCompletionInfo[ sProgram ]
|
|
|
|
if tInfo then
|
|
|
|
return tInfo.fnComplete( shell, nArgument, sPart, tPreviousParts )
|
|
|
|
end
|
|
|
|
return nil
|
2017-10-13 20:30:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function shell.complete(sLine)
|
2018-01-24 22:39:38 +00:00
|
|
|
if #sLine > 0 then
|
|
|
|
local tWords = tokenise( sLine )
|
|
|
|
local nIndex = #tWords
|
|
|
|
if string.sub( sLine, #sLine, #sLine ) == " " then
|
|
|
|
nIndex = nIndex + 1
|
|
|
|
end
|
|
|
|
if nIndex == 1 then
|
|
|
|
local sBit = tWords[1] or ""
|
|
|
|
local sPath = shell.resolveProgram( sBit )
|
|
|
|
if tCompletionInfo[ sPath ] then
|
|
|
|
return { " " }
|
|
|
|
else
|
|
|
|
local tResults = completeProgram( sBit )
|
|
|
|
for n=1,#tResults do
|
|
|
|
local sResult = tResults[n]
|
|
|
|
local cPath = shell.resolveProgram( sBit .. sResult )
|
|
|
|
if tCompletionInfo[ cPath ] then
|
|
|
|
tResults[n] = sResult .. " "
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return tResults
|
|
|
|
end
|
|
|
|
|
|
|
|
elseif nIndex > 1 then
|
|
|
|
local sPath = shell.resolveProgram( tWords[1] )
|
|
|
|
local sPart = tWords[nIndex] or ""
|
|
|
|
local tPreviousParts = tWords
|
|
|
|
tPreviousParts[nIndex] = nil
|
|
|
|
return completeProgramArgument( sPath , nIndex - 1, sPart, tPreviousParts )
|
|
|
|
end
|
|
|
|
end
|
2017-10-13 20:30:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function shell.completeProgram( sProgram )
|
2018-01-24 22:39:38 +00:00
|
|
|
return completeProgram( sProgram )
|
2017-10-13 20:30:47 +00:00
|
|
|
end
|
2016-12-11 19:24:52 +00:00
|
|
|
|
|
|
|
function shell.setCompletionFunction(sProgram, fnComplete)
|
2018-01-24 22:39:38 +00:00
|
|
|
tCompletionInfo[sProgram] = { fnComplete = fnComplete }
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function shell.getCompletionInfo()
|
2018-01-24 22:39:38 +00:00
|
|
|
return tCompletionInfo
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function shell.getRunningProgram()
|
2018-01-24 22:39:38 +00:00
|
|
|
return tProgramStack[#tProgramStack]
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
2018-01-14 23:28:23 +00:00
|
|
|
function shell.setEnv(name, value)
|
2018-01-24 22:39:38 +00:00
|
|
|
_ENV[name] = value
|
|
|
|
sandboxEnv[name] = value
|
2018-01-14 23:28:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function shell.getEnv()
|
2018-01-24 22:39:38 +00:00
|
|
|
return sandboxEnv
|
2018-01-14 23:28:23 +00:00
|
|
|
end
|
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
function shell.setAlias( _sCommand, _sProgram )
|
2018-01-24 22:39:38 +00:00
|
|
|
tAliases[_sCommand] = _sProgram
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function shell.clearAlias( _sCommand )
|
2018-01-24 22:39:38 +00:00
|
|
|
tAliases[_sCommand] = nil
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function shell.aliases()
|
2018-01-24 22:39:38 +00:00
|
|
|
local tCopy = {}
|
|
|
|
for sAlias, sCommand in pairs(tAliases) do
|
|
|
|
tCopy[sAlias] = sCommand
|
|
|
|
end
|
|
|
|
return tCopy
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
2017-10-14 07:41:54 +00:00
|
|
|
function shell.newTab(tabInfo, ...)
|
2018-01-24 22:39:38 +00:00
|
|
|
local args = tokenise(...)
|
|
|
|
local path = table.remove(args, 1)
|
|
|
|
path = shell.resolveProgram(path)
|
2016-12-11 19:24:52 +00:00
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
if path then
|
|
|
|
tabInfo.path = path
|
|
|
|
tabInfo.env = Util.shallowCopy(sandboxEnv)
|
|
|
|
tabInfo.args = args
|
|
|
|
tabInfo.title = fs.getName(path):match('([^%.]+)')
|
2016-12-11 19:24:52 +00:00
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
if path ~= 'sys/apps/shell' then
|
|
|
|
table.insert(tabInfo.args, 1, tabInfo.path)
|
|
|
|
tabInfo.path = 'sys/apps/shell'
|
|
|
|
end
|
|
|
|
return _ENV.multishell.openTab(tabInfo)
|
|
|
|
end
|
|
|
|
return nil, 'No such program'
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function shell.openTab( ... )
|
2018-01-24 22:39:38 +00:00
|
|
|
-- needs to use multishell.launch .. so we can run with stock multishell
|
|
|
|
local tWords = tokenise( ... )
|
|
|
|
local sCommand = tWords[1]
|
|
|
|
if sCommand then
|
|
|
|
local sPath = shell.resolveProgram(sCommand)
|
|
|
|
if sPath == "sys/apps/shell" then
|
|
|
|
return _ENV.multishell.launch(Util.shallowCopy(sandboxEnv), sPath, table.unpack(tWords, 2))
|
|
|
|
else
|
|
|
|
return _ENV.multishell.launch(Util.shallowCopy(sandboxEnv), "sys/apps/shell", sCommand, table.unpack(tWords, 2))
|
|
|
|
end
|
|
|
|
end
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function shell.openForegroundTab( ... )
|
2018-01-24 22:39:38 +00:00
|
|
|
return shell.newTab({ focused = true }, ...)
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function shell.openHiddenTab( ... )
|
2018-01-24 22:39:38 +00:00
|
|
|
return shell.newTab({ hidden = true }, ...)
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function shell.switchTab(tabId)
|
2018-01-24 22:39:38 +00:00
|
|
|
_ENV.multishell.setFocus(tabId)
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local tArgs = { ... }
|
|
|
|
if #tArgs > 0 then
|
2018-12-17 03:17:19 +00:00
|
|
|
local env = createShellEnv(shell.dir())
|
|
|
|
-- local env = setmetatable(Util.shallowCopy(sandboxEnv), { __index = _G })
|
2018-01-24 22:39:38 +00:00
|
|
|
return run(env, ...)
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
local Config = require('config')
|
|
|
|
local Entry = require('entry')
|
|
|
|
local History = require('history')
|
|
|
|
local Input = require('input')
|
2018-01-20 12:18:13 +00:00
|
|
|
local Terminal = require('terminal')
|
2016-12-11 19:24:52 +00:00
|
|
|
|
2017-10-11 20:31:48 +00:00
|
|
|
local colors = _G.colors
|
|
|
|
local os = _G.os
|
|
|
|
local term = _G.term
|
|
|
|
local textutils = _G.textutils
|
|
|
|
|
2018-01-20 12:18:13 +00:00
|
|
|
local terminal = term.current()
|
2018-10-24 10:50:16 +00:00
|
|
|
--Terminal.scrollable(terminal, 100)
|
2018-01-20 12:18:13 +00:00
|
|
|
terminal.noAutoScroll = true
|
|
|
|
|
2016-12-11 19:24:52 +00:00
|
|
|
local config = {
|
2018-01-24 22:39:38 +00:00
|
|
|
standard = {
|
|
|
|
textColor = colors.white,
|
|
|
|
commandTextColor = colors.lightGray,
|
|
|
|
directoryTextColor = colors.gray,
|
|
|
|
directoryBackgroundColor = colors.black,
|
|
|
|
promptTextColor = colors.gray,
|
|
|
|
promptBackgroundColor = colors.black,
|
|
|
|
directoryColor = colors.gray,
|
|
|
|
},
|
|
|
|
color = {
|
|
|
|
textColor = colors.white,
|
|
|
|
commandTextColor = colors.yellow,
|
|
|
|
directoryTextColor = colors.orange,
|
|
|
|
directoryBackgroundColor = colors.black,
|
|
|
|
promptTextColor = colors.blue,
|
|
|
|
promptBackgroundColor = colors.black,
|
|
|
|
directoryColor = colors.green,
|
|
|
|
},
|
|
|
|
displayDirectory = true,
|
2016-12-11 19:24:52 +00:00
|
|
|
}
|
|
|
|
|
2017-10-11 20:31:48 +00:00
|
|
|
Config.load('shellprompt', config)
|
2016-12-11 19:24:52 +00:00
|
|
|
|
|
|
|
local _colors = config.standard
|
|
|
|
if term.isColor() then
|
2018-01-24 22:39:38 +00:00
|
|
|
_colors = config.color
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
2017-10-13 20:30:47 +00:00
|
|
|
local function autocompleteArgument(program, words)
|
2018-01-24 22:39:38 +00:00
|
|
|
local word = ''
|
|
|
|
if #words > 1 then
|
|
|
|
word = words[#words]
|
|
|
|
end
|
2016-12-11 19:24:52 +00:00
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
local tInfo = tCompletionInfo[program]
|
|
|
|
return tInfo.fnComplete(shell, #words - 1, word, words)
|
2017-10-13 20:30:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
local function autocompleteAnything(line, words)
|
2018-01-24 22:39:38 +00:00
|
|
|
local results = shell.complete(line)
|
2017-10-13 20:30:47 +00:00
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
if results and #results == 0 and #words == 1 then
|
|
|
|
results = nil
|
|
|
|
end
|
|
|
|
if not results then
|
|
|
|
results = fs.complete(words[#words] or '', shell.dir(), true, false)
|
|
|
|
end
|
2017-10-13 20:30:47 +00:00
|
|
|
|
2018-01-24 22:39:38 +00:00
|
|
|
return results
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
2017-10-13 20:30:47 +00:00
|
|
|
local function autocomplete(line)
|
2018-01-24 22:39:38 +00:00
|
|
|
local words = { }
|
|
|
|
for word in line:gmatch("%S+") do
|
|
|
|
table.insert(words, word)
|
|
|
|
end
|
|
|
|
if line:match(' $') then
|
|
|
|
table.insert(words, '')
|
|
|
|
end
|
|
|
|
if #words == 0 then
|
|
|
|
words = { '' }
|
|
|
|
end
|
|
|
|
|
|
|
|
local results
|
|
|
|
|
|
|
|
local program = shell.resolveProgram(words[1])
|
|
|
|
if tCompletionInfo[program] then
|
|
|
|
results = autocompleteArgument(program, words) or { }
|
|
|
|
else
|
|
|
|
results = autocompleteAnything(line, words) or { }
|
|
|
|
end
|
|
|
|
|
|
|
|
Util.filterInplace(results, function(f)
|
|
|
|
return not Util.key(results, f .. '/')
|
|
|
|
end)
|
|
|
|
local w = words[#words] or ''
|
|
|
|
for k,arg in pairs(results) do
|
|
|
|
results[k] = w .. arg
|
|
|
|
end
|
|
|
|
|
|
|
|
if #results == 1 then
|
|
|
|
words[#words] = results[1]
|
|
|
|
return table.concat(words, ' ')
|
|
|
|
elseif #results > 1 then
|
|
|
|
|
|
|
|
local function someComplete()
|
|
|
|
-- ugly (complete as much as possible)
|
|
|
|
local word = words[#words] or ''
|
|
|
|
local i = #word + 1
|
|
|
|
while true do
|
|
|
|
local ch
|
|
|
|
for _,f in ipairs(results) do
|
|
|
|
if #f < i then
|
|
|
|
words[#words] = string.sub(f, 1, i - 1)
|
|
|
|
return table.concat(words, ' ')
|
|
|
|
end
|
|
|
|
if not ch then
|
|
|
|
ch = string.sub(f, i, i)
|
|
|
|
elseif string.sub(f, i, i) ~= ch then
|
|
|
|
if i == #word + 1 then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
words[#words] = string.sub(f, 1, i - 1)
|
|
|
|
return table.concat(words, ' ')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
i = i + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local t = someComplete()
|
|
|
|
if t then
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
|
|
|
print()
|
|
|
|
|
|
|
|
local word = words[#words] or ''
|
|
|
|
local prefix = word:match("(.*/)") or ''
|
|
|
|
if #prefix > 0 then
|
|
|
|
for _,f in ipairs(results) do
|
|
|
|
if f:match("^" .. prefix) ~= prefix then
|
|
|
|
prefix = ''
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local tDirs, tFiles = { }, { }
|
|
|
|
for _,f in ipairs(results) do
|
|
|
|
if fs.isDir(shell.resolve(f)) then
|
|
|
|
f = f:gsub(prefix, '', 1)
|
|
|
|
table.insert(tDirs, f)
|
|
|
|
else
|
|
|
|
f = f:gsub(prefix, '', 1)
|
|
|
|
table.insert(tFiles, f)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
table.sort(tDirs)
|
|
|
|
table.sort(tFiles)
|
|
|
|
|
|
|
|
if #tDirs > 0 and #tDirs < #tFiles then
|
|
|
|
local tw = term.getSize()
|
|
|
|
local nMaxLen = tw / 8
|
|
|
|
for _,sItem in pairs(results) do
|
|
|
|
nMaxLen = math.max(string.len(sItem) + 1, nMaxLen)
|
|
|
|
end
|
2018-02-06 13:45:43 +00:00
|
|
|
local w = term.getSize()
|
2018-01-24 22:39:38 +00:00
|
|
|
local nCols = math.floor(w / nMaxLen)
|
|
|
|
if #tDirs < nCols then
|
|
|
|
for _ = #tDirs + 1, nCols do
|
|
|
|
table.insert(tDirs, '')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if #tDirs > 0 then
|
|
|
|
textutils.tabulate(_colors.directoryColor, tDirs, colors.white, tFiles)
|
|
|
|
else
|
|
|
|
textutils.tabulate(colors.white, tFiles)
|
|
|
|
end
|
|
|
|
|
|
|
|
term.setTextColour(_colors.promptTextColor)
|
|
|
|
term.setBackgroundColor(_colors.promptBackgroundColor)
|
|
|
|
term.write("$ " )
|
|
|
|
|
|
|
|
term.setTextColour(_colors.commandTextColor)
|
|
|
|
term.setBackgroundColor(colors.black)
|
|
|
|
return line
|
|
|
|
end
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
2017-10-03 04:50:54 +00:00
|
|
|
local function shellRead(history)
|
2018-01-24 22:39:38 +00:00
|
|
|
local lastLen = 0
|
|
|
|
local entry = Entry({
|
|
|
|
width = term.getSize() - 3
|
|
|
|
})
|
|
|
|
|
|
|
|
history:reset()
|
|
|
|
term.setCursorBlink(true)
|
|
|
|
|
|
|
|
local function redraw()
|
|
|
|
local _,cy = term.getCursorPos()
|
|
|
|
term.setCursorPos(3, cy)
|
|
|
|
local filler = #entry.value < lastLen
|
|
|
|
and string.rep(' ', lastLen - #entry.value)
|
|
|
|
or ''
|
|
|
|
local str = string.sub(entry.value, entry.scroll + 1)
|
|
|
|
term.write(string.sub(str, 1, entry.width) .. filler)
|
|
|
|
term.setCursorPos(3 + entry.pos - entry.scroll, cy)
|
|
|
|
lastLen = #entry.value
|
|
|
|
end
|
|
|
|
|
|
|
|
while true do
|
|
|
|
local event, p1, p2, p3 = os.pullEventRaw()
|
|
|
|
|
|
|
|
local ie = Input:translate(event, p1, p2, p3)
|
|
|
|
if ie then
|
|
|
|
if ie.code == 'scroll_up' then
|
2018-11-03 22:13:41 +00:00
|
|
|
--terminal.scrollUp()
|
2018-01-24 22:39:38 +00:00
|
|
|
|
|
|
|
elseif ie.code == 'scroll_down' then
|
2018-11-03 22:13:41 +00:00
|
|
|
--terminal.scrollDown()
|
2018-01-24 22:39:38 +00:00
|
|
|
|
|
|
|
elseif ie.code == 'terminate' then
|
|
|
|
bExit = true
|
|
|
|
break
|
|
|
|
|
|
|
|
elseif ie.code == 'enter' then
|
|
|
|
break
|
|
|
|
|
|
|
|
elseif ie.code == 'up' or ie.code == 'down' then
|
|
|
|
if ie.code == 'up' then
|
|
|
|
entry.value = history:back() or ''
|
|
|
|
else
|
|
|
|
entry.value = history:forward() or ''
|
|
|
|
end
|
|
|
|
entry.pos = string.len(entry.value)
|
|
|
|
entry.scroll = 0
|
|
|
|
entry:updateScroll()
|
|
|
|
redraw()
|
|
|
|
|
|
|
|
elseif ie.code == 'tab' then
|
|
|
|
if entry.pos == #entry.value then
|
|
|
|
local cline = autocomplete(entry.value)
|
|
|
|
if cline then
|
|
|
|
entry.value = cline
|
|
|
|
entry.pos = #entry.value
|
|
|
|
entry:updateScroll()
|
|
|
|
redraw()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
elseif entry:process(ie) then
|
|
|
|
redraw()
|
|
|
|
end
|
|
|
|
|
|
|
|
elseif event == "term_resize" then
|
|
|
|
entry.width = term.getSize() - 3
|
|
|
|
redraw()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--local _, cy = term.getCursorPos()
|
|
|
|
--term.setCursorPos( w + 1, cy )
|
|
|
|
print()
|
|
|
|
term.setCursorBlink( false )
|
|
|
|
return entry.value
|
2016-12-11 19:24:52 +00:00
|
|
|
end
|
|
|
|
|
2017-05-20 22:27:26 +00:00
|
|
|
local history = History.load('usr/.shell_history', 25)
|
2016-12-11 19:24:52 +00:00
|
|
|
|
|
|
|
while not bExit do
|
2018-01-24 22:39:38 +00:00
|
|
|
if config.displayDirectory then
|
|
|
|
term.setTextColour(_colors.directoryTextColor)
|
|
|
|
term.setBackgroundColor(_colors.directoryBackgroundColor)
|
|
|
|
print('==' .. os.getComputerLabel() .. ':/' .. DIR)
|
|
|
|
end
|
|
|
|
term.setTextColour(_colors.promptTextColor)
|
|
|
|
term.setBackgroundColor(_colors.promptBackgroundColor)
|
|
|
|
term.write("$ " )
|
|
|
|
term.setTextColour(_colors.commandTextColor)
|
|
|
|
term.setBackgroundColor(colors.black)
|
|
|
|
local sLine = shellRead(history)
|
|
|
|
if bExit then -- terminated
|
|
|
|
break
|
|
|
|
end
|
|
|
|
sLine = Util.trim(sLine)
|
|
|
|
if #sLine > 0 and sLine ~= 'exit' then
|
|
|
|
history:add(sLine)
|
|
|
|
end
|
|
|
|
term.setTextColour(_colors.textColor)
|
|
|
|
if #sLine > 0 then
|
|
|
|
local result, err = shell.run(sLine)
|
|
|
|
if not result and err then
|
|
|
|
_G.printError(err)
|
|
|
|
end
|
|
|
|
end
|
2017-10-14 07:41:54 +00:00
|
|
|
end
|