Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local parentShell = shell
- local bExit = false
- local sDir = (parentShell and parentShell.dir()) or ""
- local sPath = (parentShell and parentShell.path()) or ".:/rom/programs"
- local tAliases = (parentShell and parentShell.aliases()) or {}
- local tProgramStack = {}
- local shell = {}
- local tEnv = {
- ["shell"] = shell,
- }
- local function cPrint(text)
- local x,y = term.getSize()
- local x2,y2 = term.getCursorPos()
- term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
- write(text.. "\n")
- end
- -- Install shell API
- function shell.run( _sCommand, ... )
- local sPath = shell.resolveProgram( _sCommand )
- if sPath ~= nil then
- tProgramStack[#tProgramStack + 1] = sPath
- local result = os.run( tEnv, sPath, ... )
- tProgramStack[#tProgramStack] = nil
- return result
- else
- print( "No such program" )
- return false
- end
- end
- function shell.exit()
- bExit = true
- end
- function shell.dir()
- return sDir
- end
- function shell.setDir( _sDir )
- sDir = _sDir
- end
- function shell.path()
- return sPath
- end
- function shell.setPath( _sPath )
- sPath = _sPath
- end
- function shell.resolve( _sPath )
- local sStartChar = string.sub( _sPath, 1, 1 )
- if sStartChar == "/" or sStartChar == "\\" then
- return fs.combine( "", _sPath )
- else
- return fs.combine( sDir, _sPath )
- end
- end
- function shell.resolveProgram( _sCommand )
- -- Substitute aliases firsts
- if tAliases[ _sCommand ] ~= nil then
- _sCommand = tAliases[ _sCommand ]
- 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(sPath, "[^:]+") do
- sPath = fs.combine( shell.resolve( sPath ), _sCommand )
- if fs.exists( sPath ) and not fs.isDir( sPath ) then
- return sPath
- end
- end
- -- Not found
- return nil
- end
- function shell.programs( _bIncludeHidden )
- local tItems = {}
- -- Add programs from the path
- for sPath in string.gmatch(sPath, "[^:]+") do
- sPath = shell.resolve( sPath )
- if fs.isDir( sPath ) then
- local tList = fs.list( sPath )
- for n,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, b in pairs( tItems ) do
- table.insert( tItemList, sItem )
- end
- table.sort( tItemList )
- return tItemList
- end
- function shell.getRunningProgram()
- if #tProgramStack > 0 then
- return tProgramStack[#tProgramStack]
- end
- return nil
- end
- function shell.setAlias( _sCommand, _sProgram )
- tAliases[ _sCommand ] = _sProgram
- end
- function shell.clearAlias( _sCommand )
- tAliases[ _sCommand ] = nil
- end
- function shell.aliases()
- -- Add aliases
- local tCopy = {}
- for sAlias, sCommand in pairs( tAliases ) do
- tCopy[sAlias] = sCommand
- end
- return tCopy
- end
- local function fixedRead( _sReplaceChar, _tHistory )
- term.setCursorBlink( true )
- local sLine = ""
- local nHistoryPos = nil
- local nPos = 0
- if _sReplaceChar then
- _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
- end
- local w, h = term.getSize()
- local sx, sy = term.getCursorPos()
- local function redraw()
- local nScroll = 0
- if sx + nPos >= w then
- nScroll = (sx + nPos) - w
- end
- term.setCursorPos( sx, sy )
- term.write( string.rep(" ", 14) )
- term.setCursorPos( sx, sy )
- if _sReplaceChar then
- term.write( string.rep(_sReplaceChar, string.len(sLine) - nScroll) )
- else
- term.write( string.sub( sLine, nScroll + 1 ) )
- end
- term.setCursorPos( sx + nPos - nScroll, sy )
- end
- while true do
- local sEvent, param = os.pullEvent()
- if sEvent == "char" then
- if nPos < 14 then
- sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
- nPos = nPos + 1
- end
- redraw()
- elseif sEvent == "key" then
- if param == keys.enter then
- -- Enter
- break
- elseif param == keys.left then
- -- Left
- if nPos > 0 then
- nPos = nPos - 1
- redraw()
- end
- elseif param == keys.right then
- -- Right
- if nPos < string.len(sLine) then
- nPos = nPos + 1
- redraw()
- end
- elseif param == keys.up or param == keys.down then
- -- Up or down
- if _tHistory then
- if param == keys.up then
- -- Up
- if nHistoryPos == nil then
- if #_tHistory > 0 then
- nHistoryPos = #_tHistory
- end
- elseif nHistoryPos > 1 then
- nHistoryPos = nHistoryPos - 1
- end
- else
- -- Down
- if nHistoryPos == #_tHistory then
- nHistoryPos = nil
- elseif nHistoryPos ~= nil then
- nHistoryPos = nHistoryPos + 1
- end
- end
- if nHistoryPos then
- sLine = _tHistory[nHistoryPos]
- nPos = string.len( sLine )
- else
- sLine = ""
- nPos = 0
- end
- redraw()
- end
- elseif param == keys.backspace then
- -- Backspace
- if nPos > 0 then
- sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
- nPos = nPos - 1
- redraw()
- end
- end
- end
- end
- term.setCursorBlink( false )
- term.setCursorPos( w + 1, sy )
- print()
- return sLine
- end
- local function newVersion()
- return "CraftOS 2.0"
- end
- os.version = newVersion
- if not fs.exists(".accounts") then
- term.clear()
- term.setCursorPos(1,3)
- cPrint( "+-----| " ..os.version().. " |-----+")
- cPrint( "|>Welcome to " ..os.version().. "!<|")
- cPrint( "|Since this is your first,|")
- cPrint( "|You will have to setup ur|")
- cPrint( "| computer.. Enjoy! |")
- cPrint( "| |")
- cPrint( "| Press enter to continue |")
- cPrint( "| |")
- cPrint( "+-------------------------+")
- repeat
- event, key = os.pullEventRaw("key")
- until key == keys.enter
- term.clear()
- term.setCursorPos(1,3)
- cPrint( "+-----| Setup : Pg1 |-----+")
- cPrint( "| Register account: |")
- cPrint( "| |")
- cPrint( "| Username: |")
- cPrint( "| |")
- cPrint( "| Password: |")
- cPrint( "| Leave password blank to |")
- cPrint( "| not ask for pass on boot|")
- cPrint( "+-------------------------+")
- repeat
- term.setCursorPos(24,6)
- username = fixedRead()
- until username ~= ""
- term.setCursorPos(24,8)
- password = fixedRead()
- term.clear()
- term.setCursorPos(1,3)
- cPrint( "+-----| Setup : Pg2 |-----+")
- cPrint( "| Label computer (opt): |")
- cPrint( "| |")
- cPrint( "| The computer label is |")
- cPrint( "| like the name of the PC,|")
- cPrint( "| it lets you keep ur file|")
- cPrint( "| after your PC is broken.|")
- cPrint( "| This step is optional. |")
- cPrint( "+-------------------------+")
- pcID = read()
- os.setComputerLabel(pcID)
- fs.makeDir(".accounts")
- theAccount = fs.open(".accounts/" ..username, "w")
- theAccount.write(password)
- theAccount.close()
- fs.makeDir(".permissions")
- fs.makeDir(".permissions/" ..username)
- end
- -- If this is the toplevel shell, run the startup programs
- if parentShell == nil then
- -- Run the startup from the ROM first
- local sRomStartup = shell.resolveProgram( "/rom/startup" )
- if sRomStartup then
- shell.run( sRomStartup )
- end
- -- Then run the user created startup, from the disks or the root
- local sUserStartup = shell.resolveProgram( "/startup" )
- for n,sSide in pairs( redstone.getSides() ) do
- if disk.isPresent( sSide ) and disk.hasData( sSide ) then
- local sDiskStartup = shell.resolveProgram( fs.combine(disk.getMountPath( sSide ), "startup") )
- if sDiskStartup then
- sUserStartup = sDiskStartup
- break
- end
- end
- end
- if sUserStartup then
- shell.run( sUserStartup )
- end
- end
- -- Run any programs passed in as arguments
- local tArgs = { ... }
- if #tArgs > 0 then
- shell.run( ... )
- end
- -- Read commands and execute them
- term.clear()
- term.setCursorPos(1,3)
- local oldevv = os.pullEvent
- os.pullEvent = os.pullEventRaw
- cPrint( "+-----| " ..os.version().. " |-----+")
- cPrint( "| Login to your CC2.0 |")
- cPrint( "| Account |")
- cPrint( "| Username: |")
- cPrint( "| |")
- cPrint( "| Password: |")
- cPrint( "| Password not required if|")
- cPrint( "| blank when registered. |")
- cPrint( "+-------------------------+")
- repeat
- term.setCursorPos(24,6)
- username = fixedRead()
- until username ~= ""
- term.setCursorPos(24,8)
- password = fixedRead()
- tPass = fs.open(".accounts/" ..username, "r")
- if tPass then
- contents = tPass.readAll()
- tPass.close()
- else
- term.clear()
- term.setCursorPos(1,5)
- cPrint( "+-----| Login Failz |-----+")
- cPrint( "| Incorrect user/pass |")
- cPrint( "+-------------------------+")
- sleep(1)
- os.reboot()
- end
- if contents ~= password then
- term.clear()
- term.setCursorPos(1,5)
- cPrint( "+-----| Login Failz |-----+")
- cPrint( "| Incorrect user/pass |")
- cPrint( "+-------------------------+")
- sleep(1)
- os.reboot()
- end
- os.pullEvent = oldevv
- ----------------------------------------------
- --Begin the OS rendering and everything else--
- ----------------------------------------------
- term.clear()
- term.setCursorPos(1,1)
- local x,y = term.getSize()
- local tCommandHistory = {}
- local currentX = 1
- local currentY = 1
- local x,y = term.getSize()
- local tProgramsRunning = {}
- local isEnter = false;
- local tCursor = ">";
- local startMenu = false;
- local startExtend = false;
- function setCursor( cursortype )
- tCursor = cursortype
- end
- function getX()
- return currentX
- end
- function getY()
- return currentY
- end
- function newButton( text, pos1, pos2, functor )
- local xx,yy = term.getCursorPos()
- term.setCursorPos(pos1,pos2)
- write(text)
- term.setCursorPos(xx,yy)
- if isEnter and currentX == pos1-1 and currentY == pos2 then
- if tostring(functor) == functor then
- if fs.isDir(functor) then
- error() --Gonna open file browser
- else
- addRunningProgram("Error")
- shell.run(functor)
- delRunningProgram("Error")
- end
- else
- functor()
- end
- end
- end
- function runStart()
- if startMenu then
- startMenu = false
- else
- startMenu = true
- end
- end
- function extStart()
- if startExtend then
- startExtend = false
- else
- startExtend = true
- end
- end
- function newAccount()
- term.setCursorPos(1,3)
- cPrint( "+-----| Setup : Pg1 |-----+")
- cPrint( "| Register account: |")
- cPrint( "| |")
- cPrint( "| Username: |")
- cPrint( "| |")
- cPrint( "| Password: |")
- cPrint( "| Leave password blank to |")
- cPrint( "| not ask for pass on boot|")
- cPrint( "+-------------------------+")
- repeat
- term.setCursorPos(24,6)
- username = fixedRead()
- until username ~= ""
- term.setCursorPos(24,8)
- password = fixedRead()
- theAccount = fs.open(".accounts/" ..username, "w")
- theAccount.write(password)
- theAccount.close()
- fs.makeDir(".permissions/" ..username)
- end
- function isRunning( tProgramz )
- for i=1,#tProgramsRunning do
- if tProgramsRunning[i] == tProgramz then
- return true
- end
- end
- return false
- end
- function addRunningProgram( pName )
- if isRunning( pName ) ~= true then
- tProgramsRunning[#tProgramsRunning+1] = pName
- end
- end
- function delRunningProgram( pName )
- for i=1,#tProgramsRunning do
- if tProgramsRunning[i] == pName then
- tProgramsRunning[i] = nil
- return true
- end
- end
- return false
- end
- function start()
- term.setCursorPos(1, y-10)
- write("----------+")
- local files = fs.list("/")
- function clearHiddenFiles( files )
- local tItems = {}
- for i=1,#files do
- if (string.sub( files[i], 1, 1 ) ~= ".") then
- if files[i] ~= "rom" then
- tItems[#tItems+1] = files[i]
- end
- end
- end
- return tItems
- end
- local tFiles = clearHiddenFiles( files )
- for n=1,#tFiles do
- if n < 6 then
- newButton(string.sub(tFiles[n],1, 10), 2, (y-10)+n, tFiles[n])
- elseif startExtend then
- newButton(string.sub(tFiles[n],1, 10), 13, 1+(n-6), tFiles[n])
- end
- end
- if #tFiles > 6 then
- newButton("More",2, y-2, extStart)
- end
- if startExtend then
- for n=1,y-2 do
- term.setCursorPos(11, n)
- if(n == y-10)then
- write("+")
- else
- write("|")
- end
- term.setCursorPos(23, n)
- write("|")
- end
- else
- for n=1,8 do
- term.setCursorPos(11, 9+n )
- write("|")
- end
- end
- end
- function redraw()
- term.clear()
- welcomeMenu()
- newButton("Start | ", 2, y, runStart)
- for i=1,#tProgramsRunning do
- term.setCursorPos(#("Start | ")+i, y)
- write(tProgramsRunning[i].. " | ")
- end
- term.setCursorPos(x-#("| " ..textutils.formatTime(os.time()).. " "), y)
- write("| " ..textutils.formatTime(os.time()).. " ")
- for i=1,x-1 do
- term.setCursorPos(i,y-1)
- if i==8 then
- write("+")
- elseif i==(x-11) then
- write("+")
- else
- write("-")
- end
- end
- if startMenu then start() end
- term.setCursorPos(currentX,currentY)
- write(tCursor)
- end
- function welcomeMenu()
- if not fs.exists(".hidewelcome") then
- term.setCursorPos(1,3)
- cPrint( "+-| Welcome |-------------+")
- cPrint( "| Welcome to your desktop!|")
- cPrint( "| Use arrow keys to nav. |")
- cPrint( "| Use enter to select |")
- cPrint( "| OS by xXm0dzXx |")
- cPrint( "| |")
- cPrint( "| [Enter to exit] |")
- cPrint( "| |")
- cPrint( "+-------------------------+")
- addRunningProgram("Welcome")
- end
- end
- while not bExit do
- redraw()
- os.startTimer(1)
- local event, key = os.pullEvent()
- if event == "timer" and key == isEnterlol then
- isEnter = false;
- elseif event == "key" then
- if key == keys.up then
- if currentY > 1 then
- currentY = currentY -1
- end
- elseif key == keys.down then
- if currentY < y then
- currentY = currentY +1
- end
- elseif key == keys.left then
- if currentX > 1 then
- currentX = currentX -1
- end
- elseif key == keys.right then
- if currentX < x-1 then
- currentX = currentX +1
- end
- elseif key == keys.enter then
- isEnterlol = os.startTimer(0)
- isEnter = true;
- if not fs.exists(".hidewelcome") then
- fs.makeDir(".hidewelcome")
- delRunningProgram("Welcome")
- end
- end
- end
- end
- while not bExit do
- write( shell.dir() .. "> " )
- local sLine = read( nil, tCommandHistory )
- table.insert( tCommandHistory, sLine )
- local tWords = {}
- for match in string.gmatch(sLine, "[^ \t]+") do
- table.insert( tWords, match )
- end
- local sCommand = tWords[1]
- if sCommand then
- shell.run( sCommand, unpack( tWords, 2 ) )
- end
- end
- -- If this is the toplevel shell, run the shutdown program
- if parentShell == nil then
- if shell.resolveProgram( "shutdown" ) then
- shell.run( "shutdown" )
- end
- os.shutdown() -- just in case
- end
Advertisement
Add Comment
Please, Sign In to add comment