Advertisement
Guest User

Rainbow 2.1

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