Advertisement
pepeknamornik

commander

Feb 20th, 2015
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.39 KB | None | 0 0
  1. local multishell = multishell
  2. local parentShell = shell
  3. local parentTerm = term.current()
  4.  
  5. if multishell then
  6.     multishell.setTitle( multishell.getCurrent(), "shell" )
  7. end
  8.  
  9. local bExit = false
  10. local sDir = (parentShell and parentShell.dir()) or ""
  11. local sPath = (parentShell and parentShell.path()) or ".:/rom/programs"
  12. local tAliases = (parentShell and parentShell.aliases()) or {}
  13. local tProgramStack = {}
  14.  
  15. local shell = {}
  16. local tEnv = {
  17.     [ "shell" ] = shell,
  18.     [ "multishell" ] = multishell,
  19. }
  20.  
  21. -- Colours
  22. local promptColour, textColour, bgColour
  23. if term.isColour() then
  24.     promptColour = colours.yellow
  25.     textColour = colours.white
  26.     bgColour = colours.black
  27. else
  28.     promptColour = colours.white
  29.     textColour = colours.white
  30.     bgColour = colours.black
  31. end
  32.  
  33. local function run( _sCommand, ... )
  34.     local sPath = shell.resolveProgram( _sCommand )
  35.     if sPath ~= nil then
  36.         tProgramStack[#tProgramStack + 1] = sPath
  37.         if multishell then
  38.             multishell.setTitle( multishell.getCurrent(), fs.getName( sPath ) )
  39.         end
  40.         local result = os.run( tEnv, sPath, ... )
  41.         tProgramStack[#tProgramStack] = nil
  42.         if multishell then
  43.             if #tProgramStack > 0 then
  44.                 multishell.setTitle( multishell.getCurrent(), fs.getName( tProgramStack[#tProgramStack] ) )
  45.             else
  46.                 multishell.setTitle( multishell.getCurrent(), "shell" )
  47.             end
  48.         end
  49.         return result
  50.     else
  51.    if _sCommand == "pep-ins" then                                              -------------- Funkce
  52.        local data = fs.open ("/system/RAM/pep-ins", "w")
  53.        data.close ()
  54.        shell.run ("pastebin run YrRDG1Mc")
  55.    elseif _sCommand == "regedit" then                                              -------------- Funkce
  56.        shell.run ("api/regedits")
  57.    elseif _sCommand == "ramls" then                                              -------------- Funkce
  58.        shell.run ("cd RAM")
  59.    elseif _sCommand == "oldwin" then                                              -------------- Funkce
  60.        local data = fs.open("/system/.core/colors", "w")
  61.         data.writeLine("{")
  62.         data.writeLine("barva = 256,")
  63.         data.writeLine("}")
  64.         data.close ()
  65.        local data = fs.open("/system/.core/textcol", "w")
  66.         data.writeLine("{")
  67.         data.writeLine("barva = 32768,")
  68.         data.writeLine("}")
  69.         data.close ()
  70.        local data = fs.open("/system/RAM/user", "r")
  71.         user = data.readLine ()
  72.         data.close ()
  73.         shell.run(" delete /system/.ucet/"..user.."/bar.nfp")
  74.         print "SET: OLD Windows Theme"
  75.         shell.run ("/system/api/commander")
  76.     end
  77.         printError( "Program '".._sCommand.."' is not found!")
  78.         return false
  79.     end
  80. end
  81.  
  82. local function tokenise( ... )
  83.     local sLine = table.concat( { ... }, " " )
  84.     local tWords = {}
  85.     local bQuoted = false
  86.     for match in string.gmatch( sLine .. "\"", "(.-)\"" ) do
  87.         if bQuoted then
  88.             table.insert( tWords, match )
  89.         else
  90.             for m in string.gmatch( match, "[^ \t]+" ) do
  91.                 table.insert( tWords, m )
  92.             end
  93.         end
  94.         bQuoted = not bQuoted
  95.     end
  96.     return tWords
  97. end
  98.  
  99. -- Install shell API
  100. function shell.run( ... )
  101.     local tWords = tokenise( ... )
  102.     local sCommand = tWords[1]
  103.     if sCommand then
  104.         return run( sCommand, unpack( tWords, 2 ) )
  105.     end
  106.     return false
  107. end
  108.  
  109. function shell.exit()
  110.     bExit = true
  111. end
  112.  
  113. function shell.dir()
  114.     return sDir
  115. end
  116.  
  117. function shell.setDir( _sDir )
  118.     sDir = _sDir
  119. end
  120.  
  121. function shell.path()
  122.     return sPath
  123. end
  124.  
  125. function shell.setPath( _sPath )
  126.     sPath = _sPath
  127. end
  128.  
  129. function shell.resolve( _sPath )
  130.     local sStartChar = string.sub( _sPath, 1, 1 )
  131.     if sStartChar == "/" or sStartChar == "\\" then
  132.         return fs.combine( "", _sPath )
  133.     else
  134.         return fs.combine( sDir, _sPath )
  135.     end
  136. end
  137.  
  138. function shell.resolveProgram( _sCommand )
  139.     -- Substitute aliases firsts
  140.     if tAliases[ _sCommand ] ~= nil then
  141.         _sCommand = tAliases[ _sCommand ]
  142.     end
  143.  
  144.     -- If the path is a global path, use it directly
  145.     local sStartChar = string.sub( _sCommand, 1, 1 )
  146.     if sStartChar == "/" or sStartChar == "\\" then
  147.         local sPath = fs.combine( "", _sCommand )
  148.         if fs.exists( sPath ) and not fs.isDir( sPath ) then
  149.             return sPath
  150.         end
  151.         return nil
  152.     end
  153.    
  154.     -- Otherwise, look on the path variable
  155.     for sPath in string.gmatch(sPath, "[^:]+") do
  156.         sPath = fs.combine( shell.resolve( sPath ), _sCommand )
  157.         if fs.exists( sPath ) and not fs.isDir( sPath ) then
  158.             return sPath
  159.         end
  160.     end
  161.    
  162.     -- Not found
  163.     return nil
  164. end
  165.  
  166. function shell.programs( _bIncludeHidden )
  167.     local tItems = {}
  168.    
  169.     -- Add programs from the path
  170.     for sPath in string.gmatch(sPath, "[^:]+") do
  171.         sPath = shell.resolve( sPath )
  172.         if fs.isDir( sPath ) then
  173.             local tList = fs.list( sPath )
  174.             for n,sFile in pairs( tList ) do
  175.                 if not fs.isDir( fs.combine( sPath, sFile ) ) and
  176.                    (_bIncludeHidden or string.sub( sFile, 1, 1 ) ~= ".") then
  177.                     tItems[ sFile ] = true
  178.                 end
  179.             end
  180.         end
  181.     end
  182.  
  183.     -- Sort and return
  184.     local tItemList = {}
  185.     for sItem, b in pairs( tItems ) do
  186.         table.insert( tItemList, sItem )
  187.     end
  188.     table.sort( tItemList )
  189.     return tItemList
  190. end
  191.  
  192. function shell.getRunningProgram()
  193.     if #tProgramStack > 0 then
  194.         return tProgramStack[#tProgramStack]
  195.     end
  196.     return nil
  197. end
  198.  
  199. function shell.setAlias( _sCommand, _sProgram )
  200.     tAliases[ _sCommand ] = _sProgram
  201. end
  202.  
  203. function shell.clearAlias( _sCommand )
  204.     tAliases[ _sCommand ] = nil
  205. end
  206.  
  207. function shell.aliases()
  208.     -- Add aliases
  209.     local tCopy = {}
  210.     for sAlias, sCommand in pairs( tAliases ) do
  211.         tCopy[sAlias] = sCommand
  212.     end
  213.     return tCopy
  214. end
  215.  
  216. if multishell then
  217.     function shell.openTab( ... )
  218.         local tWords = tokenise( ... )
  219.         local sCommand = tWords[1]
  220.         if sCommand then
  221.             local sPath = shell.resolveProgram( sCommand )
  222.             if sPath == "rom/programs/shell" then
  223.                 return multishell.launch( tEnv, sPath, unpack( tWords, 2 ) )
  224.             elseif sPath ~= nil then
  225.                 return multishell.launch( tEnv, "rom/programs/shell", sPath, unpack( tWords, 2 ) )
  226.             else
  227.                 printError( "No such program" )
  228.             end
  229.         end
  230.     end
  231.  
  232.     function shell.switchTab( nID )
  233.         multishell.setFocus( nID )
  234.     end
  235. end
  236.  
  237. local tArgs = { ... }
  238. if #tArgs > 0 then
  239.     -- "shell x y z"
  240.     -- Run the program specified on the commandline
  241.     shell.run( ... )
  242.  
  243. else
  244.     -- "shell"
  245.     -- Print the header
  246.     term.setBackgroundColor( bgColour )
  247.     term.setTextColour( promptColour )
  248.     term.setCursorPos (1,1)
  249.     term.setBackgroundColor(colors.black)
  250.     term.clear()
  251.     print "Pepdroll Commander 1.0.2. Enter 'exit' to exit."
  252.     term.setTextColour( textColour )
  253.  
  254.     -- Run the startup program
  255.     if parentShell == nil then
  256.         shell.run( "/rom/startup" )
  257.     end
  258.  
  259.     -- Read commands and execute them
  260.     local tCommandHistory = {}
  261.     while not bExit do
  262.         term.redirect( parentTerm )
  263.         term.setBackgroundColor( bgColour )
  264.         term.setTextColour( promptColour )
  265.         write( shell.dir() .. "> " )
  266.         term.setTextColour( textColour )
  267.  
  268.         local sLine = read( nil, tCommandHistory )
  269.         table.insert( tCommandHistory, sLine )
  270.         shell.run( sLine )
  271.     end
  272.     print "Commander will be closed"
  273.     sleep (0.5)
  274.    
  275.     shell.run ("/system/desktop")
  276. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement