Advertisement
jesusthekiller

Palette

Apr 18th, 2013
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.17 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.2
  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.  
  44. local DO_NOT_TOUCH = 4
  45.  
  46. -- Updater func:
  47.  
  48. function update()
  49.     if not http then
  50.         error("HTTP API required!")
  51.     end
  52.    
  53.     cprint("&5Checking for &4cprint &5update...")
  54.    
  55.     local v = http.get("http://mindblow.no-ip.org/code/cprint/ver")
  56.    
  57.     if v == nil then
  58.         error("nil response from update server! Check your internet connection!")
  59.     end
  60.    
  61.     if tonumber(v.readLine()) <= DO_NOT_TOUCH then
  62.         cprint("&5No new updates!")
  63.         return false
  64.     end
  65.    
  66.     -- Install cPrint
  67.    
  68.     cprint("&5Downloading &4cPrint&5...")
  69.    
  70.     local f = http.get(v.readLine())
  71.    
  72.     cprint("&5Installing &4cPrint&5 as &4cprint &5in main directory...")
  73.    
  74.     local e = fs.open("cprint", "w") -- API Folder
  75.    
  76.     e.write(f.readAll())
  77.    
  78.     e.close()
  79.     f.close()
  80.    
  81.     -- Install palette
  82.    
  83.     cprint("&5Downloading &4Palette&5...")
  84.    
  85.     local f = http.get(v.readLine())
  86.    
  87.     v.close()
  88.    
  89.     cprint("&5Installing &4Palette&5 as &4palette &5in main directory...")
  90.    
  91.     local e = fs.open("palette", "w") -- API Folder
  92.    
  93.     e.write(f.readAll())
  94.    
  95.     e.close()
  96.     f.close()
  97.    
  98.     v.close()
  99.     print("Done!")
  100.     return true
  101. end
  102.  
  103. function sc(x, y)
  104.     term.setCursorPos(x, y)
  105. end
  106.  
  107. function clear(move)
  108.     sb(colors.black)
  109.     term.clear()
  110.     if move ~= false then sc(1,1) end
  111. end
  112.  
  113. function sb(color)
  114.     term.setBackgroundColor(color)
  115. end
  116.  
  117. function st(color)
  118.     term.setTextColor(color)
  119. end
  120.  
  121. function cCode(h)
  122.     if term.isColor() then
  123.         return 2 ^ (tonumber(h, 16) or 0)
  124.     else
  125.         if h == "f" then
  126.             return colors.black
  127.         else
  128.             return colors.white
  129.         end
  130.     end
  131. end
  132.  
  133. function toCode(n)
  134.     return string.format('%x', n)
  135. end
  136.  
  137. function cwrite(text)
  138.     text = tostring(text)
  139.    
  140.     local i = 0
  141.     while true  do
  142.         i = i + 1
  143.         if i > #text then break end
  144.        
  145.         local c = text:sub(i, i)
  146.  
  147.         if c == "\\" then
  148.             if text:sub(i+1, i+1) == "&" then
  149.                 write("&")
  150.                 i = i + 1
  151.             elseif text:sub(i+1, i+1) == "$" then
  152.                 write("$")
  153.                 i = i + 1
  154.             else
  155.                 write(c)
  156.             end
  157.         elseif c == "&" then
  158.             st(cCode(text:sub(i+1, i+1)))
  159.             i = i + 1
  160.         elseif c == "$" then
  161.             sb(cCode(text:sub(i+1, i+1)))
  162.             i = i + 1
  163.         else
  164.             write(c)
  165.         end
  166.     end
  167.    
  168.     return
  169. end
  170.  
  171. function cprint(text)
  172.     return cwrite(tostring(text).."\n")
  173. end
  174.  
  175. if auto then update() end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement