Advertisement
Heracles421

CCiri

Jan 5th, 2013
1,531
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.53 KB | None | 0 0
  1. --[[ Variables ]]--
  2. version = "2.1.3"
  3. screenX, screenY = term.getSize()
  4. loadingBar = false
  5. running = true
  6. progress = false
  7. pastebin_url = "PRUuJxf6"
  8. isBeta = false
  9. path = shell.dir()
  10. programPath = shell.getRunningProgram()
  11.  
  12. -- Tables
  13. t_mic = {
  14.   ".::.",
  15.   "///\\\\\\",
  16.   "||||||",
  17.   "HH  ||||||  HH",
  18.   "HH==========HH",
  19.   "HH==========HH",
  20.   "HH  ######  HH",
  21.   "HH   ####   HH",
  22.   "HH    ##    HH",
  23.   "HH          HH",
  24.   "\\\\      //",
  25.   "\\\\  //",
  26.   "___\\/___ ",
  27. }
  28.  
  29. local logo = {
  30. " 9999  9999 7    7 ",
  31. "9     9            ",
  32. "9     9     7 77 7 ",
  33. "9     9     7 7  7 ",
  34. "9     9     7 7  7 ",
  35. "9     9     7 7  7 ",
  36. " 9999  9999 7 7  7 ",
  37. }
  38.  
  39. local appkeys = {"GU7GAL-HLW2TGXR4J","P99XV4-6Y359T8584","5KGYY5-YE2JLLG74H"}
  40. --[[ End of Variables ]]--
  41.  
  42. --[[ Functions ]]--
  43.  
  44. -- Draw a line at _yPos
  45. function line(_yPos)
  46.   term.setCursorPos(1, _yPos)
  47.   for i = 1, screenX do
  48.     write("-")
  49.   end
  50. end
  51.  
  52. -- Print text centered at line _yPos
  53. function printC(text, _yPos)
  54.   term.setCursorPos((screenX - #text)/2, _yPos)
  55.   write(text)
  56. end
  57.  
  58. -- Clear screen and set cursor pos
  59. function cScreen(swidth, sheight)
  60.   term.clear()
  61.   term.setCursorPos(swidth, sheight)
  62. end
  63.  
  64. -- Wait for key pressing
  65. function waitForExitKey()
  66.   t = "Press x to go back"
  67.   term.setCursorPos(screenX - #t, 4)
  68.   write(t)
  69.   while true do
  70.     _, key = os.pullEvent("key")
  71.     if key == keys.x then break end
  72.   end
  73. end
  74.  
  75. -- Print a progress bar
  76. function progressBar()
  77.   local function doWrite(char)
  78.     term.setCursorPos(3, 6)
  79.     write("Loading "..char)
  80.     sleep(0.1)
  81.   end
  82.   while true do
  83.     doWrite("|")
  84.     doWrite("/")
  85.     doWrite("-")
  86.     doWrite("\\")
  87.   end
  88. end
  89.  
  90. -- Split a paragraph into single lines
  91. function splitLine( text )
  92.   local swidth = term.getSize()
  93.   local function split( str, regex )
  94.     local t = { }
  95.     local fpat = "(.-)"..regex
  96.     local last_end = 1
  97.     local s, e, cap = str:find(fpat, 1)
  98.     while s do
  99.       if s ~= 1 or cap ~= "" then
  100.         table.insert(t,cap)
  101.       end
  102.       last_end = e+1
  103.       s, e, cap = str:find(fpat, last_end)
  104.     end
  105.     if last_end <= #str then
  106.       cap = str:sub(last_end)
  107.       table.insert(t, cap)
  108.     end
  109.     return t
  110.   end
  111.   local currLine = ""
  112.   local words = split( text, " " )
  113.   local t = {}
  114.   for i = 1, #words do
  115.     if #currLine + #words[i] + 1 > swidth then
  116.       table.insert( t, currLine )
  117.       currLine = words[i].." "
  118.     else
  119.       currLine = currLine..words[i].." "
  120.     end
  121.     if i == #words then
  122.       table.insert( t, currLine )
  123.     end
  124.   end
  125. return t
  126. end
  127.  
  128. -- Get the answer from Wolfram Alpha
  129. function getMessage(_input)
  130.   _input = textutils.urlEncode(_input)
  131.   http.request("http://api.wolframalpha.com/v2/query?appid="..appkeys[math.random(1,#appkeys)].."&input=".._input.."&format=plaintext")
  132.   parallel.waitForAny(progressBar,
  133.   function()
  134.     repeat sEvent, url, sourceText = os.pullEvent() until sEvent == "http_success" or sEvent == "http_failure"
  135.   end)
  136.   if sEvent == "http_success" then
  137.     local respondedText = sourceText.readAll()
  138.     text = {}
  139.     mainTable = {}
  140.     index = 1
  141.     for content in respondedText:gmatch "<plaintext>(.-)</plaintext>" do
  142.       table.insert(text, content)
  143.       text[index] = string.gsub(text[index],"&apos;","'")
  144.       text[index] = string.gsub(text[index],"&quot;","\"")
  145.       text[index] = string.gsub(text[index],"&amp;","&")
  146.       text[index] = string.gsub(text[index],"&lt;","<")
  147.       text[index] = string.gsub(text[index],"&gt;",">")
  148.       index = index + 1
  149.     end
  150.     cScreen(1,1)
  151.     line(1)  printC("CCiri V "..version, 2) line(3)
  152.     if text[1] ~= nil then
  153.       screenx = term.getSize()
  154.       for i = 1, #text do
  155.         if #text[i] >= screenx-5 then
  156.           k = splitLine(text[i])
  157.           for l = 1, #k do
  158.             table.insert(mainTable, k[l])
  159.           end
  160.         else
  161.           table.insert(mainTable, text[i])
  162.         end
  163.       end
  164.       return true, mainTable
  165.     else
  166.       printC("Output: No answer found", 7)
  167.       waitForExitKey()
  168.       return false, nil
  169.     end
  170.   elseif sEvent == "http_failure" then
  171.     cScreen(1,1)
  172.     line(1)  printC("CCiri V "..version, 2) line(3)
  173.     write("Couldn't contact server")
  174.     waitForExitKey()
  175.     return false, nil
  176.   end
  177. end
  178.  
  179. -- Print the answer
  180. function printMessage(_table)
  181.   yPos = 1
  182.   while true do
  183.     cScreen(1,1)
  184.     line(1)  printC("CCiri V "..version, 2) line(3)
  185.     write("Input: "..input, 5)
  186.     t = "Press x to go back"
  187.     term.setCursorPos(screenX - #t, 4)
  188.     write(t)
  189.     for i = 6, screenY do
  190.       term.setCursorPos(1, i)
  191.       term.clearLine()
  192.       if not _table[i + yPos - 6] then
  193.         break
  194.       else
  195.         if string.find(_table[i + yPos - 6], "\n") then
  196.           _table[i + yPos - 6] = string.sub(_table[i + yPos - 6], 1, string.find(_table[i + yPos - 6], "\n") - 1)
  197.           .. " " ..
  198.           string.sub(_table[i + yPos - 6], string.find(_table[i + yPos - 6], "\n") + 1)
  199.         end
  200.         write(_table[i + yPos - 6])
  201.       end
  202.     end
  203.     e, button = os.pullEvent()
  204.     if e == "mouse_scroll" and term.isColor() then
  205.       if button == 1 and yPos < #_table - (screenY - 6) then
  206.       -- Down
  207.       yPos = yPos + 1
  208.     elseif button == -1 and yPos > 1 then
  209.       yPos = yPos - 1
  210.       end
  211.     elseif e == "key" then
  212.       if button == keys.down and yPos < #_table - (screenY - 6) then
  213.         -- Down
  214.         yPos = yPos + 1
  215.       elseif button == keys.up and yPos > 1 then
  216.         --Up
  217.         yPos = yPos - 1
  218.       elseif button == keys.x then
  219.         break
  220.       end
  221.     end
  222.   end
  223.   sleep(0.1)
  224. end  
  225.  
  226. -- Check for Updates
  227. function checkForUpdate()
  228.   intBar:setMessage( "Starting up..." )
  229.   sleep(1)
  230.   intBar:triggerUpdate( "1/4 Checking for updates..." )
  231.   sleep(0.5)
  232.   if http then
  233.     local response = http.get("http://pastebin.com/raw.php?i="..textutils.urlEncode( pastebin_url ))
  234.     if response then
  235.       v = response.readLine()
  236.       v = response.readLine()
  237.       content = "--[[ Variables ]]--\n"..v.."\n"..response.readAll()
  238.       v = string.sub(v, 12, -2)
  239.       response.close()
  240.       if v == version then
  241.         intBar:triggerUpdate( "2/4 No update required" )
  242.         sleep(0.75)
  243.         intBar:triggerUpdate()
  244.         sleep(0.5)
  245.         intBar:triggerUpdate( "4/4 Done." )
  246.       else
  247.         intBar:triggerUpdate( "2/4 Update found ("..v..")" )
  248.         t = "Click Y to update, N to continue"
  249.         term.setCursorPos((screenX-#t)/2, screenY-2)
  250.         write(t)
  251.         repeat _, k = os.pullEvent("key") until k == keys.y or k == keys.n
  252.         term.setCursorPos(1, screenY-2) term.clearLine()
  253.         if k == keys.y then
  254.           version = v
  255.           intBar:triggerUpdate( "3/4 Updating... Please wait" )
  256.           fileName = fs.getName(shell.getRunningProgram())
  257.           file = fs.open(path.."/"..fileName, "w")
  258.           file.write(content)
  259.           file.close()
  260.           sleep(1)
  261.           intBar:triggerUpdate( "4/4 Done." )
  262.           sleep(1)
  263.           return true        
  264.         elseif k == keys.n then
  265.           intBar:triggerUpdate( "3/4 Update canceled.")
  266.           sleep(0.75)
  267.           intBar:triggerUpdate( "4/4 Done." )
  268.           sleep(1)
  269.           return false
  270.         end
  271.       end
  272.     else
  273.       write("Failed to connect to pastebin.")
  274.       return false
  275.     end
  276.   else
  277.     intBar:triggerUpdate()
  278.     intBar:triggerUpdate( "3/4 HTTP api is disabled" )
  279.     sleep(1)
  280.     intBar:triggerUpdate( "4/4 Done." )
  281.   end
  282.   sleep(2)
  283. end
  284.  
  285. os.unloadAPI(path.."/CCiri_Files/load")
  286. os.loadAPI(path.."/CCiri_Files/load")
  287.  
  288. -- Load bar API by TheOriginalBit
  289. if term.isColor and term.isColor() then -- this allows it to work on CC1.3 AND CC1.4
  290.   intBar = load.init( load.LOGO_IS_LOAD, logo, 4, nil, screenY - 5, colors.red, "Starting...", " ", "CCiri " .. version )
  291. else
  292.   intBar = load.init( load.ASCII, logo, 4, nil, sizeY - 5, "Starting...", " ", "CCiri".. version )
  293. end
  294. local function loadBar()
  295.   intBar:run( true )
  296. end
  297. parallel.waitForAll( loadBar, checkForUpdate )
  298. term.setBackgroundColour(colours.black)
  299. term.setTextColour(colours.white)
  300. term.clear()
  301.  
  302. -- Startup
  303. while running do
  304.   cScreen(1, 1)
  305.   line(1) printC("CCiri V "..version, 2) line(3) line(17) line(19)
  306.   for i = 1, #t_mic do
  307.     term.setCursorPos((screenX - #t_mic[i])/2, i+3)
  308.     write(t_mic[i])
  309.   end
  310.   term.setCursorPos(1,18)
  311.   write("> ")
  312.   input = read()
  313.   cScreen(1,1)
  314.   line(1)  printC("CCiri V "..version, 2) line(3)
  315.   write("Input: "..input, 5)
  316.   progress, tab = getMessage(input)
  317.   if progress then printMessage(tab) end
  318. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement