Advertisement
TheRockettek

Untitled

May 27th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1.  
  2. bufferAPI = {}
  3. bufferAPI.term = _G.term
  4.  
  5. -- Colour Manager
  6.  
  7. bufferAPI.colour = {}
  8. bufferAPI.colour.tohex = {[1] = "0",[2] = "1",[4] = "2",[8] = "3",[16] = "4",[32] = "5",[64] = "6",[128] = "7",[256] = "8",[512] = "9",[1024] = "a",[2048] = "b",[4098] = "c",[8192] = "d",[16384] = "e",[32768] = "f"}
  9. bufferAPI.colour.todec = {}
  10. for i,k in pairs(bufferAPI.colour.tohex) do
  11. bufferAPI.colour.todec[k] = i
  12. end
  13. -- Buffer Manager
  14.  
  15. bufferAPI.buffer = {}
  16. bufferAPI.buffer.textColour = bufferAPI.colour.tohex[term.getTextColour()]
  17. bufferAPI.buffer.backColour = bufferAPI.colour.tohex[term.getBackgroundColour()]
  18. bufferAPI.buffer.cursorX = 1
  19. bufferAPI.buffer.cursorY = 1
  20. bufferAPI.buffer.width , bufferAPI.buffer.height = term.getSize()
  21.  
  22. -- Default Term
  23. function bufferAPI.getCursorPos()
  24. return bufferAPI.buffer.cursorX , bufferAPI.buffer.cursorY
  25. end
  26.  
  27. function bufferAPI.current()
  28. return bufferAPI
  29. end
  30.  
  31. function bufferAPI.getBackgroundColour()
  32. return bufferAPI.buffer.backColour
  33. end
  34.  
  35. function bufferAPI.getTextColour()
  36. return bufferAPI.buffer.textColour
  37. end
  38.  
  39. function bufferAPI.scroll(nA)
  40. --WIP
  41. end
  42.  
  43. function bufferAPI.redirect(tT)
  44. return bufferAPI.term.redirect(tT)
  45. end
  46.  
  47. function bufferAPI.setTextColour(nC)
  48. if type(nC) == "number" then
  49. bufferAPI.buffer.textColour = bufferAPI.colour.tohex[nC]
  50. return bufferAPI.term.setTextColour(nC)
  51. elseif type(nC) == "string" then
  52. bufferAPI.buffer.textColour = nC
  53. return bufferAPI.term.setTextColour(bufferAPI.colour.todec[nC])
  54. end
  55. end
  56.  
  57. function bufferAPI.native()
  58. return bufferAPI.term.native
  59. end
  60.  
  61. function bufferAPI.getSize()
  62. return bufferAPI.buffer.width , bufferAPI.buffer.height
  63. end
  64.  
  65. function bufferAPI.write(sText)
  66. for i=1,#sText do
  67. if bufferAPI.main[bufferAPI.buffer.cursorY][bufferAPI.buffer.cursorX+i-1] then
  68. bufferAPI.main[bufferAPI.buffer.cursorY][bufferAPI.buffer.cursorX+i-1] = bufferAPI.buffer.textColour .. bufferAPI.buffer.backColour .. sText:sub(i,i)
  69. end
  70. end
  71. bufferAPI.buffer.cursorX = bufferAPI.buffer.cursorX + #sText
  72. return bufferAPI.term.write(sText)
  73. end
  74.  
  75.  
  76. function bufferAPI.setCursorPos(nX,nY)
  77. bufferAPI.buffer.cursorX = nX
  78. bufferAPI.buffer.cursorY = nY
  79. return bufferAPI.term.setCursorPos(nX,nY)
  80. end
  81.  
  82. function bufferAPI.clear()
  83. for y=1,bufferAPI.buffer.height do
  84. bufferAPI.main[y] = {}
  85. for x=1,bufferAPI.buffer.width do
  86. bufferAPI.main[y][x] = bufferAPI.buffer.textColour .. bufferAPI.buffer.backColour .. " "
  87. end
  88. end
  89. end
  90.  
  91. function bufferAPI.setCursorBlink(bA)
  92. return bufferAPI.term.setCursorBlink(bA)
  93. end
  94.  
  95. function bufferAPI.blit(sText,sTextC,sBackC)
  96. if not ((#sTextC == #sText) and (#sBackC == #sTextC)) then
  97. error("Arguments must be the same length")
  98. end
  99. for i=1,#sText do
  100. if bufferAPI.main[bufferAPI.buffer.cursorY][bufferAPI.buffer.cursorX+i-1] then
  101. bufferAPI.main[bufferAPI.buffer.cursorY][bufferAPI.buffer.cursorX+i-1] = sTextC:sub(i,i) .. sTextB:sub(i,i) .. sText:sub(i,i)
  102. end
  103. end
  104. bufferAPI.buffer.cursorX = bufferAPI.buffer.cursorX + #sText
  105. return bufferAPI.term.blit(sText,sTextC,sBackC)
  106. end
  107.  
  108. function bufferAPI.setBackgroundColour(nC)
  109. if type(nC) == "number" then
  110. bufferAPI.buffer.backColour = bufferAPI.colour.tohex[nC]
  111. return bufferAPI.term.setBackgroundColour(nC)
  112. elseif type(nC) == "string" then
  113. bufferAPI.buffer.backColour = nC
  114. return bufferAPI.term.setBackgroundColour(bufferAPI.colour.todec[nC])
  115. end
  116. end
  117.  
  118. function bufferAPI.clearLine()
  119. for x=1,bufferAPI.buffer.width do
  120. bufferAPI.main[bufferAPI.buffer.cursorY][x] = bufferAPI.buffer.textColour .. bufferAPI.buffer.backColour .. " "
  121. end
  122. return buffer.term.clearLine()
  123. end
  124.  
  125. function bufferAPI.isColour()
  126. return buffer.term.isColour()
  127. end
  128.  
  129. bufferAPI.isColor = bufferAPI.isColour
  130. bufferAPI.setTextColor = bufferAPI.setTextColour
  131. bufferAPI.getTextColor = bufferAPI.getTextColor
  132. bufferAPI.setBackgroundColor = bufferAPI.setBackgroundColour
  133. bufferAPI.getBackgroundColor = bufferAPI.getBackgroundColor
  134.  
  135. -- Generation
  136. bufferAPI.main = {}
  137. for y=1,bufferAPI.buffer.height do
  138. bufferAPI.main[y] = {}
  139. for x=1,bufferAPI.buffer.width do
  140. bufferAPI.main[y][x] = bufferAPI.buffer.textColour .. bufferAPI.buffer.backColour .. " "
  141. end
  142. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement