From f9221e67be2a2ab345a30389e64c494ccdb1d5a8 Mon Sep 17 00:00:00 2001 From: "kepler155c@gmail.com" Date: Wed, 11 Oct 2017 16:31:48 -0400 Subject: [PATCH] cleanup --- sys/apis/nft.lua | 7 +- sys/apis/util.lua | 12 ++-- sys/apps/Network.lua | 3 +- sys/apps/shell | 160 +++++++++++++++++++++--------------------- sys/etc/app.db | 10 +++ sys/services/chat.lua | 4 +- 6 files changed, 103 insertions(+), 93 deletions(-) diff --git a/sys/apis/nft.lua b/sys/apis/nft.lua index 1bd1e70..1a4cdd8 100644 --- a/sys/apis/nft.lua +++ b/sys/apis/nft.lua @@ -21,7 +21,12 @@ function NFT.parse(imageText) } local num = 1 - for _,sLine in ipairs(Util.split(imageText)) do + local lines = Util.split(imageText) + while #lines[#lines] == 0 do + table.remove(lines, #lines) + end + + for _,sLine in ipairs(lines) do table.insert(image.fg, { }) table.insert(image.bg, { }) table.insert(image.text, { }) diff --git a/sys/apis/util.lua b/sys/apis/util.lua index cbde077..7659494 100644 --- a/sys/apis/util.lua +++ b/sys/apis/util.lua @@ -380,15 +380,11 @@ function Util.download(url, filename) end function Util.loadUrl(url, env) -- loadfile equivalent - local s, m = pcall(function() - local c = Util.download(url) - return load(c, url, nil, env) - end) - - if s then - return m + local c, msg = Util.httpGet(url) + if not c then + return c, msg end - return s, m + return load(c, url, nil, env) end function Util.runUrl(env, url, ...) -- os.run equivalent diff --git a/sys/apps/Network.lua b/sys/apps/Network.lua index 22b0487..17337ab 100644 --- a/sys/apps/Network.lua +++ b/sys/apps/Network.lua @@ -9,6 +9,7 @@ local colors = _G.colors local device = _G.device local multishell = _ENV.multishell local network = _G.network +local os = _G.os local shell = _ENV.shell multishell.setTitle(multishell.getCurrent(), 'Network') @@ -112,7 +113,7 @@ function page:eventHandler(event) elseif event.type == 'chat' then multishell.openTab({ path = 'sys/apps/shell', - args = { 'chat join opusChat-' .. t.id .. ' guest'}, + args = { 'chat join opusChat-' .. t.id .. ' guest-' .. os.getComputerID() }, title = 'Chatroom', focused = true, }) diff --git a/sys/apps/shell b/sys/apps/shell index 0eaaf54..2704c6e 100644 --- a/sys/apps/shell +++ b/sys/apps/shell @@ -1,16 +1,20 @@ -local parentShell = shell +local parentShell = _ENV.shell -shell = { } -multishell = multishell or { } +_ENV.shell = { } +_ENV.multishell = _ENV.multishell or { } + +local fs = _G.fs +local shell = _ENV.shell +local multishell = _ENV.multishell local sandboxEnv = setmetatable({ }, { __index = _G }) -for k,v in pairs(getfenv(1)) do - sandboxEnv[k] = v +for k,v in pairs(_ENV) do + sandboxEnv[k] = v end sandboxEnv.shell = shell sandboxEnv.multishell = multishell -requireInjector(getfenv(1)) +_G.requireInjector() local Util = require('util') @@ -40,42 +44,59 @@ local function parseCommandLine( ... ) return table.remove(tWords, 1), tWords end --- Install shell API -function shell.run(...) - +local function run(env, ...) local path, args = parseCommandLine(...) - local isUrl = not not path:match("^(https?:)//(([^/:]+):?([0-9]*))(/?.*)$") + if not path then + error('No such program') + end + + local isUrl = not not path:match("^(https?:)//(([^/:]+):?([0-9]*))(/?.*)$") if not isUrl then path = shell.resolveProgram(path) + if not path then + error('No such program') + end end - if path then + local fn, err + + if isUrl then + fn, err = Util.loadUrl(path, env) + else + fn, err = loadfile(path, env) + end + + if not fn then + error(err) + end + + local oldTitle + + if multishell and multishell.getTitle then + oldTitle = multishell.getTitle(multishell.getCurrent()) + multishell.setTitle(multishell.getCurrent(), fs.getName(path)) + end + + if isUrl then + tProgramStack[#tProgramStack + 1] = path:match("^https?://([^/:]+:?[0-9]*/?.*)$") + else tProgramStack[#tProgramStack + 1] = path - local oldTitle - - if multishell and multishell.getTitle then - oldTitle = multishell.getTitle(multishell.getCurrent()) - multishell.setTitle(multishell.getCurrent(), fs.getName(path)) - end - - local result, err - - local env = Util.shallowCopy(sandboxEnv) - if isUrl then - result, err = Util.runUrl(env, path, unpack(args)) - else - result, err = Util.run(env, path, unpack(args)) - end - tProgramStack[#tProgramStack] = nil - - if multishell and multishell.getTitle then - multishell.setTitle(multishell.getCurrent(), oldTitle or 'shell') - end - - return result, err end - return false, 'No such program' + local r = { fn(table.unpack(args)) } + + tProgramStack[#tProgramStack] = nil + + if multishell and multishell.getTitle then + multishell.setTitle(multishell.getCurrent(), oldTitle or 'shell') + end + + return table.unpack(r) +end + +-- Install shell API +function shell.run(...) + return pcall(run, setmetatable(Util.shallowCopy(sandboxEnv), { __index = _G }), ...) end function shell.exit() @@ -119,7 +140,7 @@ function shell.resolveProgram( _sCommand ) end return nil end - + -- Otherwise, look on the path variable for sPath in string.gmatch(PATH or '', "[^:]+") do sPath = fs.combine(sPath, _sCommand ) @@ -130,20 +151,19 @@ function shell.resolveProgram( _sCommand ) return sPath .. '.lua' end end - -- Not found return nil end function shell.programs( _bIncludeHidden ) 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 n,sFile in pairs( tList ) do + 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 @@ -154,7 +174,7 @@ function shell.programs( _bIncludeHidden ) -- Sort and return local tItemList = {} - for sItem, b in pairs( tItems ) do + for sItem in pairs( tItems ) do table.insert( tItemList, sItem ) end table.sort( tItemList ) @@ -229,40 +249,18 @@ end local tArgs = { ... } if #tArgs > 0 then - - local path, args = parseCommandLine(...) - - if not path then - error('No such program') - end - - local isUrl = not not path:match("^(https?:)//(([^/:]+):?([0-9]*))(/?.*)$") - if not isUrl then - path = shell.resolveProgram(path) - if not path then - error('No such program') - end - end - - local fn, err - - if isUrl then - fn, err = Util.loadUrl(path, getfenv(1)) - else - fn, err = loadfile(path, getfenv(1)) - end - - if not fn then - error(err) - end - - tProgramStack[#tProgramStack + 1] = path - return fn(table.unpack(args)) + return run(_ENV, ...) end local Config = require('config') local History = require('history') +local colors = _G.colors +local keys = _G.keys +local os = _G.os +local term = _G.term +local textutils = _G.textutils + local config = { standard = { textColor = colors.white, @@ -285,7 +283,7 @@ local config = { displayDirectory = true, } ---Config.load('shell', config) +Config.load('shellprompt', config) local _colors = config.standard if term.isColor() then @@ -384,6 +382,7 @@ local function autocomplete(line, suggestions) end local results = { } + local files = { } if #words == 0 then files = autocompleteFile(results, words) @@ -398,7 +397,6 @@ local function autocomplete(line, suggestions) end local match = words[#words] or '' - local files = { } for f in pairs(results) do if f:sub(1, #match) == match then table.insert(files, f) @@ -406,7 +404,7 @@ local function autocomplete(line, suggestions) end if #files == 1 then - words[#words] = files[1] + words[#words] = files[1] return table.concat(words, ' ') elseif #files > 1 and suggestions then print() @@ -438,12 +436,12 @@ local function autocomplete(line, suggestions) if #tDirs > 0 and #tDirs < #tFiles then local w = term.getSize() local nMaxLen = w / 8 - for n, sItem in pairs(files) do + for _,sItem in pairs(files) do nMaxLen = math.max(string.len(sItem) + 1, nMaxLen) end local nCols = math.floor(w / nMaxLen) if #tDirs < nCols then - for i = #tDirs + 1, nCols do + for _ = #tDirs + 1, nCols do table.insert(tDirs, '') end end @@ -457,7 +455,7 @@ local function autocomplete(line, suggestions) term.setTextColour(_colors.promptTextColor) term.setBackgroundColor(_colors.promptBackgroundColor) - write("$ " ) + term.write("$ " ) term.setTextColour(_colors.commandTextColor) term.setBackgroundColor(colors.black) @@ -507,7 +505,7 @@ local function shellRead(history) nScroll = (sx + nPos) - w end - local cx,cy = term.getCursorPos() + local _,cy = term.getCursorPos() term.setCursorPos( sx, cy ) if sReplace then term.write( string.rep( sReplace, math.max( string.len(sLine) - nScroll, 0 ) ) ) @@ -518,7 +516,7 @@ local function shellRead(history) end while true do - local sEvent, param, param2 = os.pullEventRaw() + local sEvent, param = os.pullEventRaw() if sEvent == "char" then sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 ) @@ -581,7 +579,7 @@ local function shellRead(history) if nPos > 0 then redraw(" ") sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 ) - nPos = nPos - 1 + nPos = nPos - 1 redraw() end elseif param == keys.home then @@ -605,7 +603,7 @@ local function shellRead(history) end end - local cx, cy = term.getCursorPos() + local _, cy = term.getCursorPos() term.setCursorPos( w + 1, cy ) print() term.setCursorBlink( false ) @@ -622,7 +620,7 @@ while not bExit do end term.setTextColour(_colors.promptTextColor) term.setBackgroundColor(_colors.promptBackgroundColor) - write("$ " ) + term.write("$ " ) term.setTextColour(_colors.commandTextColor) term.setBackgroundColor(colors.black) local sLine = shellRead(history) @@ -635,9 +633,9 @@ while not bExit do end term.setTextColour(_colors.textColor) if #sLine > 0 then - local result, err = shell.run( sLine ) + local result, err = shell.run(sLine) if not result and err then - printError(err) + _G.printError(err) end end end \ No newline at end of file diff --git a/sys/etc/app.db b/sys/etc/app.db index 957a7f3..f1215d0 100644 --- a/sys/etc/app.db +++ b/sys/etc/app.db @@ -88,6 +88,16 @@ run = "simpleMiner.lua", requires = 'turtle', }, + [ "131260cbfbb0c821f8eae5e7c3c296c7aa4d50b9" ] = { + title = "Music", + category = "Apps", + icon = "\030 \031f === \ +\030 \031f | |\ +\030 \031fo| o|\ +", + run = "usr/apps/music.lua", + requires = 'turtle', + }, c47ae15370cfe1ed2781eedc1dc2547d12d9e972 = { title = "Help", category = "Apps", diff --git a/sys/services/chat.lua b/sys/services/chat.lua index 8f273f0..cb6436a 100644 --- a/sys/services/chat.lua +++ b/sys/services/chat.lua @@ -46,8 +46,8 @@ if device.wireless_modem then function() os.sleep(3) local tabId = multishell.openTab({ - fn = chatClient, - title = 'Chatroom', + fn = chatClient, + title = 'Chat', hidden = true, }) tab = multishell.getTab(tabId)