Advertisement
Bjornir90

Simple desk

Jun 19th, 2013
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 51.17 KB | None | 0 0
  1. --First launch
  2. if not fs.exists("desktop") then
  3.  fs.makeDir("desktop")
  4.  shell.run("clear")
  5.  print("Installing API, this could take some time")
  6.  guiApi = fs.open("/desktop/gui", "w")
  7.  guiApi.write([[function getVersion()
  8.  return "1.6.1indev2"
  9. end
  10.  
  11. --Declaration of variables
  12. local runButton = {}
  13. local runHeader = {}
  14. local coordObject = {}
  15. local list = {}
  16. local sizeButtons = nil
  17. local tickBoxT = {}
  18. local path = "/guiAPI/log"
  19. --End of declaration
  20.  
  21. function initializeLog()
  22.  if fs.isDir("/guiAPI") then
  23.   local logFile = fs.open(path, "w")
  24.   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")
  25.   logFile.close()
  26.   isInitialized = true
  27.  else
  28.   if fs.exists("/guiAPI") then
  29.    error("Gui API : guiAPI is not a directory, cannot create log file, please delete this file or rename it")
  30.   else
  31.    fs.makeDir("/guiAPI")
  32.    initializeLog()
  33.   end
  34.  end
  35. end
  36.  
  37.  
  38. function buttonClick(x, y, a, b)
  39.  if type(x) ~= "number" or type(y) ~= "number" or type(a) ~= "number" or type(b) ~= "number" then
  40.   if isInitialized == true then
  41.    local logFile = fs.open(path, "a")
  42.    logFile.write("[!]Detection of click ended with level 2 error\n")
  43.    logFile.close()
  44.   end
  45.   error("Number expected for x, y, a or b values", 2)
  46.  end
  47.  event, clic, x1, y1 = os.pullEvent("mouse_click")
  48.  if x1 <= a and x1 >= x and y1 <= b and y1 >= y then
  49.  return "true"
  50.  else
  51.  return "false"
  52.  end
  53. end
  54.  
  55. function buttonWaitClick(x, y, a, b)
  56.  clicked = "false"
  57.  while clicked == "false" do
  58.   if type(x) ~= "number" or type(y) ~= "number" or type(a) ~= "number" or type(b) ~= "number" then
  59.    if isInitialized == true then
  60.     local logFile = fs.open(path, "a")
  61.     logFile.write("[!]Detection of click ended with level 2 error\n")
  62.     logFile.close()
  63.    end
  64.    error("Number expected for x, y, a or b values", 2)
  65.   end
  66.   event, button, x1, y1 = os.pullEvent("mouse_click")
  67.   if  x1 <= a and x1 >= x and y1 <= b and y1 >= y then
  68.    clicked = "true"
  69.   else
  70.    clicked = "false"
  71.   end
  72.  end
  73.  return clicked
  74. end
  75.  
  76. function setButton(x, y, text, colorLine, color, clears)
  77.  if type(x) ~= "number" or type(y) ~= "number" or type(text) ~= "string" then
  78.   if isInitialized == true then
  79.    local logFile = fs.open(path, "a")
  80.    logFile.write("[!]Setting of button ended with level 2 error\n")
  81.  
  82.    logFile.close()
  83.   end
  84.   error("Number expected for x and y values or string expected for text value", 2)
  85.  end
  86. if clears == nil then
  87.  local clears = "false"
  88. end
  89. if tostring(clears) == "true" then
  90.  clear()
  91. end
  92. if colorLine then
  93.  colorL = colorLine
  94. else
  95.  colorL = colors.white
  96. end
  97. if color then
  98.  colorB = color
  99. else
  100.  colorB = colors.black
  101. end
  102.  local sizeText = #tostring(text)
  103.  a = x+1+sizeText
  104.  b = y+2
  105.  table.insert(runButton, x)
  106.  table.insert(runButton, y)
  107.  table.insert(runButton, a)
  108.  table.insert(runButton, b)
  109.  table.insert(runButton, text)
  110.  table.insert(runButton, colorL)
  111.  table.insert(runButton, colorB)
  112.  table.insert(coordObject, {x,y,a,b,"button"})
  113.  if isInitialized == true then
  114.   local logFile = fs.open(path, "a")
  115.   logFile.write("Set a button at "..x.."X and at "..y.."Y with text "..text.."\n")
  116.   logFile.close()
  117.  end
  118. end
  119.  
  120. function drawButton(x, y, a, b, text, colorLine, color)
  121.  if type(x) ~= "number" or type(y) ~= "number" or type(a) ~= "number" or type(b) ~= "number" or type(text) ~= "string" then
  122.   if isInitialized == true then
  123.    local logFile = fs.open(path, "a")
  124.    logFile.write("[!]Drawing of button ended with level 2 error\n")
  125.  
  126.    logFile.close()
  127.   end
  128.   error("Number expected for x and y and a and b values or string expected for text value")
  129.  end
  130.  if colorLine then
  131.   colorL = colorLine
  132.  else
  133.   colorL = colors.white
  134.  end
  135.  if color then
  136.   colorB = color
  137.  else
  138.   colorB = colors.black
  139.  end
  140.  paintutils.drawLine(x, y, a, y, colorL) --draw the top line
  141.  paintutils.drawLine(x, y, x, b, colorL) --draw the left line
  142.  paintutils.drawLine(a, y, a, b, colorL) --draw the right line
  143.  paintutils.drawLine(x, b, a, b, colorL) --draw the bottom line
  144.  fill(x+1, y+1, a-1, b-1, colorB)
  145.  term.setCursorPos(x+1, y+1)--Print the text in the button
  146.  term.write(text)
  147.  if isInitialized == true then
  148.   local logFile = fs.open(path, "a")
  149.   logFile.write("Drawn a button at "..x.."X and at "..y.."Y with text "..text.."\n")
  150.   logFile.close()
  151.  end
  152. end
  153.  
  154. function setHeader(text, colorLine)
  155.  if type(text) ~= "string" then
  156.   if isInitialized == true then
  157.    local logFile = fs.open(path, "a")
  158.    logFile.write("[!]Setting of header ended with level 2 error\n")
  159.  
  160.    logFile.close()
  161.   end
  162.   error("String expected for text value", 2)
  163.  end
  164.  if colorLine then
  165.   colorL = colorLine
  166.  else
  167.   colorL = colors.white
  168.  end
  169.  local text = tostring(text)
  170.  table.insert(runHeader, text)
  171.  table.insert(runHeader, colorL)
  172.  if isInitialized == true then
  173.   local logFile = fs.open(path, "a")
  174.   logFile.write("Set a header with text "..text.."\n")
  175.   logFile.close()
  176.  end
  177. end
  178.  
  179. function drawHeader(text, colorLine)
  180.  if type(text) ~= "string" then
  181.   if isInitialized == true then
  182.    local logFile = fs.open(path, "a")
  183.    logFile.write("[!]Drawing of header ended with level 1 error\n")
  184.  
  185.    logFile.close()
  186.   end
  187.   error("String expected for text value")
  188.  end
  189.  if colorLine then
  190.   colorL = colorLine
  191.  else
  192.   colorL = colors.white
  193.  end
  194.  xScreen, yScreen = term.getSize()
  195.  paintutils.drawLine(1, 1, xScreen, 1)
  196.  term.setCursorPos(1, 1)
  197.  write(text)
  198.  paintutils.drawLine(1, 2, xScreen, 2, colorL)
  199.  term.setCursorPos(1, 3)
  200.  term.setBackgroundColor(colors.black)
  201.  if isInitialized == true then
  202.   local logFile = fs.open(path, "a")
  203.   logFile.write("Drawn a header with text "..text.."\n")
  204.   logFile.close()
  205.  end
  206. end
  207.  
  208. function clear()
  209.  term.setCursorPos(1, 1)
  210.  term.setBackgroundColor(colors.black)
  211.  term.setTextColor(colors.white)
  212.  term.clear()
  213.  if isInitialized == true then
  214.   local logFile = fs.open(path, "a")
  215.   logFile.write("Cleared the screen\n")
  216.   logFile.close()
  217.  end
  218. end
  219.  
  220. function fill(x, y, a, b, color)
  221.  term.setBackgroundColor(color)
  222.  oldY = y
  223.  for x=x, a do
  224.   for y=y, b do
  225.    term.setCursorPos(x,y)
  226.    print(" ")
  227.   end
  228.  end
  229. end
  230.  
  231. function render(shouldClear)
  232.  if isInitialized == true then
  233.   local logFile = fs.open(path, "a")
  234.   logFile.write("----------------Started rendering------------------\n")
  235.   logFile.close()
  236.  end
  237.  if not shouldClear then
  238.   clear()
  239.  end
  240.  local sizeBut = #runButton
  241.  local sizeHead = #runHeader
  242.  local sizeTickBox = #tickBoxT
  243.  for x=1, sizeBut, 7 do
  244.   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
  245.  end
  246.  for x=1, sizeHead, 2 do
  247.   drawHeader(runHeader[x], runHeader[x+1]) --Run the function to redraw it
  248.  end
  249.  for x=1, sizeTickBox, 4 do
  250.   drawTickBox(tickBoxT[x], tickBoxT[x+1], tickBoxT[x+2], tickBoxT[x+3])
  251.  end
  252.  if reset == nil then
  253.   local reset = "false"
  254.  end
  255.  if tostring(reset) == "true" then
  256.   clearBuffer()
  257.  end
  258.  if isInitialized == true then
  259.   local logFile = fs.open(path, "a")
  260.   logFile.write("--------------Succesfully rendered-----------------\n")
  261.   logFile.close()
  262.  end
  263. end
  264.  
  265. function clearBuffer()
  266.  runButton = {}
  267.  runHeader = {}
  268.  tickBoxT = {}
  269.  if isInitialized == true then
  270.   local logFile = fs.open(path, "a")
  271.   logFile.write("Cleared buffer\n")
  272.   logFile.close()
  273.  end
  274. end
  275.  
  276. function getSelection()
  277.  local sizeObject = #coordObject
  278.  detectObject = {}
  279.  for x=1, sizeObject do
  280.   table.insert(detectObject, function() buttonWaitClick(coordObject[x][1], coordObject[x][2], coordObject[x][3], coordObject[x][4]) end)
  281.  end
  282.  local input = parallel.waitForAny(unpack(detectObject))
  283.  coordObject = {}
  284.  if isInitialized == true then
  285.   local logFile = fs.open(path, "a")
  286.   logFile.write("Got user selection : "..input.."\n")
  287.   logFile.close()
  288.  end
  289.  return input
  290. end
  291.  
  292. function animation(x, y, a, b, colorL)
  293.  m = a-x
  294.  n = x-a
  295.  if isInitialized == true then
  296.   local logFile = fs.open(path, "a")
  297.   logFile.write("-----------**Started pop-up animation**------------\nX : "..x.." Y : "..y.." ColorLine : "..colorL.."\n")
  298.   logFile.close()
  299.  end
  300.  paintutils.drawLine(x+3, y+3, a-3, y-3, colorL) --draw the top line
  301.  paintutils.drawLine(x+3, y+3, x-3, b-3, colorL) --draw the left line
  302.  paintutils.drawLine(a+3, y+3, a-3, b-3, colorL) --draw the right line
  303.  paintutils.drawLine(x+3, b+3, a-3, b-3, colorL) --draw the bottom line
  304.  sleep(0.05)
  305.  render()
  306.  paintutils.drawLine(x+2, y+2, a-2, y-2, colorL) --draw the top line
  307.  paintutils.drawLine(x+2, y+2, x-2, b-2, colorL) --draw the left line
  308.  paintutils.drawLine(a+2, y+2, a-2, b-2, colorL) --draw the right line
  309.  paintutils.drawLine(x+2, b+2, a-2, b-2, colorL) --draw the bottom line
  310.  sleep(0.05)
  311.  render()
  312.  paintutils.drawLine(x+1, y+1, a-1, y-1, colorL) --draw the top line
  313.  paintutils.drawLine(x+1, y+1, x-1, b-1, colorL) --draw the left line
  314.  paintutils.drawLine(a+1, y+1, a-1, b-1, colorL) --draw the right line
  315.  paintutils.drawLine(x+1, b+1, a-1, b-1, colorL) --draw the bottom line
  316.  sleep(0.05)
  317.  render()
  318.  if isInitialized == true then
  319.   local logFile = fs.open(path, "a")
  320.   logFile.write("-----**Successfully ended pop-up animation**-------\n")
  321.   logFile.close()
  322.  end
  323. end
  324.  
  325. function popUp(title, text, x, y, colorLine, color, textB1, textB2)
  326.  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
  327.   if isInitialized == true then
  328.    local logFile = fs.open(path, "a")
  329.    logFile.write("[!]Drawing of pop-up ended with level 2 error\n[!]Skipped drawing of pop-up\n")
  330.    logFile.close()
  331.   end
  332.   skip = true
  333.  else
  334.   skip = false
  335.  end
  336. if not skip then
  337.  render() --Render so the things drawn behind appears
  338.  sizeTitle = #title
  339.  sizeText = #text
  340. if textB2 ~= nil then
  341.  sizeB1 = #textB1
  342.  sizeB2 = #textB2
  343.  sizeButtons = sizeB2+sizeB1
  344.  mustButton = "true"
  345. else
  346.  sizeButtons = 0
  347.  mustButton = "false"
  348. end
  349.  if sizeTitle > sizeText and sizeTitle > sizeButtons then
  350.   greaterS = sizeTitle
  351.  elseif sizeText > sizeTitle and sizeText > sizeButtons then
  352.   greaterS = sizeText
  353.  else
  354.   greaterS = sizeButtons
  355.  end
  356. if colorLine then
  357.  local colorL = colorLine
  358. else
  359.  local colorL = colors.white
  360. end
  361. if color then
  362.  local colorB = color
  363. else
  364.  local colorB = colors.black
  365. end
  366. local a = x + greaterS + 1
  367. local b = y + 4
  368. animation(x, y, a, b, colorL)
  369. paintutils.drawLine(x, y, a, y, colorL) --draw the top line
  370. paintutils.drawLine(x, y, x, b, colorL) --draw the left line
  371. paintutils.drawLine(a, y, a, b, colorL) --draw the right line
  372. paintutils.drawLine(x, b, a, b, colorL) --draw the bottom line
  373. fill(x+1, y+1, a-1, b-1, colorB)
  374. term.setCursorPos(x+1, y+1) --Print the title
  375. term.write(title)
  376. paintutils.drawLine(x, y+2, a, b-2, colorL) --Draw the line for the title
  377. term.setBackgroundColor(colorB)
  378. term.setCursorPos(x+1, y+3)
  379. term.write(text) --Print the text
  380.  if isInitialized == true then
  381.   local logFile = fs.open(path, "a")
  382.   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")
  383.   logFile.close()
  384.  end
  385. if mustButton == "true" then
  386.  local midX = math.ceil((a-x)/2)
  387.  term.setCursorPos(midX-1-sizeB1, b)
  388.  term.write(textB1)
  389.  term.setCursorPos(midX+1, b)
  390.  term.write(textB2)
  391.  clickedB = parallel.waitForAny(function() buttonWaitClick(midX-sizeB1-1, b, midX-1, b) end, function() buttonWaitClick(midX+1, b, midX+sizeB2+1, b) end)
  392.  return clickedB
  393. else
  394.  clickedPopup = buttonWaitClick(x, y, a, b)
  395.  return clickedPopup
  396. end
  397. else
  398.  return
  399. end
  400. end
  401. ]])
  402. guiApi.close()
  403. print("API installed")
  404. print("Installing shell")
  405. local shellF = fs.open("desktop/shell", "w")
  406. shellF.write([[
  407.  
  408. local parentShell = shell
  409.  
  410. local bExit = false
  411. local sDir = (parentShell and parentShell.dir()) or ""
  412. local sPath = (parentShell and parentShell.path()) or ".:/rom/programs"
  413. local tAliases = (parentShell and parentShell.aliases()) or {}
  414. local tProgramStack = {}
  415.  
  416. local shell = {}
  417. local tEnv = {
  418.     ["shell"] = shell,
  419. }
  420.  
  421. -- Colours
  422. local promptColour, textColour, bgColour
  423. if term.isColour() then
  424.     promptColour = colours.yellow
  425.     textColour = colours.white
  426.     bgColour = colours.black
  427. else
  428.     promptColour = colours.white
  429.     textColour = colours.white
  430.     bgColour = colours.black
  431. end
  432.  
  433.  
  434. local function run( _sCommand, ... )
  435.     --Modified by Bjornir90
  436.     if _sCommand == "exit" then
  437.      shell.run("desk")
  438.     end
  439.     local sPath = shell.resolveProgram( _sCommand )
  440.     if sPath ~= nil then
  441.         tProgramStack[#tProgramStack + 1] = sPath
  442.         local result = os.run( tEnv, sPath, ... )
  443.         tProgramStack[#tProgramStack] = nil
  444.         return result
  445.     else
  446.         printError( "No such program" )
  447.         return false
  448.     end
  449. end
  450.  
  451. local function runLine( _sLine )
  452.     local tWords = {}
  453.     for match in string.gmatch( _sLine, "[^ \t]+" ) do
  454.         table.insert( tWords, match )
  455.     end
  456.  
  457.     local sCommand = tWords[1]
  458.     if sCommand then
  459.         return run( sCommand, unpack( tWords, 2 ) )
  460.     end
  461.     return false
  462. end
  463.  
  464. -- Install shell API
  465. function shell.run( ... )
  466.     return runLine( table.concat( { ... }, " " ) )
  467. end
  468.  
  469. function shell.exit()
  470.     bExit = true
  471. end
  472.  
  473. function shell.dir()
  474.     return sDir
  475. end
  476.  
  477. function shell.setDir( _sDir )
  478.     sDir = _sDir
  479. end
  480.  
  481. function shell.path()
  482.     return sPath
  483. end
  484.  
  485. function shell.setPath( _sPath )
  486.     sPath = _sPath
  487. end
  488.  
  489. function shell.resolve( _sPath )
  490.     local sStartChar = string.sub( _sPath, 1, 1 )
  491.     if sStartChar == "/" or sStartChar == "\\" then
  492.         return fs.combine( "", _sPath )
  493.     else
  494.         return fs.combine( sDir, _sPath )
  495.     end
  496. end
  497.  
  498. function shell.resolveProgram( _sCommand )
  499.     -- Substitute aliases firsts
  500.     if tAliases[ _sCommand ] ~= nil then
  501.         _sCommand = tAliases[ _sCommand ]
  502.     end
  503.  
  504.     -- If the path is a global path, use it directly
  505.     local sStartChar = string.sub( _sCommand, 1, 1 )
  506.     if sStartChar == "/" or sStartChar == "\\" then
  507.         local sPath = fs.combine( "", _sCommand )
  508.         if fs.exists( sPath ) and not fs.isDir( sPath ) then
  509.             return sPath
  510.         end
  511.         return nil
  512.     end
  513.    
  514.     -- Otherwise, look on the path variable
  515.     for sPath in string.gmatch(sPath, "[^:]+") do
  516.         sPath = fs.combine( shell.resolve( sPath ), _sCommand )
  517.         if fs.exists( sPath ) and not fs.isDir( sPath ) then
  518.             return sPath
  519.         end
  520.     end
  521.    
  522.     -- Not found
  523.     return nil
  524. end
  525.  
  526. function shell.programs( _bIncludeHidden )
  527.     local tItems = {}
  528.    
  529.     -- Add programs from the path
  530.     for sPath in string.gmatch(sPath, "[^:]+") do
  531.         sPath = shell.resolve( sPath )
  532.         if fs.isDir( sPath ) then
  533.             local tList = fs.list( sPath )
  534.             for n,sFile in pairs( tList ) do
  535.                 if not fs.isDir( fs.combine( sPath, sFile ) ) and
  536.                    (_bIncludeHidden or string.sub( sFile, 1, 1 ) ~= ".") then
  537.                     tItems[ sFile ] = true
  538.                 end
  539.             end
  540.         end
  541.     end
  542.  
  543.     -- Sort and return
  544.     local tItemList = {}
  545.     for sItem, b in pairs( tItems ) do
  546.         table.insert( tItemList, sItem )
  547.     end
  548.     table.sort( tItemList )
  549.     return tItemList
  550. end
  551.  
  552. function shell.getRunningProgram()
  553.     if #tProgramStack > 0 then
  554.         return tProgramStack[#tProgramStack]
  555.     end
  556.     return nil
  557. end
  558.  
  559. function shell.setAlias( _sCommand, _sProgram )
  560.     tAliases[ _sCommand ] = _sProgram
  561. end
  562.  
  563. function shell.clearAlias( _sCommand )
  564.     tAliases[ _sCommand ] = nil
  565. end
  566.  
  567. function shell.aliases()
  568.     -- Add aliases
  569.     local tCopy = {}
  570.     for sAlias, sCommand in pairs( tAliases ) do
  571.         tCopy[sAlias] = sCommand
  572.     end
  573.     return tCopy
  574. end
  575.    
  576. term.setBackgroundColor( bgColour )
  577. term.setTextColour( promptColour )
  578. print( os.version() )
  579. term.setTextColour( textColour )
  580.  
  581. -- If this is the toplevel shell, run the startup programs
  582. if parentShell == nil then
  583.     -- Run the startup from the ROM first
  584.     local sRomStartup = shell.resolveProgram( "/rom/startup" )
  585.     if sRomStartup then
  586.         shell.run( sRomStartup )
  587.     end
  588.    
  589.     -- Then run the user created startup, from the disks or the root
  590.     local sUserStartup = shell.resolveProgram( "/startup" )
  591.     for n,sSide in pairs( peripheral.getNames() ) do
  592.     --for n,sSide in pairs( redstone.getSides() ) do
  593.         if disk.isPresent( sSide ) and disk.hasData( sSide ) then
  594.             local sDiskStartup = shell.resolveProgram( fs.combine(disk.getMountPath( sSide ), "startup") )
  595.             if sDiskStartup then
  596.                 sUserStartup = sDiskStartup
  597.                 break
  598.             end
  599.         end
  600.     end
  601.    
  602.     if sUserStartup then
  603.         shell.run( sUserStartup )
  604.     end
  605. end
  606.  
  607. -- Run any programs passed in as arguments
  608. local tArgs = { ... }
  609. if #tArgs > 0 then
  610.     shell.run( ... )
  611. end
  612.  
  613. -- Read commands and execute them
  614. local tCommandHistory = {}
  615. while not bExit do
  616.     term.setBackgroundColor( bgColour )
  617.     term.setTextColour( promptColour )
  618.     write( shell.dir() .. "> " )
  619.     term.setTextColour( textColour )
  620.  
  621.     local sLine = read( nil, tCommandHistory )
  622.     table.insert( tCommandHistory, sLine )
  623.     runLine( sLine )
  624. end
  625.  
  626. -- If this is the toplevel shell, run the shutdown program
  627. if parentShell == nil then
  628.     if shell.resolveProgram( "shutdown" ) then
  629.         shell.run( "shutdown" )
  630.     end
  631.     os.shutdown() -- just in case
  632. end
  633. ]])
  634. shellF.close()
  635. print("Installing edit")
  636. local editF = fs.open("desktop/edit", "w")
  637. editF.write([[ -- Get file to edit
  638. local tArgs = { ... }
  639. if #tArgs == 0 then
  640.         print( "Usage: edit <path>" )
  641.         return
  642. end
  643.  
  644. -- Error checking
  645. local sPath = shell.resolve( tArgs[1] )
  646. local bReadOnly = fs.isReadOnly( sPath )
  647. if fs.exists( sPath ) and fs.isDir( sPath ) then
  648.         print( "Cannot edit a directory." )
  649.         return
  650. end
  651.  
  652. local x,y = 1,1
  653. local w,h = term.getSize()
  654. local scrollX, scrollY = 0,0
  655.  
  656. local tLines = {}
  657. local bRunning = true
  658.  
  659. -- Colours
  660. local highlightColour, keywordColour, commentColour, textColour, bgColour
  661. if term.isColour() then
  662.         bgColour = colours.black
  663.         textColour = colours.white
  664.         highlightColour = colours.yellow
  665.         keywordColour = colours.yellow
  666.         commentColour = colours.lime
  667.         stringColour = colours.red
  668. else
  669.         bgColour = colours.black
  670.         textColour = colours.white
  671.         highlightColour = colours.white
  672.         keywordColour = colours.white
  673.         commentColour = colours.white
  674.         stringColour = colours.white
  675. end
  676.  
  677. -- Menus
  678. local bMenu = false
  679. local nMenuItem = 1
  680. local tMenuItems = {"Save", "Exit", "Print"}
  681. local sStatus = "Press Ctrl to access menu"
  682.  
  683. local function load(_sPath)
  684.         tLines = {}
  685.         if fs.exists( _sPath ) then
  686.                 local file = io.open( _sPath, "r" )
  687.                 local sLine = file:read()
  688.                 while sLine do
  689.                         table.insert( tLines, sLine )
  690.                         sLine = file:read()
  691.                 end
  692.                 file:close()
  693.         end
  694.        
  695.         if #tLines == 0 then
  696.                 table.insert( tLines, "" )
  697.         end
  698. end
  699.  
  700. local function save( _sPath )
  701.         -- Create intervening folder
  702.         local sDir = sPath:sub(1, sPath:len() - fs.getName(sPath):len() )
  703.         if not fs.exists( sDir ) then
  704.                 fs.makeDir( sDir )
  705.         end
  706.  
  707.         -- Save
  708.         local file = nil
  709.         local function innerSave()
  710.                 file = fs.open( _sPath, "w" )
  711.                 if file then
  712.                         for n, sLine in ipairs( tLines ) do
  713.                                 file.write( sLine .. "\n" )
  714.                         end
  715.                 else
  716.                         error( "Failed to open ".._sPath )
  717.                 end
  718.         end
  719.        
  720.         local ok = pcall( innerSave )
  721.         if file then
  722.                 file.close()
  723.         end
  724.         return ok
  725. end
  726.  
  727. local tKeywords = {
  728.         ["and"] = true,
  729.         ["break"] = true,
  730.         ["do"] = true,
  731.         ["else"] = true,
  732.         ["elseif"] = true,
  733.         ["end"] = true,
  734.         ["false"] = true,
  735.         ["for"] = true,
  736.         ["function"] = true,
  737.         ["if"] = true,
  738.         ["in"] = true,
  739.         ["local"] = true,
  740.         ["nil"] = true,
  741.         ["not"] = true,
  742.         ["or"] = true,
  743.         ["repeat"] = true,
  744.         ["return"] = true,
  745.         ["then"] = true,
  746.         ["true"] = true,
  747.         ["until"]= true,
  748.         ["while"] = true,
  749. }
  750.  
  751. local function tryWrite( sLine, regex, colour )
  752.         local match = string.match( sLine, regex )
  753.         if match then
  754.                 if type(colour) == "number" then
  755.                         term.setTextColour( colour )
  756.                 else
  757.                         term.setTextColour( colour(match) )
  758.                 end
  759.                 term.write( match )
  760.                 term.setTextColour( textColour )
  761.                 return string.sub( sLine, string.len(match) + 1 )
  762.         end
  763.         return nil
  764. end
  765.  
  766. local function writeHighlighted( sLine )
  767.         while string.len(sLine) > 0 do
  768.                 sLine =
  769.                         tryWrite( sLine, "^%-%-%[%[.-%]%]", commentColour ) or
  770.                         tryWrite( sLine, "^%-%-.*", commentColour ) or
  771.                         tryWrite( sLine, "^\".-[^\\]\"", stringColour ) or
  772.                         tryWrite( sLine, "^\'.-[^\\]\'", stringColour ) or
  773.                         tryWrite( sLine, "^%[%[.-%]%]", stringColour ) or
  774.                         tryWrite( sLine, "^[%w_]+", function( match )
  775.                                 if tKeywords[ match ] then
  776.                                         return keywordColour
  777.                                 end
  778.                                 return textColour
  779.                         end ) or
  780.                         tryWrite( sLine, "^[^%w_]", textColour )
  781.         end
  782. end
  783.  
  784. local function redrawText()
  785.         for y=1,h-1 do
  786.                 term.setCursorPos( 1 - scrollX, y )
  787.                 term.clearLine()
  788.  
  789.                 local sLine = tLines[ y + scrollY ]
  790.                 if sLine ~= nil then
  791.                         writeHighlighted( sLine )
  792.                 end
  793.         end
  794.         term.setCursorPos( x - scrollX, y - scrollY )
  795. end
  796.  
  797. local function redrawLine(_nY)
  798.         local sLine = tLines[_nY]
  799.         term.setCursorPos( 1 - scrollX, _nY - scrollY )
  800.         term.clearLine()
  801.         writeHighlighted( sLine )
  802.         term.setCursorPos( x - scrollX, _nY - scrollY )
  803. end
  804.  
  805. local function setLeftStatus()
  806. end
  807.  
  808. local function redrawMenu()
  809.     term.setCursorPos( 1, h )
  810.         term.clearLine()
  811.  
  812.         local sLeft, sRight
  813.         local nLeftColour, nLeftHighlight1, nLeftHighlight2
  814.         if bMenu then
  815.                 local sMenu = ""
  816.                 for n,sItem in ipairs( tMenuItems ) do
  817.                         if n == nMenuItem then
  818.                                 nLeftHighlight1 = sMenu:len() + 1
  819.                                 nLeftHighlight2 = sMenu:len() + sItem:len() + 2
  820.                         end
  821.                         sMenu = sMenu.." "..sItem.." "
  822.                 end
  823.                 sLeft = sMenu
  824.                 nLeftColour = textColour
  825.         else
  826.                 sLeft = sStatus
  827.                 nLeftColour = highlightColour
  828.         end
  829.        
  830.         -- Left goes last so that it can overwrite the line numbers.
  831.         sRight = "Ln "..y
  832.         term.setTextColour( highlightColour )
  833.         term.setCursorPos( w-sRight:len() + 1, h )
  834.         term.write(sRight)
  835.  
  836.         sRight = tostring(y)
  837.         term.setTextColour( textColour )
  838.         term.setCursorPos( w-sRight:len() + 1, h )
  839.         term.write(sRight)
  840.  
  841.         if sLeft then
  842.                 term.setCursorPos( 1, h )
  843.                 term.setTextColour( nLeftColour )
  844.                 term.write(sLeft)              
  845.                 if nLeftHighlight1 then
  846.                         term.setTextColour( highlightColour )
  847.                         term.setCursorPos( nLeftHighlight1, h )
  848.                         term.write( "[" )
  849.                         term.setCursorPos( nLeftHighlight2, h )
  850.                         term.write( "]" )
  851.                 end
  852.                 term.setTextColour( textColour )
  853.         end
  854.        
  855.         -- Cursor highlights selection
  856.         term.setCursorPos( x - scrollX, y - scrollY )
  857. end
  858.  
  859. local tMenuFuncs = {
  860.         Save=function()
  861.                 if bReadOnly then
  862.                         sStatus = "Access denied"
  863.                 else
  864.                         local ok, err = save( sPath )
  865.                         if ok then
  866.                                 sStatus="Saved to "..sPath
  867.                         else
  868.                                 sStatus="Error saving to "..sPath
  869.                         end
  870.                 end
  871.                 redrawMenu()
  872.         end,
  873.         Print=function()
  874.                 local sPrinterSide = nil
  875.                 for n,sName in ipairs(peripheral.getNames()) do
  876.                         if peripheral.isPresent(sName) and peripheral.getType(sName) == "printer" then
  877.                                 sPrinterSide = sName
  878.                                 break
  879.                         end
  880.                 end
  881.                
  882.                 if not sPrinterSide then
  883.                         sStatus = "No printer attached"
  884.                         return
  885.                 end
  886.  
  887.                 local nPage = 0
  888.                 local sName = fs.getName( sPath )
  889.                 local printer = peripheral.wrap(sPrinterSide)
  890.                 if printer.getInkLevel() < 1 then
  891.                         sStatus = "Printer out of ink"
  892.                         return
  893.                 elseif printer.getPaperLevel() < 1 then
  894.                         sStatus = "Printer out of paper"
  895.                         return
  896.                 end
  897.                
  898.                 local terminal = {
  899.                         getCursorPos = printer.getCursorPos,
  900.                         setCursorPos = printer.setCursorPos,
  901.                         getSize = printer.getPageSize,
  902.                         write = printer.write,
  903.                 }
  904.                 terminal.scroll = function()
  905.                         if nPage == 1 then
  906.                                 printer.setPageTitle( sName.." (page "..nPage..")" )                  
  907.                         end
  908.                        
  909.                         while not printer.newPage()     do
  910.                                 if printer.getInkLevel() < 1 then
  911.                                         sStatus = "Printer out of ink, please refill"
  912.                                 elseif printer.getPaperLevel() < 1 then
  913.                                         sStatus = "Printer out of paper, please refill"
  914.                                 else
  915.                                         sStatus = "Printer output tray full, please empty"
  916.                                 end
  917.        
  918.                                 term.restore()
  919.                                 redrawMenu()
  920.                                 term.redirect( terminal )
  921.                                
  922.                                 local timer = os.startTimer(0.5)
  923.                                 sleep(0.5)
  924.                         end
  925.  
  926.                         nPage = nPage + 1
  927.                         if nPage == 1 then
  928.                                 printer.setPageTitle( sName )
  929.                         else
  930.                                 printer.setPageTitle( sName.." (page "..nPage..")" )
  931.                         end
  932.                 end
  933.                
  934.                 bMenu = false
  935.                 term.redirect( terminal )
  936.                 local ok, error = pcall( function()
  937.                         term.scroll()
  938.                         for n, sLine in ipairs( tLines ) do
  939.                                 print( sLine )
  940.                         end
  941.                 end )
  942.                 term.restore()
  943.                 if not ok then
  944.                         print( error )
  945.                 end
  946.                
  947.                 while not printer.endPage() do
  948.                         sStatus = "Printer output tray full, please empty"
  949.                         redrawMenu()
  950.                         sleep( 0.5 )
  951.                 end
  952.                 bMenu = true
  953.                        
  954.                 if nPage > 1 then
  955.                         sStatus = "Printed "..nPage.." Pages"
  956.                 else
  957.                         sStatus = "Printed 1 Page"
  958.                 end
  959.                 redrawMenu()
  960.         end,
  961.         Exit=function()
  962.                 --Modified by Bjornir90
  963.                 shell.run("desk")
  964.                 bRunning = false
  965.         end
  966. }
  967.  
  968. local function doMenuItem( _n )
  969.         tMenuFuncs[tMenuItems[_n] ]()
  970.         if bMenu then
  971.                 bMenu = false
  972.                 term.setCursorBlink( true )
  973.         end
  974.         redrawMenu()
  975. end
  976.  
  977. local function setCursor( x, y )
  978.         local screenX = x - scrollX
  979.         local screenY = y - scrollY
  980.        
  981.         local bRedraw = false
  982.         if screenX < 1 then
  983.                 scrollX = x - 1
  984.                 screenX = 1
  985.                 bRedraw = true
  986.         elseif screenX > w then
  987.                 scrollX = x - w
  988.                 screenX = w
  989.                 bRedraw = true
  990.         end
  991.        
  992.         if screenY < 1 then
  993.                 scrollY = y - 1
  994.                 screenY = 1
  995.                 bRedraw = true
  996.         elseif screenY > h-1 then
  997.                 scrollY = y - (h-1)
  998.                 screenY = h-1
  999.                 bRedraw = true
  1000.         end
  1001.        
  1002.         if bRedraw then
  1003.                 redrawText()
  1004.         end
  1005.         term.setCursorPos( screenX, screenY )
  1006.        
  1007.         -- Statusbar now pertains to menu, it would probably be safe to redraw the menu on every key event.
  1008.         redrawMenu()
  1009. end
  1010.  
  1011. -- Actual program functionality begins
  1012. load(sPath)
  1013.  
  1014. term.setBackgroundColour( bgColour )
  1015. term.clear()
  1016. term.setCursorPos(x,y)
  1017. term.setCursorBlink( true )
  1018.  
  1019. redrawText()
  1020. redrawMenu()
  1021.  
  1022. -- Handle input
  1023. while bRunning do
  1024.         local sEvent, param, param2, param3 = os.pullEvent()
  1025.         if sEvent == "key" then
  1026.                 if param == keys.up then
  1027.                         -- Up
  1028.                         if not bMenu then
  1029.                                 if y > 1 then
  1030.                                         -- Move cursor up
  1031.                                         y = y - 1
  1032.                                         x = math.min( x, string.len( tLines[y] ) + 1 )
  1033.                                         setCursor( x, y )
  1034.                                 end
  1035.                         end
  1036.                 elseif param == keys.down then
  1037.                         -- Down
  1038.                         if not bMenu then
  1039.                                 -- Move cursor down
  1040.                                 if y < #tLines then
  1041.                                         y = y + 1
  1042.                                         x = math.min( x, string.len( tLines[y] ) + 1 )
  1043.                                         setCursor( x, y )
  1044.                                 end
  1045.                         end
  1046.                 elseif param == keys.tab then
  1047.                         -- Tab
  1048.                         if not bMenu then
  1049.                                 local sLine = tLines[y]
  1050.  
  1051.                                 -- Indent line
  1052.                                 -- IN CASE OF INSERT TAB IN PLACE:
  1053.                                 -- tLines[y] = string.sub(sLine,1,x-1) .. "  " .. string.sub(sLine,x)
  1054.                                 tLines[y]="  "..tLines[y]
  1055.                                 x = x + 2
  1056.                                 setCursor( x, y )
  1057.                                 redrawLine(y)
  1058.                         end
  1059.                 elseif param == keys.pageUp then
  1060.                         -- Page Up
  1061.                         if not bMenu then
  1062.                                 -- Move up a page
  1063.                                 local sx,sy=term.getSize()
  1064.                                 y=y-sy-1
  1065.                                 if y<1 then     y=1 end
  1066.                                 x = math.min( x, string.len( tLines[y] ) + 1 )
  1067.                                 setCursor( x, y )
  1068.                         end
  1069.                 elseif param == keys.pageDown then
  1070.                         -- Page Down
  1071.                         if not bMenu then
  1072.                                 -- Move down a page
  1073.                                 local sx,sy=term.getSize()
  1074.                                 if y<#tLines-sy-1 then
  1075.                                         y = y+sy-1
  1076.                                 else
  1077.                                         y = #tLines
  1078.                                 end
  1079.                                 x = math.min( x, string.len( tLines[y] ) + 1 )
  1080.                                 setCursor( x, y )
  1081.                         end
  1082.                 elseif param == keys.home then
  1083.                         -- Home
  1084.                         if not bMenu then
  1085.                                 -- Move cursor to the beginning
  1086.                                 x=1
  1087.                                 setCursor(x,y)
  1088.                         end
  1089.                 elseif param == keys["end"] then
  1090.                         -- End
  1091.                         if not bMenu then
  1092.                                 -- Move cursor to the end
  1093.                                 x = string.len( tLines[y] ) + 1
  1094.                                 setCursor(x,y)
  1095.                         end
  1096.                 elseif param == keys.left then
  1097.                         -- Left
  1098.                         if not bMenu then
  1099.                                 if x > 1 then
  1100.                                         -- Move cursor left
  1101.                                         x = x - 1
  1102.                                 elseif x==1 and y>1 then
  1103.                                         x = string.len( tLines[y-1] ) + 1
  1104.                                         y = y - 1
  1105.                                 end
  1106.                                 setCursor( x, y )
  1107.                         else
  1108.                                 -- Move menu left
  1109.                                 nMenuItem = nMenuItem - 1
  1110.                                 if nMenuItem < 1 then
  1111.                                         nMenuItem = #tMenuItems
  1112.                                 end
  1113.                                 redrawMenu()
  1114.                         end
  1115.                 elseif param == keys.right then
  1116.                         -- Right
  1117.                         if not bMenu then
  1118.                                 if x < string.len( tLines[y] ) + 1 then
  1119.                                         -- Move cursor right
  1120.                                         x = x + 1
  1121.                                 elseif x==string.len( tLines[y] ) + 1 and y<#tLines then
  1122.                                         x = 1
  1123.                                         y = y + 1
  1124.                                 end
  1125.                                 setCursor( x, y )
  1126.                         else
  1127.                                 -- Move menu right
  1128.                                 nMenuItem = nMenuItem + 1
  1129.                                 if nMenuItem > #tMenuItems then
  1130.                                         nMenuItem = 1
  1131.                                 end
  1132.                                 redrawMenu()
  1133.                         end
  1134.                 elseif param == keys.delete then
  1135.                         -- Delete
  1136.                         if not bMenu then
  1137.                                 if  x < string.len( tLines[y] ) + 1 then
  1138.                                         local sLine = tLines[y]
  1139.                                         tLines[y] = string.sub(sLine,1,x-1) .. string.sub(sLine,x+1)
  1140.                                         redrawLine(y)
  1141.                                 elseif y<#tLines then
  1142.                                         tLines[y] = tLines[y] .. tLines[y+1]
  1143.                                         table.remove( tLines, y+1 )
  1144.                                         redrawText()
  1145.                                         redrawMenu()
  1146.                                 end
  1147.                         end
  1148.                 elseif param == keys.backspace then
  1149.                         -- Backspace
  1150.                         if not bMenu then
  1151.                                 if x > 1 then
  1152.                                         -- Remove character
  1153.                                         local sLine = tLines[y]
  1154.                                         tLines[y] = string.sub(sLine,1,x-2) .. string.sub(sLine,x)
  1155.                                         redrawLine(y)
  1156.                        
  1157.                                         x = x - 1
  1158.                                         setCursor( x, y )
  1159.                                 elseif y > 1 then
  1160.                                         -- Remove newline
  1161.                                         local sPrevLen = string.len( tLines[y-1] )
  1162.                                         tLines[y-1] = tLines[y-1] .. tLines[y]
  1163.                                         table.remove( tLines, y )
  1164.                                         redrawText()
  1165.                                
  1166.                                         x = sPrevLen + 1
  1167.                                         y = y - 1
  1168.                                         setCursor( x, y )
  1169.                                 end
  1170.                         end
  1171.                 elseif param == keys.enter then
  1172.                         -- Enter
  1173.                         if not bMenu then
  1174.                                 -- Newline
  1175.                                 local sLine = tLines[y]
  1176.                                 local _,spaces=string.find(sLine,"^[ ]+")
  1177.                                 if not spaces then
  1178.                                         spaces=0
  1179.                                 end
  1180.                                 tLines[y] = string.sub(sLine,1,x-1)
  1181.                                 table.insert( tLines, y+1, string.rep(' ',spaces)..string.sub(sLine,x) )
  1182.                                 redrawText()
  1183.                        
  1184.                                 x = spaces+1
  1185.                                 y = y + 1
  1186.                                 setCursor( x, y )
  1187.                         else
  1188.                                 -- Menu selection
  1189.                                 doMenuItem( nMenuItem )
  1190.                         end
  1191.                 elseif param == keys.leftCtrl or param == keys.rightCtrl then
  1192.                         -- Menu toggle
  1193.                         bMenu = not bMenu
  1194.                         if bMenu then
  1195.                                 term.setCursorBlink( false )
  1196.                                 nMenuItem = 1
  1197.                         else
  1198.                                 term.setCursorBlink( true )
  1199.                         end
  1200.                         redrawMenu()
  1201.                 end
  1202.                
  1203.         elseif sEvent == "char" then
  1204.                 if not bMenu then
  1205.                         -- Input text
  1206.                         local sLine = tLines[y]
  1207.                         tLines[y] = string.sub(sLine,1,x-1) .. param .. string.sub(sLine,x)
  1208.                         redrawLine(y)
  1209.                
  1210.                         x = x + string.len( param )
  1211.                         setCursor( x, y )
  1212.                 else
  1213.                         -- Select menu items
  1214.                         for n,sMenuItem in ipairs( tMenuItems ) do
  1215.                                 if string.lower(string.sub(sMenuItem,1,1)) == string.lower(param) then
  1216.                                         doMenuItem( n )
  1217.                                         break
  1218.                                 end
  1219.                         end
  1220.                 end
  1221.                
  1222.         elseif sEvent == "mouse_click" then
  1223.                 if not bMenu then
  1224.                         if param == 1 then
  1225.                                 -- Left click
  1226.                                 local cx,cy = param2, param3
  1227.                                 if cy < h then
  1228.                                         y = math.min( math.max( scrollY + cy, 1 ), #tLines )
  1229.                                         x = math.min( math.max( scrollX + cx, 1 ), string.len( tLines[y] ) + 1 )
  1230.                                         setCursor( x, y )
  1231.                                 end
  1232.                         end
  1233.                 end
  1234.                
  1235.         elseif sEvent == "mouse_scroll" then
  1236.                 if not bMenu then
  1237.                         if param == -1 then
  1238.                                 -- Scroll up
  1239.                                 if scrollY > 0 then
  1240.                                         -- Move cursor up
  1241.                                         scrollY = scrollY - 1
  1242.                                         redrawText()
  1243.                                 end
  1244.                        
  1245.                         elseif param == 1 then
  1246.                                 -- Scroll down
  1247.                                 local nMaxScroll = #tLines - (h-1)
  1248.                                 if scrollY < nMaxScroll then
  1249.                                         -- Move cursor down
  1250.                                         scrollY = scrollY + 1
  1251.                                         redrawText()
  1252.                                 end
  1253.                                
  1254.                         end
  1255.                 end
  1256.         end
  1257. end
  1258.  
  1259. -- Cleanup
  1260. term.clear()
  1261. term.setCursorBlink( false )
  1262. term.setCursorPos( 1, 1 )]])
  1263. editF.close()
  1264. local favori = fs.open("/desktop/favoris", "w")
  1265. favori.write("{[1]=\"0\", [2]=\"0\", [3]=\"0\", [4]=\"0\", [5]=\"0\", [6]=\"0\",}")
  1266. favori.close()
  1267. print("Installation done")
  1268. sleep(0.5)
  1269. end
  1270.  
  1271. --Initialisation
  1272. os.loadAPI("/desktop/gui")
  1273. gui.initializeLog()
  1274. local _path = shell.getRunningProgram()
  1275. shell.setAlias("desk", _path)
  1276. local termX, termY = term.getSize()
  1277. local midX, midY = termX/2-0.5, termY/2-0.5
  1278. --Format : table {fav1{infos} fav2{infos}}
  1279. local favF = fs.open("/desktop/favoris", "r")
  1280. local favS = favF.readAll()
  1281. local favT = textutils.unserialize(favS)
  1282. local fav1 = favT[1]
  1283. local fav2 = favT[2]
  1284. local fav3 = favT[3]
  1285. local fav4 = favT[4]
  1286. local fav5 = favT[5]
  1287. local fav6 = favT[6]
  1288. favF.close()
  1289. --End of initialisation
  1290.  
  1291. -- args : x, y, text, colorLine, colorBG
  1292.  
  1293.  
  1294. local function createFav(number)
  1295.  favT[number] = {}
  1296.  gui.clear()
  1297.  gui.clearBuffer()
  1298.  gui.setHeader("     Favori "..number.." editing", colors.lightBlue)
  1299.  gui.fill(1,1,termX,termY,colors.cyan)
  1300.  term.setBackgroundColor(colors.cyan)
  1301.  term.setCursorPos(1,4)
  1302.  write("Name of favori :")
  1303.  term.setBackgroundColor(colors.black)
  1304.  write("             ") --Draw a black line to enter characters
  1305.  term.setCursorPos(1,6)
  1306.  term.setBackgroundColor(colors.cyan)
  1307.  write("Path to program :")
  1308.  term.setBackgroundColor(colors.black)
  1309.  write("                 ")
  1310.  term.setCursorPos(1, 8)
  1311.  term.setBackgroundColor(colors.cyan)
  1312.  write("Argument (optional) :")
  1313.  term.setBackgroundColor(colors.black)
  1314.  write("                 ")
  1315.  term.setCursorPos(17,4)
  1316.  favT[number]["text"] = tostring(read())
  1317.  term.setCursorPos(18,6)
  1318.  favT[number]["path"] = tostring(read())
  1319.  term.setCursorPos(22,8)
  1320.  favT[number]["arg"] = tostring(read())
  1321.  local favS = textutils.serialize(favT)
  1322.  local favF = fs.open("desktop/favoris", "w")
  1323.  favF.write(favS)
  1324.  favF.close()
  1325.  local favF = fs.open("/desktop/favoris", "r")
  1326.  local favS = favF.readAll()
  1327.  favT = textutils.unserialize(favS)
  1328.  fav1 = favT[1]
  1329.  fav2 = favT[2]
  1330.  fav3 = favT[3]
  1331.  fav4 = favT[4]
  1332.  fav5 = favT[5]
  1333.  fav6 = favT[6]
  1334.  favF.close()
  1335.  drawFavori()
  1336. end
  1337.  
  1338.  
  1339.  
  1340. local function editFav(number)
  1341.  gui.clear()
  1342.  gui.clearBuffer()
  1343.  gui.fill(1,1,termX,termY,colors.cyan)
  1344.  gui.setButton(3,4, "EDIT FAVORI", colors.lightBlue, colors.blue)
  1345.  gui.setButton(3,9, "DELETE FAVORI", colors.lightBlue, colors.blue)
  1346.  gui.setHeader("                   EDIT FAVORI "..number, colors.lightBlue)
  1347.  gui.setButton(midX-4, midY, "CANCEL", colors.lightBlue, colors.blue)
  1348.  gui.render(true)
  1349.  local input = gui.getSelection()
  1350.  if input == 1 then
  1351.   createFav(number)
  1352.  elseif input == 2 then
  1353.   if favT[number+1] ~= "0" then
  1354.    favT[number] = "0"
  1355.    favT[number] = favT[number+1]
  1356.    favT[number+1] = "0"
  1357.   elseif favT[number+1] == "0" then
  1358.    favT[number] = "0"
  1359.   end
  1360.   local favS = textutils.serialize(favT)
  1361.   local favF = fs.open("desktop/favoris", "w")
  1362.   favF.write(favS)
  1363.   favF.close()
  1364.   local favF = fs.open("/desktop/favoris", "r")
  1365.   local favS = favF.readAll()
  1366.   favT = textutils.unserialize(favS)
  1367.   fav1 = favT[1]
  1368.   fav2 = favT[2]
  1369.   fav3 = favT[3]
  1370.   fav4 = favT[4]
  1371.   fav5 = favT[5]
  1372.   fav6 = favT[6]
  1373.   favF.close()
  1374.   drawFavori()
  1375.  elseif input == 3 then
  1376.   drawFavori()
  1377.  end
  1378. end
  1379.  
  1380.  
  1381. local function selectFavori()
  1382.  local input = gui.getSelection()
  1383.  if input == 1 then
  1384.   if fav1 ~= "0" then
  1385.    editFav(1)
  1386.   else
  1387.    createFav(1)
  1388.   end
  1389.  elseif input == 2 then
  1390.   if fav2 ~= "0" then
  1391.    editFav(2)
  1392.   else
  1393.    createFav(2)
  1394.   end
  1395.  elseif input == 3 then
  1396.   if fav3 ~= "0" then
  1397.    editFav(3)
  1398.   else
  1399.    createFav(3)
  1400.   end
  1401.  elseif input == 4 then
  1402.   if fav4 ~= "0" then
  1403.    editFav(4)
  1404.   else
  1405.    createFav(4)
  1406.   end
  1407.  elseif input == 5 then
  1408.   if fav5 ~= "0" then
  1409.    editFav(5)
  1410.   else
  1411.    createFav(5)
  1412.   end
  1413.  elseif input == 6 then
  1414.   if fav6 ~= "0" then
  1415.    editFav(6)
  1416.   else
  1417.    createFav(6)
  1418.   end
  1419.  elseif input == 7 then
  1420.   drawOptionDev()
  1421.  end
  1422. end
  1423.  
  1424. function drawFavori()
  1425.  gui.clear()
  1426.  gui.clearBuffer()
  1427.  gui.fill(1,1,termX,termY, colors.cyan)
  1428.  gui.setButton(4, 4, "FAVORI 1", colors.lightBlue, colors.blue)
  1429.  gui.setButton(4, 8, "FAVORI 2", colors.lightBlue, colors.blue)
  1430.  gui.setButton(4, 12, "FAVORI 3", colors.lightBlue, colors.blue)
  1431.  gui.setButton(4, 16, "FAVORI 4", colors.lightBlue, colors.blue)
  1432.  gui.setButton(midX-5, 4, "FAVORI 5", colors.lightBlue, colors.blue)
  1433.  gui.setButton(midX-5, 8, "FAVORI 6", colors.lightBlue, colors.blue)
  1434.  gui.setButton(midX-4, 12, "CANCEL", colors.lightBlue, colors.blue)
  1435.  gui.render(true)
  1436.  selectFavori()
  1437. end
  1438.  
  1439. local function selectOption()
  1440.  local input = gui.getSelection()
  1441.  if input == 1 then
  1442.   drawFavori()
  1443.  elseif input == 2 then
  1444.   shell.run("desktop/autoupdater")
  1445.  elseif input == 3 then
  1446.   gui.clear()
  1447.   term.setCursorPos(2, 4)
  1448.   write("Owner, coder and technical support : Bjornir90")
  1449.   term.setCursorPos(4, 6)
  1450.   write("Thanks to :")
  1451.   term.setCursorPos(6, 7)
  1452.   write("Dan200 and Cloudy for computercraft\n and everything that come with it")
  1453.   term.setCursorPos(6, 9)
  1454.   write("ArchAngel075 and Oct125 for helping me\n fix my GUI API")
  1455.   term.setCursorPos(6, 11)
  1456.   write("Lesiteduzero.com for giving me the bases\n in (go here http://www.youtube.com/watch?v=dQw4w9WgXcQ) programmation")
  1457.   term.setCursorPos(6, 15)
  1458.   write("The amazing community which helped me a lot")
  1459.   term.setCursorPos(2, 17)
  1460.   print("Simple desk inDev, please report errors\n to the technical support") --Line to edit for versions
  1461.   term.setTextColor(colors.red)
  1462.   print("Press any key to continue")
  1463.   term.setTextColor(colors.white)
  1464.   local event = os.pullEvent("key")
  1465.   drawOption()
  1466.  elseif input == 4 then
  1467.   drawDesk()
  1468.  end
  1469. end
  1470.  
  1471. function drawOption()
  1472.  gui.clear()
  1473.  gui.clearBuffer()
  1474.  gui.fill(1,1,termX,termY,colors.cyan)
  1475.  gui.setButton(midX-4, 4, "FAVORIS", colors.lightBlue, colors.blue)
  1476.  gui.setHeader("             Options", colors.lightBlue)
  1477.  gui.setButton(midX-7, 8, "AUTO-UPDATE", colors.lightBlue, colors.blue)
  1478.  gui.setButton(midX-4, 12, "CREDIT", colors.lightBlue, colors.blue)
  1479.  gui.setButton(midX-4, 16, "CANCEL", colors.lightBlue, colors.blue)
  1480.  gui.render(true)
  1481.  gui.clearBuffer()
  1482.  selectOption()
  1483. end
  1484.  
  1485. local function selectOptionDev()
  1486.  local input = gui.getSelection()
  1487.  if input == 1 then
  1488.   os.unloadAPI("gui")
  1489.   fs.delete("desktop")
  1490.   shell.run("desk")
  1491.  elseif input == 2 then
  1492.   os.unloadAPI("gui")
  1493.   fs.delete("desktop")
  1494.  elseif input == 3
  1495.  then
  1496.   drawDesk()
  1497.  elseif input == 4 then
  1498.   os.reboot()
  1499.  elseif input == 5 then
  1500.   shell.run("desktop/edit", "desktop/favoris")
  1501.  elseif input == 6 then
  1502.   shell.run("desktop/edit", "guiAPI/log")
  1503.  elseif input == 7 then
  1504.   os.loadAPI("/desktop/gui")
  1505.   gui.initializeLog()
  1506.   local _path = shell.getRunningProgram()
  1507.   shell.setAlias("desk", _path)
  1508.   local termX, termY = term.getSize()
  1509.   local midX, midY = termX/2-0.5, termY/2-0.5
  1510.   --Format : table {fav1{infos} fav2{infos}}
  1511.   local favF = fs.open("/desktop/favoris", "r")
  1512.   local favS = favF.readAll()
  1513.   favT = textutils.unserialize(favS)
  1514.   fav1 = favT[1]
  1515.   fav2 = favT[2]
  1516.   fav3 = favT[3]
  1517.   fav4 = favT[4]
  1518.   fav5 = favT[5]
  1519.   fav6 = favT[6]
  1520.   favF.close()
  1521.   drawOptionDev()
  1522.  end
  1523. end
  1524.  
  1525. function drawOptionDev()
  1526.  gui.clear()
  1527.  gui.clearBuffer()
  1528.  gui.setButton(2, 2, "Remake disk")
  1529.  gui.setButton(2, 5, "Unload disk")
  1530.  gui.setButton(2, 8, "Cancel")
  1531.  gui.setButton(2, 11, "Reboot")
  1532.  gui.setButton(18, 5, "Edit favoris file")
  1533.  gui.setButton(2, 14, "Open GUI log")
  1534.  gui.setButton(2,17, "Reload disk")
  1535.  gui.render()
  1536.  selectOptionDev()
  1537. end
  1538.  
  1539. local function selectDesk()
  1540.  local input = gui.getSelection()
  1541.  if input == 1 then
  1542.   gui.clear()
  1543.   os.unloadAPI("gui")
  1544.   shell.run("/desktop/shell")
  1545.  elseif input == 2 then
  1546.   local clickedB = gui.popUp("Edit a program", "You are going to chose which program to edit", 1, midY, colors.lightBlue, colors.blue, "Cancel", "Next")
  1547.   if clickedB == 1 then
  1548.    drawDesk()
  1549.   elseif clickedB == 2 then
  1550.    gui.clear()
  1551.    write("Choose your program : ")
  1552.    local program = tostring(read())
  1553.    gui.clear()
  1554.    os.unloadAPI("gui")
  1555.    shell.run("desktop/edit", program)
  1556.   end
  1557.  elseif input == 3 then
  1558.   local toShut = gui.popUp("Shutdown", "Are you sure you want to shutdown ?", midX-12, midY, colors.lightBlue, colors.blue, "No", "Yes")
  1559.   if toShut == 1 then
  1560.    drawDesk()
  1561.   elseif toShut == 2 then
  1562.    gui.clear()
  1563.    print("Thanks for using Simple Desk :\) ")
  1564.    sleep(1.4)
  1565.    os.shutdown()
  1566.   end
  1567.  elseif input == 4 then
  1568.   drawOption()
  1569.  elseif input == 5 then
  1570.   --drawOptionDev()
  1571.   os.reboot()
  1572.  elseif input == 6 then
  1573.   shell.run(fav1.path, fav1.arg)
  1574.  elseif input == 7 then
  1575.   shell.run(fav2.path, fav2.arg)
  1576.  elseif input == 8 then
  1577.   shell.run(fav3.path, fav3.arg)
  1578.  elseif input == 9 then
  1579.   shell.run(fav4.path, fav4.arg)
  1580.  elseif input == 10 then
  1581.   shell.run(fav5.path, fav5.arg)
  1582.  elseif input == 11 then
  1583.   shell.run(fav6.path, fav6.arg)
  1584.  end
  1585. end
  1586.  
  1587. function drawDesk()
  1588. gui.clear()
  1589. gui.clearBuffer()
  1590. gui.fill(1, 1, termX, termY, colors.cyan) --Wallpaper
  1591. gui.setButton(midX-4, 4, "SHELL", colors.lightBlue, colors.blue)
  1592. gui.setButton(midX-3, 8, "EDIT", colors.lightBlue, colors.blue)
  1593. gui.setButton(midX-5, 12,"SHUTDOWN", colors.lightBlue, colors.blue)
  1594. gui.setButton(midX-4, 16, "OPTION", colors.lightBlue, colors.blue)
  1595. --gui.setButton(midX+6, 16, "OPTIONSDEV", colors.lightBlue, colors.blue)
  1596. gui.setButton(midX+6, 16, "REBOOT", colors.lightBlue, colors.blue)
  1597. term.setBackgroundColor(colors.blue)
  1598. gui.setHeader("                  Simple Desk inDev", colors.lightBlue) --Line to edit for version
  1599. term.setBackgroundColor(colors.black)
  1600. if fav1 ~= "0" then
  1601.  gui.setButton(2, 4, fav1.text, colors.lightBlue, colors.blue)
  1602.  if fav2 ~= "0" then
  1603.   gui.setButton(2, 8, fav2.text, colors.lightBlue, colors.blue)
  1604.   if fav3 ~= "0" then
  1605.    gui.setButton(2, 12, fav3.text, colors.lightBlue, colors.blue)
  1606.    if fav4 ~= "0" then
  1607.     gui.setButton(2, 16, fav4.text, colors.lightBlue, colors.blue)
  1608.     if fav5 ~= "0" then
  1609.      gui.setButton(35, 4, fav5.text, colors.lightBlue, colors.blue)
  1610.      if fav6 ~= "0" then
  1611.       gui.setButton(35, 8, fav6.text, colors.lightBlue, colors.blue)
  1612.      end
  1613.     end
  1614.    end
  1615.   end
  1616.  end
  1617. end
  1618. gui.render(true)
  1619. selectDesk()
  1620. end
  1621.  
  1622.  
  1623. drawDesk()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement