Advertisement
jesusthekiller

cPrint

Apr 18th, 2013
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.25 KB | None | 0 0
  1. -- Do auto update?
  2. local auto = true
  3.  
  4. --[[
  5.     ###INFO###
  6.     * Language
  7.        Lua (ComputerCraft)
  8.        
  9.     * Version
  10.        1.3
  11.        
  12.     * Status
  13.        Working, not closed.
  14. ]]--
  15.  
  16. --[[
  17.     cPrint API by Jesusthekiller
  18.     This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
  19.     More info: http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_US
  20. ]]--
  21.  
  22. --[[
  23.     Big thanks to theorginalbit for help with cCode and toCode!
  24. ]]--
  25.  
  26. --[[
  27.     Version log:
  28.     1.0:
  29.      * Initial release
  30.    
  31.     1.1:
  32.      * Added cwrite
  33.      * Added update
  34.      * cprint now adds "\n" to the end of the line
  35.      * Improved cCode
  36.      * Improved toCode
  37.      * Auto updater
  38.      
  39.     1.2:
  40.      * Added non-advanced computer support
  41.      * Bugfixes
  42.  
  43.     1.3:
  44.      * Even better non-advanced computer support
  45. ]]--
  46.  
  47. local DO_NOT_TOUCH = 5
  48.  
  49. -- Updater func:
  50.  
  51. function update()
  52.     if not http then
  53.         error("HTTP API required!")
  54.     end
  55.    
  56.     cprint("&5Checking for &4cprint &5update...")
  57.    
  58.     local v = http.get("http://mindblow.no-ip.org/code/cprint/ver")
  59.    
  60.     if v == nil then
  61.         error("nil response from update server! Check your internet connection!")
  62.     end
  63.    
  64.     if tonumber(v.readLine()) <= DO_NOT_TOUCH then
  65.         cprint("&5No new updates!")
  66.         return false
  67.     end
  68.    
  69.     -- Install cPrint
  70.    
  71.     cprint("&5Downloading &4cPrint&5...")
  72.    
  73.     local f = http.get(v.readLine())
  74.    
  75.     cprint("&5Installing &4cPrint&5 as &4cprint &5in main directory...")
  76.    
  77.     local e = fs.open("cprint", "w") -- API Folder
  78.    
  79.     e.write(f.readAll())
  80.    
  81.     e.close()
  82.     f.close()
  83.    
  84.     -- Install palette
  85.    
  86.     cprint("&5Downloading &4Palette&5...")
  87.    
  88.     local f = http.get(v.readLine())
  89.    
  90.     v.close()
  91.    
  92.     cprint("&5Installing &4Palette&5 as &4palette &5in main directory...")
  93.    
  94.     local e = fs.open("palette", "w") -- API Folder
  95.    
  96.     e.write(f.readAll())
  97.    
  98.     e.close()
  99.     f.close()
  100.    
  101.     v.close()
  102.     print("Done!")
  103.     return true
  104. end
  105.  
  106. function sc(x, y)
  107.     term.setCursorPos(x, y)
  108. end
  109.  
  110. function clear(move)
  111.     sb(colors.black)
  112.     term.clear()
  113.     if move ~= false then sc(1,1) end
  114. end
  115.  
  116. function sb(color)
  117.     term.setBackgroundColor(color)
  118. end
  119.  
  120. function st(color)
  121.     term.setTextColor(color)
  122. end
  123.  
  124. function cCode(h)
  125.     if term.isColor() and term.isColor then
  126.         return 2 ^ (tonumber(h, 16) or 0)
  127.     else
  128.         if h == "f" then
  129.             return colors.black
  130.         else
  131.             return colors.white
  132.         end
  133.     end
  134. end
  135.  
  136. function toCode(n)
  137.     return string.format('%x', n)
  138. end
  139.  
  140. function cwrite(text)
  141.     text = tostring(text)
  142.    
  143.     local i = 0
  144.     while true  do
  145.         i = i + 1
  146.         if i > #text then break end
  147.        
  148.         local c = text:sub(i, i)
  149.  
  150.         if c == "\\" then
  151.             if text:sub(i+1, i+1) == "&" then
  152.                 write("&")
  153.                 i = i + 1
  154.             elseif text:sub(i+1, i+1) == "$" then
  155.                 write("$")
  156.                 i = i + 1
  157.             else
  158.                 write(c)
  159.             end
  160.         elseif c == "&" then
  161.             st(cCode(text:sub(i+1, i+1)))
  162.             i = i + 1
  163.         elseif c == "$" then
  164.             sb(cCode(text:sub(i+1, i+1)))
  165.             i = i + 1
  166.         else
  167.             write(c)
  168.         end
  169.     end
  170.    
  171.     return
  172. end
  173.  
  174. function cprint(text)
  175.     return cwrite(tostring(text).."\n")
  176. end
  177.  
  178. if auto then update() end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement