Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --First launch
- if not fs.exists("desktop") then
- fs.makeDir("desktop")
- shell.run("clear")
- print("Installing API, this could take some time")
- guiApi = fs.open("/desktop/gui", "w")
- guiApi.write([[function getVersion()
- return "1.6.1indev2"
- end
- --Declaration of variables
- local runButton = {}
- local runHeader = {}
- local coordObject = {}
- local list = {}
- local sizeButtons = nil
- local tickBoxT = {}
- local path = "/guiAPI/log"
- --End of declaration
- function initializeLog()
- if fs.isDir("/guiAPI") then
- local logFile = fs.open(path, "w")
- logFile.write("Log initialized\nLevel 1 error means it's an error in the API, please contact me (Bjornir90) if this happens\nLevel 2 error means that this error is due to the program that use the API\n---------------------------------------------------\n")
- logFile.close()
- isInitialized = true
- else
- if fs.exists("/guiAPI") then
- error("Gui API : guiAPI is not a directory, cannot create log file, please delete this file or rename it")
- else
- fs.makeDir("/guiAPI")
- initializeLog()
- end
- end
- end
- function buttonClick(x, y, a, b)
- if type(x) ~= "number" or type(y) ~= "number" or type(a) ~= "number" or type(b) ~= "number" then
- if isInitialized == true then
- local logFile = fs.open(path, "a")
- logFile.write("[!]Detection of click ended with level 2 error\n")
- logFile.close()
- end
- error("Number expected for x, y, a or b values", 2)
- end
- event, clic, x1, y1 = os.pullEvent("mouse_click")
- if x1 <= a and x1 >= x and y1 <= b and y1 >= y then
- return "true"
- else
- return "false"
- end
- end
- function buttonWaitClick(x, y, a, b)
- clicked = "false"
- while clicked == "false" do
- if type(x) ~= "number" or type(y) ~= "number" or type(a) ~= "number" or type(b) ~= "number" then
- if isInitialized == true then
- local logFile = fs.open(path, "a")
- logFile.write("[!]Detection of click ended with level 2 error\n")
- logFile.close()
- end
- error("Number expected for x, y, a or b values", 2)
- end
- event, button, x1, y1 = os.pullEvent("mouse_click")
- if x1 <= a and x1 >= x and y1 <= b and y1 >= y then
- clicked = "true"
- else
- clicked = "false"
- end
- end
- return clicked
- end
- function setButton(x, y, text, colorLine, color, clears)
- if type(x) ~= "number" or type(y) ~= "number" or type(text) ~= "string" then
- if isInitialized == true then
- local logFile = fs.open(path, "a")
- logFile.write("[!]Setting of button ended with level 2 error\n")
- logFile.close()
- end
- error("Number expected for x and y values or string expected for text value", 2)
- end
- if clears == nil then
- local clears = "false"
- end
- if tostring(clears) == "true" then
- clear()
- end
- if colorLine then
- colorL = colorLine
- else
- colorL = colors.white
- end
- if color then
- colorB = color
- else
- colorB = colors.black
- end
- local sizeText = #tostring(text)
- a = x+1+sizeText
- b = y+2
- table.insert(runButton, x)
- table.insert(runButton, y)
- table.insert(runButton, a)
- table.insert(runButton, b)
- table.insert(runButton, text)
- table.insert(runButton, colorL)
- table.insert(runButton, colorB)
- table.insert(coordObject, {x,y,a,b,"button"})
- if isInitialized == true then
- local logFile = fs.open(path, "a")
- logFile.write("Set a button at "..x.."X and at "..y.."Y with text "..text.."\n")
- logFile.close()
- end
- end
- function drawButton(x, y, a, b, text, colorLine, color)
- if type(x) ~= "number" or type(y) ~= "number" or type(a) ~= "number" or type(b) ~= "number" or type(text) ~= "string" then
- if isInitialized == true then
- local logFile = fs.open(path, "a")
- logFile.write("[!]Drawing of button ended with level 2 error\n")
- logFile.close()
- end
- error("Number expected for x and y and a and b values or string expected for text value")
- end
- if colorLine then
- colorL = colorLine
- else
- colorL = colors.white
- end
- if color then
- colorB = color
- else
- colorB = colors.black
- end
- paintutils.drawLine(x, y, a, y, colorL) --draw the top line
- paintutils.drawLine(x, y, x, b, colorL) --draw the left line
- paintutils.drawLine(a, y, a, b, colorL) --draw the right line
- paintutils.drawLine(x, b, a, b, colorL) --draw the bottom line
- fill(x+1, y+1, a-1, b-1, colorB)
- term.setCursorPos(x+1, y+1)--Print the text in the button
- term.write(text)
- if isInitialized == true then
- local logFile = fs.open(path, "a")
- logFile.write("Drawn a button at "..x.."X and at "..y.."Y with text "..text.."\n")
- logFile.close()
- end
- end
- function setHeader(text, colorLine)
- if type(text) ~= "string" then
- if isInitialized == true then
- local logFile = fs.open(path, "a")
- logFile.write("[!]Setting of header ended with level 2 error\n")
- logFile.close()
- end
- error("String expected for text value", 2)
- end
- if colorLine then
- colorL = colorLine
- else
- colorL = colors.white
- end
- local text = tostring(text)
- table.insert(runHeader, text)
- table.insert(runHeader, colorL)
- if isInitialized == true then
- local logFile = fs.open(path, "a")
- logFile.write("Set a header with text "..text.."\n")
- logFile.close()
- end
- end
- function drawHeader(text, colorLine)
- if type(text) ~= "string" then
- if isInitialized == true then
- local logFile = fs.open(path, "a")
- logFile.write("[!]Drawing of header ended with level 1 error\n")
- logFile.close()
- end
- error("String expected for text value")
- end
- if colorLine then
- colorL = colorLine
- else
- colorL = colors.white
- end
- xScreen, yScreen = term.getSize()
- paintutils.drawLine(1, 1, xScreen, 1)
- term.setCursorPos(1, 1)
- write(text)
- paintutils.drawLine(1, 2, xScreen, 2, colorL)
- term.setCursorPos(1, 3)
- term.setBackgroundColor(colors.black)
- if isInitialized == true then
- local logFile = fs.open(path, "a")
- logFile.write("Drawn a header with text "..text.."\n")
- logFile.close()
- end
- end
- function clear()
- term.setCursorPos(1, 1)
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- if isInitialized == true then
- local logFile = fs.open(path, "a")
- logFile.write("Cleared the screen\n")
- logFile.close()
- end
- end
- function fill(x, y, a, b, color)
- term.setBackgroundColor(color)
- oldY = y
- for x=x, a do
- for y=y, b do
- term.setCursorPos(x,y)
- print(" ")
- end
- end
- end
- function render(shouldClear)
- if isInitialized == true then
- local logFile = fs.open(path, "a")
- logFile.write("----------------Started rendering------------------\n")
- logFile.close()
- end
- if not shouldClear then
- clear()
- end
- local sizeBut = #runButton
- local sizeHead = #runHeader
- local sizeTickBox = #tickBoxT
- for x=1, sizeBut, 7 do
- drawButton(runButton[x], runButton[x+1], runButton[x+2], runButton[x+3], runButton[x+4], runButton[x+5], runButton[x+6]) --Run the functions to redraw it
- end
- for x=1, sizeHead, 2 do
- drawHeader(runHeader[x], runHeader[x+1]) --Run the function to redraw it
- end
- for x=1, sizeTickBox, 4 do
- drawTickBox(tickBoxT[x], tickBoxT[x+1], tickBoxT[x+2], tickBoxT[x+3])
- end
- if reset == nil then
- local reset = "false"
- end
- if tostring(reset) == "true" then
- clearBuffer()
- end
- if isInitialized == true then
- local logFile = fs.open(path, "a")
- logFile.write("--------------Succesfully rendered-----------------\n")
- logFile.close()
- end
- end
- function clearBuffer()
- runButton = {}
- runHeader = {}
- tickBoxT = {}
- if isInitialized == true then
- local logFile = fs.open(path, "a")
- logFile.write("Cleared buffer\n")
- logFile.close()
- end
- end
- function getSelection()
- local sizeObject = #coordObject
- detectObject = {}
- for x=1, sizeObject do
- table.insert(detectObject, function() buttonWaitClick(coordObject[x][1], coordObject[x][2], coordObject[x][3], coordObject[x][4]) end)
- end
- local input = parallel.waitForAny(unpack(detectObject))
- coordObject = {}
- if isInitialized == true then
- local logFile = fs.open(path, "a")
- logFile.write("Got user selection : "..input.."\n")
- logFile.close()
- end
- return input
- end
- function animation(x, y, a, b, colorL)
- m = a-x
- n = x-a
- if isInitialized == true then
- local logFile = fs.open(path, "a")
- logFile.write("-----------**Started pop-up animation**------------\nX : "..x.." Y : "..y.." ColorLine : "..colorL.."\n")
- logFile.close()
- end
- paintutils.drawLine(x+3, y+3, a-3, y-3, colorL) --draw the top line
- paintutils.drawLine(x+3, y+3, x-3, b-3, colorL) --draw the left line
- paintutils.drawLine(a+3, y+3, a-3, b-3, colorL) --draw the right line
- paintutils.drawLine(x+3, b+3, a-3, b-3, colorL) --draw the bottom line
- sleep(0.05)
- render()
- paintutils.drawLine(x+2, y+2, a-2, y-2, colorL) --draw the top line
- paintutils.drawLine(x+2, y+2, x-2, b-2, colorL) --draw the left line
- paintutils.drawLine(a+2, y+2, a-2, b-2, colorL) --draw the right line
- paintutils.drawLine(x+2, b+2, a-2, b-2, colorL) --draw the bottom line
- sleep(0.05)
- render()
- paintutils.drawLine(x+1, y+1, a-1, y-1, colorL) --draw the top line
- paintutils.drawLine(x+1, y+1, x-1, b-1, colorL) --draw the left line
- paintutils.drawLine(a+1, y+1, a-1, b-1, colorL) --draw the right line
- paintutils.drawLine(x+1, b+1, a-1, b-1, colorL) --draw the bottom line
- sleep(0.05)
- render()
- if isInitialized == true then
- local logFile = fs.open(path, "a")
- logFile.write("-----**Successfully ended pop-up animation**-------\n")
- logFile.close()
- end
- end
- function popUp(title, text, x, y, colorLine, color, textB1, textB2)
- if type(x) ~= "number" or type(y) ~= "number" or type(title) ~= "string" or type(textB1) ~= "string" or type(text) ~= "string" or type(textB2) ~= "string" then
- if isInitialized == true then
- local logFile = fs.open(path, "a")
- logFile.write("[!]Drawing of pop-up ended with level 2 error\n[!]Skipped drawing of pop-up\n")
- logFile.close()
- end
- skip = true
- else
- skip = false
- end
- if not skip then
- render() --Render so the things drawn behind appears
- sizeTitle = #title
- sizeText = #text
- if textB2 ~= nil then
- sizeB1 = #textB1
- sizeB2 = #textB2
- sizeButtons = sizeB2+sizeB1
- mustButton = "true"
- else
- sizeButtons = 0
- mustButton = "false"
- end
- if sizeTitle > sizeText and sizeTitle > sizeButtons then
- greaterS = sizeTitle
- elseif sizeText > sizeTitle and sizeText > sizeButtons then
- greaterS = sizeText
- else
- greaterS = sizeButtons
- end
- if colorLine then
- local colorL = colorLine
- else
- local colorL = colors.white
- end
- if color then
- local colorB = color
- else
- local colorB = colors.black
- end
- local a = x + greaterS + 1
- local b = y + 4
- animation(x, y, a, b, colorL)
- paintutils.drawLine(x, y, a, y, colorL) --draw the top line
- paintutils.drawLine(x, y, x, b, colorL) --draw the left line
- paintutils.drawLine(a, y, a, b, colorL) --draw the right line
- paintutils.drawLine(x, b, a, b, colorL) --draw the bottom line
- fill(x+1, y+1, a-1, b-1, colorB)
- term.setCursorPos(x+1, y+1) --Print the title
- term.write(title)
- paintutils.drawLine(x, y+2, a, b-2, colorL) --Draw the line for the title
- term.setBackgroundColor(colorB)
- term.setCursorPos(x+1, y+3)
- term.write(text) --Print the text
- if isInitialized == true then
- local logFile = fs.open(path, "a")
- logFile.write("Drawn a pop-up with :\nTitle : "..title.."\nText : "..text.."\nAt coordinates x : "..x.." and y : "..y.."\nColorLine : "..colorL.." ColorBackground : "..colorB.."\nDrawn buttons : "..mustButton.."\n--------------End of pop-up description------------\n")
- logFile.close()
- end
- if mustButton == "true" then
- local midX = math.ceil((a-x)/2)
- term.setCursorPos(midX-1-sizeB1, b)
- term.write(textB1)
- term.setCursorPos(midX+1, b)
- term.write(textB2)
- clickedB = parallel.waitForAny(function() buttonWaitClick(midX-sizeB1-1, b, midX-1, b) end, function() buttonWaitClick(midX+1, b, midX+sizeB2+1, b) end)
- return clickedB
- else
- clickedPopup = buttonWaitClick(x, y, a, b)
- return clickedPopup
- end
- else
- return
- end
- end
- ]])
- guiApi.close()
- print("API installed")
- print("Installing shell")
- local shellF = fs.open("desktop/shell", "w")
- shellF.write([[
- 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,
- }
- -- Colours
- local promptColour, textColour, bgColour
- if term.isColour() then
- promptColour = colours.yellow
- textColour = colours.white
- bgColour = colours.black
- else
- promptColour = colours.white
- textColour = colours.white
- bgColour = colours.black
- end
- local function run( _sCommand, ... )
- --Modified by Bjornir90
- if _sCommand == "exit" then
- shell.run("desk")
- end
- 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
- printError( "No such program" )
- return false
- end
- end
- local function runLine( _sLine )
- local tWords = {}
- for match in string.gmatch( _sLine, "[^ \t]+" ) do
- table.insert( tWords, match )
- end
- local sCommand = tWords[1]
- if sCommand then
- return run( sCommand, unpack( tWords, 2 ) )
- end
- return false
- end
- -- Install shell API
- function shell.run( ... )
- return runLine( table.concat( { ... }, " " ) )
- 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
- term.setBackgroundColor( bgColour )
- term.setTextColour( promptColour )
- print( os.version() )
- term.setTextColour( textColour )
- -- 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( peripheral.getNames() ) do
- --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
- local tCommandHistory = {}
- while not bExit do
- term.setBackgroundColor( bgColour )
- term.setTextColour( promptColour )
- write( shell.dir() .. "> " )
- term.setTextColour( textColour )
- local sLine = read( nil, tCommandHistory )
- table.insert( tCommandHistory, sLine )
- runLine( sLine )
- 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
- ]])
- shellF.close()
- print("Installing edit")
- local editF = fs.open("desktop/edit", "w")
- editF.write([[ -- Get file to edit
- local tArgs = { ... }
- if #tArgs == 0 then
- print( "Usage: edit <path>" )
- return
- end
- -- Error checking
- local sPath = shell.resolve( tArgs[1] )
- local bReadOnly = fs.isReadOnly( sPath )
- if fs.exists( sPath ) and fs.isDir( sPath ) then
- print( "Cannot edit a directory." )
- return
- end
- local x,y = 1,1
- local w,h = term.getSize()
- local scrollX, scrollY = 0,0
- local tLines = {}
- local bRunning = true
- -- Colours
- local highlightColour, keywordColour, commentColour, textColour, bgColour
- if term.isColour() then
- bgColour = colours.black
- textColour = colours.white
- highlightColour = colours.yellow
- keywordColour = colours.yellow
- commentColour = colours.lime
- stringColour = colours.red
- else
- bgColour = colours.black
- textColour = colours.white
- highlightColour = colours.white
- keywordColour = colours.white
- commentColour = colours.white
- stringColour = colours.white
- end
- -- Menus
- local bMenu = false
- local nMenuItem = 1
- local tMenuItems = {"Save", "Exit", "Print"}
- local sStatus = "Press Ctrl to access menu"
- local function load(_sPath)
- tLines = {}
- if fs.exists( _sPath ) then
- local file = io.open( _sPath, "r" )
- local sLine = file:read()
- while sLine do
- table.insert( tLines, sLine )
- sLine = file:read()
- end
- file:close()
- end
- if #tLines == 0 then
- table.insert( tLines, "" )
- end
- end
- local function save( _sPath )
- -- Create intervening folder
- local sDir = sPath:sub(1, sPath:len() - fs.getName(sPath):len() )
- if not fs.exists( sDir ) then
- fs.makeDir( sDir )
- end
- -- Save
- local file = nil
- local function innerSave()
- file = fs.open( _sPath, "w" )
- if file then
- for n, sLine in ipairs( tLines ) do
- file.write( sLine .. "\n" )
- end
- else
- error( "Failed to open ".._sPath )
- end
- end
- local ok = pcall( innerSave )
- if file then
- file.close()
- end
- return ok
- end
- local tKeywords = {
- ["and"] = true,
- ["break"] = true,
- ["do"] = true,
- ["else"] = true,
- ["elseif"] = true,
- ["end"] = true,
- ["false"] = true,
- ["for"] = true,
- ["function"] = true,
- ["if"] = true,
- ["in"] = true,
- ["local"] = true,
- ["nil"] = true,
- ["not"] = true,
- ["or"] = true,
- ["repeat"] = true,
- ["return"] = true,
- ["then"] = true,
- ["true"] = true,
- ["until"]= true,
- ["while"] = true,
- }
- local function tryWrite( sLine, regex, colour )
- local match = string.match( sLine, regex )
- if match then
- if type(colour) == "number" then
- term.setTextColour( colour )
- else
- term.setTextColour( colour(match) )
- end
- term.write( match )
- term.setTextColour( textColour )
- return string.sub( sLine, string.len(match) + 1 )
- end
- return nil
- end
- local function writeHighlighted( sLine )
- while string.len(sLine) > 0 do
- sLine =
- tryWrite( sLine, "^%-%-%[%[.-%]%]", commentColour ) or
- tryWrite( sLine, "^%-%-.*", commentColour ) or
- tryWrite( sLine, "^\".-[^\\]\"", stringColour ) or
- tryWrite( sLine, "^\'.-[^\\]\'", stringColour ) or
- tryWrite( sLine, "^%[%[.-%]%]", stringColour ) or
- tryWrite( sLine, "^[%w_]+", function( match )
- if tKeywords[ match ] then
- return keywordColour
- end
- return textColour
- end ) or
- tryWrite( sLine, "^[^%w_]", textColour )
- end
- end
- local function redrawText()
- for y=1,h-1 do
- term.setCursorPos( 1 - scrollX, y )
- term.clearLine()
- local sLine = tLines[ y + scrollY ]
- if sLine ~= nil then
- writeHighlighted( sLine )
- end
- end
- term.setCursorPos( x - scrollX, y - scrollY )
- end
- local function redrawLine(_nY)
- local sLine = tLines[_nY]
- term.setCursorPos( 1 - scrollX, _nY - scrollY )
- term.clearLine()
- writeHighlighted( sLine )
- term.setCursorPos( x - scrollX, _nY - scrollY )
- end
- local function setLeftStatus()
- end
- local function redrawMenu()
- term.setCursorPos( 1, h )
- term.clearLine()
- local sLeft, sRight
- local nLeftColour, nLeftHighlight1, nLeftHighlight2
- if bMenu then
- local sMenu = ""
- for n,sItem in ipairs( tMenuItems ) do
- if n == nMenuItem then
- nLeftHighlight1 = sMenu:len() + 1
- nLeftHighlight2 = sMenu:len() + sItem:len() + 2
- end
- sMenu = sMenu.." "..sItem.." "
- end
- sLeft = sMenu
- nLeftColour = textColour
- else
- sLeft = sStatus
- nLeftColour = highlightColour
- end
- -- Left goes last so that it can overwrite the line numbers.
- sRight = "Ln "..y
- term.setTextColour( highlightColour )
- term.setCursorPos( w-sRight:len() + 1, h )
- term.write(sRight)
- sRight = tostring(y)
- term.setTextColour( textColour )
- term.setCursorPos( w-sRight:len() + 1, h )
- term.write(sRight)
- if sLeft then
- term.setCursorPos( 1, h )
- term.setTextColour( nLeftColour )
- term.write(sLeft)
- if nLeftHighlight1 then
- term.setTextColour( highlightColour )
- term.setCursorPos( nLeftHighlight1, h )
- term.write( "[" )
- term.setCursorPos( nLeftHighlight2, h )
- term.write( "]" )
- end
- term.setTextColour( textColour )
- end
- -- Cursor highlights selection
- term.setCursorPos( x - scrollX, y - scrollY )
- end
- local tMenuFuncs = {
- Save=function()
- if bReadOnly then
- sStatus = "Access denied"
- else
- local ok, err = save( sPath )
- if ok then
- sStatus="Saved to "..sPath
- else
- sStatus="Error saving to "..sPath
- end
- end
- redrawMenu()
- end,
- Print=function()
- local sPrinterSide = nil
- for n,sName in ipairs(peripheral.getNames()) do
- if peripheral.isPresent(sName) and peripheral.getType(sName) == "printer" then
- sPrinterSide = sName
- break
- end
- end
- if not sPrinterSide then
- sStatus = "No printer attached"
- return
- end
- local nPage = 0
- local sName = fs.getName( sPath )
- local printer = peripheral.wrap(sPrinterSide)
- if printer.getInkLevel() < 1 then
- sStatus = "Printer out of ink"
- return
- elseif printer.getPaperLevel() < 1 then
- sStatus = "Printer out of paper"
- return
- end
- local terminal = {
- getCursorPos = printer.getCursorPos,
- setCursorPos = printer.setCursorPos,
- getSize = printer.getPageSize,
- write = printer.write,
- }
- terminal.scroll = function()
- if nPage == 1 then
- printer.setPageTitle( sName.." (page "..nPage..")" )
- end
- while not printer.newPage() do
- if printer.getInkLevel() < 1 then
- sStatus = "Printer out of ink, please refill"
- elseif printer.getPaperLevel() < 1 then
- sStatus = "Printer out of paper, please refill"
- else
- sStatus = "Printer output tray full, please empty"
- end
- term.restore()
- redrawMenu()
- term.redirect( terminal )
- local timer = os.startTimer(0.5)
- sleep(0.5)
- end
- nPage = nPage + 1
- if nPage == 1 then
- printer.setPageTitle( sName )
- else
- printer.setPageTitle( sName.." (page "..nPage..")" )
- end
- end
- bMenu = false
- term.redirect( terminal )
- local ok, error = pcall( function()
- term.scroll()
- for n, sLine in ipairs( tLines ) do
- print( sLine )
- end
- end )
- term.restore()
- if not ok then
- print( error )
- end
- while not printer.endPage() do
- sStatus = "Printer output tray full, please empty"
- redrawMenu()
- sleep( 0.5 )
- end
- bMenu = true
- if nPage > 1 then
- sStatus = "Printed "..nPage.." Pages"
- else
- sStatus = "Printed 1 Page"
- end
- redrawMenu()
- end,
- Exit=function()
- --Modified by Bjornir90
- shell.run("desk")
- bRunning = false
- end
- }
- local function doMenuItem( _n )
- tMenuFuncs[tMenuItems[_n] ]()
- if bMenu then
- bMenu = false
- term.setCursorBlink( true )
- end
- redrawMenu()
- end
- local function setCursor( x, y )
- local screenX = x - scrollX
- local screenY = y - scrollY
- local bRedraw = false
- if screenX < 1 then
- scrollX = x - 1
- screenX = 1
- bRedraw = true
- elseif screenX > w then
- scrollX = x - w
- screenX = w
- bRedraw = true
- end
- if screenY < 1 then
- scrollY = y - 1
- screenY = 1
- bRedraw = true
- elseif screenY > h-1 then
- scrollY = y - (h-1)
- screenY = h-1
- bRedraw = true
- end
- if bRedraw then
- redrawText()
- end
- term.setCursorPos( screenX, screenY )
- -- Statusbar now pertains to menu, it would probably be safe to redraw the menu on every key event.
- redrawMenu()
- end
- -- Actual program functionality begins
- load(sPath)
- term.setBackgroundColour( bgColour )
- term.clear()
- term.setCursorPos(x,y)
- term.setCursorBlink( true )
- redrawText()
- redrawMenu()
- -- Handle input
- while bRunning do
- local sEvent, param, param2, param3 = os.pullEvent()
- if sEvent == "key" then
- if param == keys.up then
- -- Up
- if not bMenu then
- if y > 1 then
- -- Move cursor up
- y = y - 1
- x = math.min( x, string.len( tLines[y] ) + 1 )
- setCursor( x, y )
- end
- end
- elseif param == keys.down then
- -- Down
- if not bMenu then
- -- Move cursor down
- if y < #tLines then
- y = y + 1
- x = math.min( x, string.len( tLines[y] ) + 1 )
- setCursor( x, y )
- end
- end
- elseif param == keys.tab then
- -- Tab
- if not bMenu then
- local sLine = tLines[y]
- -- Indent line
- -- IN CASE OF INSERT TAB IN PLACE:
- -- tLines[y] = string.sub(sLine,1,x-1) .. " " .. string.sub(sLine,x)
- tLines[y]=" "..tLines[y]
- x = x + 2
- setCursor( x, y )
- redrawLine(y)
- end
- elseif param == keys.pageUp then
- -- Page Up
- if not bMenu then
- -- Move up a page
- local sx,sy=term.getSize()
- y=y-sy-1
- if y<1 then y=1 end
- x = math.min( x, string.len( tLines[y] ) + 1 )
- setCursor( x, y )
- end
- elseif param == keys.pageDown then
- -- Page Down
- if not bMenu then
- -- Move down a page
- local sx,sy=term.getSize()
- if y<#tLines-sy-1 then
- y = y+sy-1
- else
- y = #tLines
- end
- x = math.min( x, string.len( tLines[y] ) + 1 )
- setCursor( x, y )
- end
- elseif param == keys.home then
- -- Home
- if not bMenu then
- -- Move cursor to the beginning
- x=1
- setCursor(x,y)
- end
- elseif param == keys["end"] then
- -- End
- if not bMenu then
- -- Move cursor to the end
- x = string.len( tLines[y] ) + 1
- setCursor(x,y)
- end
- elseif param == keys.left then
- -- Left
- if not bMenu then
- if x > 1 then
- -- Move cursor left
- x = x - 1
- elseif x==1 and y>1 then
- x = string.len( tLines[y-1] ) + 1
- y = y - 1
- end
- setCursor( x, y )
- else
- -- Move menu left
- nMenuItem = nMenuItem - 1
- if nMenuItem < 1 then
- nMenuItem = #tMenuItems
- end
- redrawMenu()
- end
- elseif param == keys.right then
- -- Right
- if not bMenu then
- if x < string.len( tLines[y] ) + 1 then
- -- Move cursor right
- x = x + 1
- elseif x==string.len( tLines[y] ) + 1 and y<#tLines then
- x = 1
- y = y + 1
- end
- setCursor( x, y )
- else
- -- Move menu right
- nMenuItem = nMenuItem + 1
- if nMenuItem > #tMenuItems then
- nMenuItem = 1
- end
- redrawMenu()
- end
- elseif param == keys.delete then
- -- Delete
- if not bMenu then
- if x < string.len( tLines[y] ) + 1 then
- local sLine = tLines[y]
- tLines[y] = string.sub(sLine,1,x-1) .. string.sub(sLine,x+1)
- redrawLine(y)
- elseif y<#tLines then
- tLines[y] = tLines[y] .. tLines[y+1]
- table.remove( tLines, y+1 )
- redrawText()
- redrawMenu()
- end
- end
- elseif param == keys.backspace then
- -- Backspace
- if not bMenu then
- if x > 1 then
- -- Remove character
- local sLine = tLines[y]
- tLines[y] = string.sub(sLine,1,x-2) .. string.sub(sLine,x)
- redrawLine(y)
- x = x - 1
- setCursor( x, y )
- elseif y > 1 then
- -- Remove newline
- local sPrevLen = string.len( tLines[y-1] )
- tLines[y-1] = tLines[y-1] .. tLines[y]
- table.remove( tLines, y )
- redrawText()
- x = sPrevLen + 1
- y = y - 1
- setCursor( x, y )
- end
- end
- elseif param == keys.enter then
- -- Enter
- if not bMenu then
- -- Newline
- local sLine = tLines[y]
- local _,spaces=string.find(sLine,"^[ ]+")
- if not spaces then
- spaces=0
- end
- tLines[y] = string.sub(sLine,1,x-1)
- table.insert( tLines, y+1, string.rep(' ',spaces)..string.sub(sLine,x) )
- redrawText()
- x = spaces+1
- y = y + 1
- setCursor( x, y )
- else
- -- Menu selection
- doMenuItem( nMenuItem )
- end
- elseif param == keys.leftCtrl or param == keys.rightCtrl then
- -- Menu toggle
- bMenu = not bMenu
- if bMenu then
- term.setCursorBlink( false )
- nMenuItem = 1
- else
- term.setCursorBlink( true )
- end
- redrawMenu()
- end
- elseif sEvent == "char" then
- if not bMenu then
- -- Input text
- local sLine = tLines[y]
- tLines[y] = string.sub(sLine,1,x-1) .. param .. string.sub(sLine,x)
- redrawLine(y)
- x = x + string.len( param )
- setCursor( x, y )
- else
- -- Select menu items
- for n,sMenuItem in ipairs( tMenuItems ) do
- if string.lower(string.sub(sMenuItem,1,1)) == string.lower(param) then
- doMenuItem( n )
- break
- end
- end
- end
- elseif sEvent == "mouse_click" then
- if not bMenu then
- if param == 1 then
- -- Left click
- local cx,cy = param2, param3
- if cy < h then
- y = math.min( math.max( scrollY + cy, 1 ), #tLines )
- x = math.min( math.max( scrollX + cx, 1 ), string.len( tLines[y] ) + 1 )
- setCursor( x, y )
- end
- end
- end
- elseif sEvent == "mouse_scroll" then
- if not bMenu then
- if param == -1 then
- -- Scroll up
- if scrollY > 0 then
- -- Move cursor up
- scrollY = scrollY - 1
- redrawText()
- end
- elseif param == 1 then
- -- Scroll down
- local nMaxScroll = #tLines - (h-1)
- if scrollY < nMaxScroll then
- -- Move cursor down
- scrollY = scrollY + 1
- redrawText()
- end
- end
- end
- end
- end
- -- Cleanup
- term.clear()
- term.setCursorBlink( false )
- term.setCursorPos( 1, 1 )]])
- editF.close()
- local favori = fs.open("/desktop/favoris", "w")
- favori.write("{[1]=\"0\", [2]=\"0\", [3]=\"0\", [4]=\"0\", [5]=\"0\", [6]=\"0\",}")
- favori.close()
- print("Installation done")
- sleep(0.5)
- end
- --Initialisation
- os.loadAPI("/desktop/gui")
- gui.initializeLog()
- local _path = shell.getRunningProgram()
- shell.setAlias("desk", _path)
- local termX, termY = term.getSize()
- local midX, midY = termX/2-0.5, termY/2-0.5
- --Format : table {fav1{infos} fav2{infos}}
- local favF = fs.open("/desktop/favoris", "r")
- local favS = favF.readAll()
- local favT = textutils.unserialize(favS)
- local fav1 = favT[1]
- local fav2 = favT[2]
- local fav3 = favT[3]
- local fav4 = favT[4]
- local fav5 = favT[5]
- local fav6 = favT[6]
- favF.close()
- --End of initialisation
- -- args : x, y, text, colorLine, colorBG
- local function createFav(number)
- favT[number] = {}
- gui.clear()
- gui.clearBuffer()
- gui.setHeader(" Favori "..number.." editing", colors.lightBlue)
- gui.fill(1,1,termX,termY,colors.cyan)
- term.setBackgroundColor(colors.cyan)
- term.setCursorPos(1,4)
- write("Name of favori :")
- term.setBackgroundColor(colors.black)
- write(" ") --Draw a black line to enter characters
- term.setCursorPos(1,6)
- term.setBackgroundColor(colors.cyan)
- write("Path to program :")
- term.setBackgroundColor(colors.black)
- write(" ")
- term.setCursorPos(1, 8)
- term.setBackgroundColor(colors.cyan)
- write("Argument (optional) :")
- term.setBackgroundColor(colors.black)
- write(" ")
- term.setCursorPos(17,4)
- favT[number]["text"] = tostring(read())
- term.setCursorPos(18,6)
- favT[number]["path"] = tostring(read())
- term.setCursorPos(22,8)
- favT[number]["arg"] = tostring(read())
- local favS = textutils.serialize(favT)
- local favF = fs.open("desktop/favoris", "w")
- favF.write(favS)
- favF.close()
- local favF = fs.open("/desktop/favoris", "r")
- local favS = favF.readAll()
- favT = textutils.unserialize(favS)
- fav1 = favT[1]
- fav2 = favT[2]
- fav3 = favT[3]
- fav4 = favT[4]
- fav5 = favT[5]
- fav6 = favT[6]
- favF.close()
- drawFavori()
- end
- local function editFav(number)
- gui.clear()
- gui.clearBuffer()
- gui.fill(1,1,termX,termY,colors.cyan)
- gui.setButton(3,4, "EDIT FAVORI", colors.lightBlue, colors.blue)
- gui.setButton(3,9, "DELETE FAVORI", colors.lightBlue, colors.blue)
- gui.setHeader(" EDIT FAVORI "..number, colors.lightBlue)
- gui.setButton(midX-4, midY, "CANCEL", colors.lightBlue, colors.blue)
- gui.render(true)
- local input = gui.getSelection()
- if input == 1 then
- createFav(number)
- elseif input == 2 then
- if favT[number+1] ~= "0" then
- favT[number] = "0"
- favT[number] = favT[number+1]
- favT[number+1] = "0"
- elseif favT[number+1] == "0" then
- favT[number] = "0"
- end
- local favS = textutils.serialize(favT)
- local favF = fs.open("desktop/favoris", "w")
- favF.write(favS)
- favF.close()
- local favF = fs.open("/desktop/favoris", "r")
- local favS = favF.readAll()
- favT = textutils.unserialize(favS)
- fav1 = favT[1]
- fav2 = favT[2]
- fav3 = favT[3]
- fav4 = favT[4]
- fav5 = favT[5]
- fav6 = favT[6]
- favF.close()
- drawFavori()
- elseif input == 3 then
- drawFavori()
- end
- end
- local function selectFavori()
- local input = gui.getSelection()
- if input == 1 then
- if fav1 ~= "0" then
- editFav(1)
- else
- createFav(1)
- end
- elseif input == 2 then
- if fav2 ~= "0" then
- editFav(2)
- else
- createFav(2)
- end
- elseif input == 3 then
- if fav3 ~= "0" then
- editFav(3)
- else
- createFav(3)
- end
- elseif input == 4 then
- if fav4 ~= "0" then
- editFav(4)
- else
- createFav(4)
- end
- elseif input == 5 then
- if fav5 ~= "0" then
- editFav(5)
- else
- createFav(5)
- end
- elseif input == 6 then
- if fav6 ~= "0" then
- editFav(6)
- else
- createFav(6)
- end
- elseif input == 7 then
- drawOptionDev()
- end
- end
- function drawFavori()
- gui.clear()
- gui.clearBuffer()
- gui.fill(1,1,termX,termY, colors.cyan)
- gui.setButton(4, 4, "FAVORI 1", colors.lightBlue, colors.blue)
- gui.setButton(4, 8, "FAVORI 2", colors.lightBlue, colors.blue)
- gui.setButton(4, 12, "FAVORI 3", colors.lightBlue, colors.blue)
- gui.setButton(4, 16, "FAVORI 4", colors.lightBlue, colors.blue)
- gui.setButton(midX-5, 4, "FAVORI 5", colors.lightBlue, colors.blue)
- gui.setButton(midX-5, 8, "FAVORI 6", colors.lightBlue, colors.blue)
- gui.setButton(midX-4, 12, "CANCEL", colors.lightBlue, colors.blue)
- gui.render(true)
- selectFavori()
- end
- local function selectOption()
- local input = gui.getSelection()
- if input == 1 then
- drawFavori()
- elseif input == 2 then
- shell.run("desktop/autoupdater")
- elseif input == 3 then
- gui.clear()
- term.setCursorPos(2, 4)
- write("Owner, coder and technical support : Bjornir90")
- term.setCursorPos(4, 6)
- write("Thanks to :")
- term.setCursorPos(6, 7)
- write("Dan200 and Cloudy for computercraft\n and everything that come with it")
- term.setCursorPos(6, 9)
- write("ArchAngel075 and Oct125 for helping me\n fix my GUI API")
- term.setCursorPos(6, 11)
- write("Lesiteduzero.com for giving me the bases\n in (go here http://www.youtube.com/watch?v=dQw4w9WgXcQ) programmation")
- term.setCursorPos(6, 15)
- write("The amazing community which helped me a lot")
- term.setCursorPos(2, 17)
- print("Simple desk inDev, please report errors\n to the technical support") --Line to edit for versions
- term.setTextColor(colors.red)
- print("Press any key to continue")
- term.setTextColor(colors.white)
- local event = os.pullEvent("key")
- drawOption()
- elseif input == 4 then
- drawDesk()
- end
- end
- function drawOption()
- gui.clear()
- gui.clearBuffer()
- gui.fill(1,1,termX,termY,colors.cyan)
- gui.setButton(midX-4, 4, "FAVORIS", colors.lightBlue, colors.blue)
- gui.setHeader(" Options", colors.lightBlue)
- gui.setButton(midX-7, 8, "AUTO-UPDATE", colors.lightBlue, colors.blue)
- gui.setButton(midX-4, 12, "CREDIT", colors.lightBlue, colors.blue)
- gui.setButton(midX-4, 16, "CANCEL", colors.lightBlue, colors.blue)
- gui.render(true)
- gui.clearBuffer()
- selectOption()
- end
- local function selectOptionDev()
- local input = gui.getSelection()
- if input == 1 then
- os.unloadAPI("gui")
- fs.delete("desktop")
- shell.run("desk")
- elseif input == 2 then
- os.unloadAPI("gui")
- fs.delete("desktop")
- elseif input == 3
- then
- drawDesk()
- elseif input == 4 then
- os.reboot()
- elseif input == 5 then
- shell.run("desktop/edit", "desktop/favoris")
- elseif input == 6 then
- shell.run("desktop/edit", "guiAPI/log")
- elseif input == 7 then
- os.loadAPI("/desktop/gui")
- gui.initializeLog()
- local _path = shell.getRunningProgram()
- shell.setAlias("desk", _path)
- local termX, termY = term.getSize()
- local midX, midY = termX/2-0.5, termY/2-0.5
- --Format : table {fav1{infos} fav2{infos}}
- local favF = fs.open("/desktop/favoris", "r")
- local favS = favF.readAll()
- favT = textutils.unserialize(favS)
- fav1 = favT[1]
- fav2 = favT[2]
- fav3 = favT[3]
- fav4 = favT[4]
- fav5 = favT[5]
- fav6 = favT[6]
- favF.close()
- drawOptionDev()
- end
- end
- function drawOptionDev()
- gui.clear()
- gui.clearBuffer()
- gui.setButton(2, 2, "Remake disk")
- gui.setButton(2, 5, "Unload disk")
- gui.setButton(2, 8, "Cancel")
- gui.setButton(2, 11, "Reboot")
- gui.setButton(18, 5, "Edit favoris file")
- gui.setButton(2, 14, "Open GUI log")
- gui.setButton(2,17, "Reload disk")
- gui.render()
- selectOptionDev()
- end
- local function selectDesk()
- local input = gui.getSelection()
- if input == 1 then
- gui.clear()
- os.unloadAPI("gui")
- shell.run("/desktop/shell")
- elseif input == 2 then
- local clickedB = gui.popUp("Edit a program", "You are going to chose which program to edit", 1, midY, colors.lightBlue, colors.blue, "Cancel", "Next")
- if clickedB == 1 then
- drawDesk()
- elseif clickedB == 2 then
- gui.clear()
- write("Choose your program : ")
- local program = tostring(read())
- gui.clear()
- os.unloadAPI("gui")
- shell.run("desktop/edit", program)
- end
- elseif input == 3 then
- local toShut = gui.popUp("Shutdown", "Are you sure you want to shutdown ?", midX-12, midY, colors.lightBlue, colors.blue, "No", "Yes")
- if toShut == 1 then
- drawDesk()
- elseif toShut == 2 then
- gui.clear()
- print("Thanks for using Simple Desk :\) ")
- sleep(1.4)
- os.shutdown()
- end
- elseif input == 4 then
- drawOption()
- elseif input == 5 then
- --drawOptionDev()
- os.reboot()
- elseif input == 6 then
- shell.run(fav1.path, fav1.arg)
- elseif input == 7 then
- shell.run(fav2.path, fav2.arg)
- elseif input == 8 then
- shell.run(fav3.path, fav3.arg)
- elseif input == 9 then
- shell.run(fav4.path, fav4.arg)
- elseif input == 10 then
- shell.run(fav5.path, fav5.arg)
- elseif input == 11 then
- shell.run(fav6.path, fav6.arg)
- end
- end
- function drawDesk()
- gui.clear()
- gui.clearBuffer()
- gui.fill(1, 1, termX, termY, colors.cyan) --Wallpaper
- gui.setButton(midX-4, 4, "SHELL", colors.lightBlue, colors.blue)
- gui.setButton(midX-3, 8, "EDIT", colors.lightBlue, colors.blue)
- gui.setButton(midX-5, 12,"SHUTDOWN", colors.lightBlue, colors.blue)
- gui.setButton(midX-4, 16, "OPTION", colors.lightBlue, colors.blue)
- --gui.setButton(midX+6, 16, "OPTIONSDEV", colors.lightBlue, colors.blue)
- gui.setButton(midX+6, 16, "REBOOT", colors.lightBlue, colors.blue)
- term.setBackgroundColor(colors.blue)
- gui.setHeader(" Simple Desk inDev", colors.lightBlue) --Line to edit for version
- term.setBackgroundColor(colors.black)
- if fav1 ~= "0" then
- gui.setButton(2, 4, fav1.text, colors.lightBlue, colors.blue)
- if fav2 ~= "0" then
- gui.setButton(2, 8, fav2.text, colors.lightBlue, colors.blue)
- if fav3 ~= "0" then
- gui.setButton(2, 12, fav3.text, colors.lightBlue, colors.blue)
- if fav4 ~= "0" then
- gui.setButton(2, 16, fav4.text, colors.lightBlue, colors.blue)
- if fav5 ~= "0" then
- gui.setButton(35, 4, fav5.text, colors.lightBlue, colors.blue)
- if fav6 ~= "0" then
- gui.setButton(35, 8, fav6.text, colors.lightBlue, colors.blue)
- end
- end
- end
- end
- end
- end
- gui.render(true)
- selectDesk()
- end
- drawDesk()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement