Advertisement
xXm0dzXx

TurtleBrowse

Nov 23rd, 2012
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.08 KB | None | 0 0
  1. local x,y = term.getSize()
  2.  
  3. local function cPrint( txt ) --Version 2.0 of cPrint
  4.     local function printC( text )
  5.         x2,y2 = term.getCursorPos()
  6.         term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
  7.         write(text.. "\n")
  8.     end
  9.    
  10.     if type(txt) == "string" then
  11.         printC( txt )
  12.     elseif type(txt) == "table" then
  13.         for i=1,#txt do
  14.             printC( txt[i] )
  15.         end
  16.     end
  17. end
  18.  
  19. function drawTop()
  20.     term.setBackgroundColour( colors.white )
  21.     term.clear()
  22.     term.setCursorPos(1,1)
  23.     term.setBackgroundColour( colors.black )
  24.     term.clearLine()
  25.     term.setTextColour( colors.white )
  26.     print("TurtleBrowse v1.0 - CCFan11")
  27.     print( string.rep( "=", x ) )
  28. end
  29.  
  30. function _( xz )
  31.     term.setTextColour( colors.orange )
  32.     if xz then
  33.         write("|")
  34.     else
  35.         print("|")
  36.     end
  37.     term.setTextColour( colors.black )
  38. end
  39.  
  40. drawTop()
  41. term.setBackgroundColour( colors.orange )
  42. term.clearLine()
  43. print()
  44. term.clearLine()
  45. local xx, yy = term.getCursorPos()
  46. term.clearLine()
  47. cPrint("TurtleBrowse")
  48. term.clearLine()
  49. cPrint("Your first stop for ComputerCraft scripts.")
  50. term.clearLine()
  51. print()
  52. term.setBackgroundColour( colors.gray )
  53. term.clearLine()
  54. print("   TurtleScripts.com")
  55. print()
  56. term.setBackgroundColour( colors.white )
  57. term.setTextColour( colors.black )
  58. print("Welcome to TurtleBrowse, TurtleBrowse is a program created by CCFan11 that allows users to easily browse TurtleScripts with a nice and user friendly GUI.")
  59.  
  60. local selection = 1
  61. while true do
  62.     term.setCursorPos(1,y-3)
  63.     term.setTextColour( colors.orange )
  64.     print("Menu------+")
  65.     term.setTextColour( colors.black )
  66.     if selection == 1 then
  67.         write("[>] Browse") _()
  68.     else
  69.         write("[ ] Browse") _()
  70.     end
  71.     if selection == 2 then
  72.         write("[>] Upload") _()
  73.     else
  74.         write("[ ] Upload") _()
  75.     end
  76.     if selection == 3 then
  77.         write("[>] Exit  ") _( true )
  78.     else
  79.         write("[ ] Exit  ") _( true )
  80.     end
  81.     local event, key = os.pullEvent("key")
  82.     if key == keys.up then
  83.         if selection ~= 1 then selection = selection-1 end
  84.     elseif key == keys.down then
  85.         if selection ~= 3 then selection = selection+1 end
  86.     elseif key == keys.enter then
  87.         if selection == 3 then
  88.             term.setBackgroundColour( colors.black )
  89.             term.clear()
  90.             term.setCursorPos(1,1)
  91.             error()
  92.         end
  93.         break
  94.     end
  95. end
  96.  
  97. drawTop()
  98. term.setTextColour( colors.black )
  99. if selection == 2 then
  100.     term.setTextColour( colors.red )
  101.     print("Coming soon...")
  102.     error()
  103. else
  104.     term.setBackgroundColour( colors.white )
  105.     print("Downloader\n")
  106.     write("Enter Post ID: ")
  107.     local sID = read()
  108.     print("Retrieving Post...")
  109.     local response = http.get("http://api.turtlescripts.com/getFileRAW/" ..sID)
  110.     if response ~= nil then
  111.         local sCode = response.readAll()
  112.         local sInfo = http.get("http://api.turtlescripts.com/getProject/" ..sID).readAll()
  113.         local tLines = {}
  114.         local currentLine = 1
  115.         local currentDownload = 0
  116.        
  117.         function redrawLines()
  118.             term.clear()
  119.             term.setCursorPos(1,1)
  120.             for i=1,#tLines do--currentLine,y-3 do
  121.                 if i == 2 then
  122.                     term.setTextColour( colors.green )
  123.                     print( tLines[i] )
  124.                 elseif i == 3 then
  125.                     term.setTextColour( colors.blue )
  126.                     local _, xzvc = term.getCursorPos()
  127.                     currentDownload = xzvc - currentLine
  128.                     print( tLines[i] )
  129.                 else
  130.                     print()
  131.                     local start = string.find(sInfo, "\"postName\":\"")
  132.                     local enddd = string.find(sInfo, "\"", start+13)
  133.                     term.setTextColour( colors.gray )
  134.                     print( string.sub( sInfo, start+12, enddd-1 ) )
  135.                     print()
  136.                     term.setTextColour( colors.green )
  137.                     print( "Description" )
  138.                     local start = string.find(sInfo, "\"postDescription\":\"")
  139.                     local enddd = string.find(sInfo, "\"", start+20)
  140.                     term.setTextColour( colors.red )
  141.                     print( string.sub( sInfo, start+19, enddd-1 ) )
  142.                    
  143.                     print()
  144.                 end
  145.             end
  146.             term.scroll( currentLine )
  147.             term.setCursorPos(1,y-1)
  148.             term.setTextColour( colors.red )
  149.             term.setBackgroundColour( colors.black )
  150.             print("------------+")
  151.             write("Click to esc|")
  152.             term.setBackgroundColour( colors.white )
  153.         end
  154.        
  155.         function load()
  156.             tLines[1] = "EVERYTAHANG!!!"
  157.             tLines[2] = "Download"
  158.             tLines[3] = "Click here to download"
  159.         end
  160.        
  161.         load()
  162.         redrawLines()
  163.         while true do
  164.             local event, key, xz, yz = os.pullEvent()
  165.             if event == "key" then
  166.                 if key == keys.up and currentLine ~= 1 then
  167.                     currentLine = currentLine -1
  168.                     redrawLines()
  169.                 elseif key == keys.down and currentLine ~= #tLines then
  170.                     currentLine = currentLine +1
  171.                     redrawLines()
  172.                 end
  173.             elseif event == "mouse_click" and key == 1 then
  174.                 if xz < 23 and yz == currentDownload then
  175.                     term.setTextColour( colors.blue )
  176.                     term.setCursorPos(1,currentDownload)
  177.                     term.clearLine()
  178.                     write("Downloading...")
  179.                     sleep(1)
  180.                     term.setTextColour( colors.blue )
  181.                     term.setCursorPos(1,currentDownload)
  182.                     fs.delete( sID )
  183.                     file = fs.open( sID, "w" )
  184.                     file.write( sCode )
  185.                     file.close()
  186.                     write("Done! Saved as " ..sID )
  187.                 elseif xz < 34 and yz == y then
  188.                     shell.run( shell.getRunningProgram() )
  189.                     error()
  190.                 end
  191.             end
  192.         end
  193.     else
  194.         print("Invalid Post ID. (Example: gjdgxt)")
  195.     end
  196. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement