Advertisement
Guest User

Rainbow

a guest
Jun 3rd, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.84 KB | None | 0 0
  1. --
  2. -- Rainbow
  3. -- @author LeshaInc
  4. --
  5.  
  6.  
  7. local gpu = require("component").gpu
  8.  
  9. local core = {
  10.   print = function(...)
  11.     local text = table.concat({...}, "")
  12.     text = "\\F+FFFFFF\\\\B+000000\\" .. text
  13.     for _type, _color, __p in string.gmatch(text, "\\([FB])%+(......)\\()") do
  14.       local substr = text:sub(__p, -1)
  15.       local _text, gPtrn = substr:match("(.-)(\\[FB]%+......\\)")
  16.       _text = _text or (gPtrn and "" or substr)
  17.       if _type == "F" then
  18.         gpu.setForeground(tonumber(_color, 16))
  19.       else
  20.         gpu.setBackground(tonumber(_color, 16))
  21.       end
  22.       io.write(_text)
  23.     end
  24.   end,
  25.   colorize = function (color, fg)
  26.     local hexstr = '0123456789ABCDEF'
  27.     local s = ''
  28.    
  29.     while color > 0 do
  30.       local mod = math.fmod(color, 16)
  31.       s = string.sub(hexstr, mod+1, mod+1) .. s
  32.       color = math.floor(color / 16)
  33.     end
  34.    
  35.     for i=1, 6 - #s do
  36.       s = s .. "0"
  37.     end
  38.     return function(t)
  39.       return "\\" .. (fg and "F" or "B") .. "+" .. s .. "\\" .. t
  40.     end
  41.   end,
  42.   colors = {
  43.     background = {
  44.       black      = 0x000000,
  45.       gray       = 0x424242,
  46.       white      = 0xFFFFFF,
  47.       red        = 0xCE0202,
  48.       green      = 0x02CE02,
  49.       yellow     = 0xCECE02,
  50.       blue       = 0x0202CE,
  51.       violet     = 0xCE02CE,
  52.       light_blue = 0x02CECE,
  53.     },
  54.     foreground = {
  55.       black      = 0x000000,
  56.       gray       = 0x424242,
  57.       white      = 0xFFFFFF,
  58.       red        = 0xCE0202,
  59.       green      = 0x02CE02,
  60.       yellow     = 0xCECE02,
  61.       blue       = 0x0202CE,
  62.       violet     = 0xCE02CE,
  63.       light_blue = 0x02CECE,
  64.     }
  65.   }
  66. }
  67. local core_meta = {
  68.   __call = function (self, ...)
  69.     rawget(self, "print")(...)
  70.   end,
  71.   __index = function (self, k)
  72.     if type(rawget(self, k)) ~= "function" and
  73.        rawget(self, "colors")["foreground"][k:sub(4, -1)] or
  74.        rawget(self, "colors")["background"][k:sub(4, -1)] then
  75.       if k:sub(1, 3) == "fg_" then
  76.         return rawget(self, "colorize")
  77.               (rawget(self, "colors")["foreground"][k:sub(4, -1)], true)
  78.       elseif k:sub(1, 3) == "bg_" then
  79.         return rawget(self, "colorize")
  80.               (rawget(self, "colors")["background"][k:sub(4, -1)])
  81.       end
  82.     end
  83.     return rawget(self, k)
  84.   end
  85. }
  86. setmetatable(core, core_meta)
  87.  
  88. local buffer = function (options)
  89.   local self = {}
  90.  
  91.   options = options or {}
  92.   options.colors_ext = options.colors_ext or {}
  93.   options.colors_ext.foreground = options.colors_ext.foreground or {}
  94.   options.colors_ext.background = options.colors_ext.background or {}
  95.  
  96.   local colors = options.colors or core.colors
  97.   local patterns = options.patterns or {}
  98.  
  99.   for k, v in pairs(options.colors_ext.foreground) do
  100.     colors.foreground[k] = v
  101.   end
  102.  
  103.   for k, v in pairs(options.colors_ext.background) do
  104.     colors.background[k] = v
  105.   end
  106.  
  107.   self.colorize = core.colorize
  108.  
  109.   function self.print(ispattern, pattern, ...)
  110.     if type(ispattern) == "string" then
  111.       core.print(ispattern, pattern, ...)
  112.     else
  113.       if patterns[pattern] then
  114.         core.print(patterns[pattern](self, ...))
  115.       else
  116.         error("no such pattern")
  117.       end
  118.     end
  119.   end
  120.  
  121.   setmetatable(self, {
  122.     __call = function (tbl, ...)
  123.       self.print(...)
  124.     end,
  125.     __index = function (tbl, key)
  126.       if type(rawget(self, key)) ~= "function" then
  127.         if colors.foreground[key:sub(4, -1)] or
  128.            colors.background[key:sub(4, -1)] then
  129.           if key:sub(1, 3) == "fg_" then
  130.             return core.colorize(colors.foreground[key:sub(4, -1)], true)
  131.           else
  132.             return core.colorize(colors.background[key:sub(4, -1)])
  133.           end
  134.         end
  135.       end
  136.       return rawget(self, key)
  137.     end
  138.   })
  139.  
  140.   return self
  141. end
  142.  
  143. return {
  144.   core = core,
  145.   buffer = buffer
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement