einsteinK

CCraft - Terminal Bridge

Mar 15th, 2014
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local p = peripheral.wrap("right")
  2. p.clear()
  3. local mon = {}
  4. local oterm = term
  5. local mon2 = setmetatable({},{
  6.   __index = function(s,k)
  7.     local a = mon[k]
  8.     local b = oterm[k]
  9.     return function(...)
  10.       if a and b then
  11.         pcall(b,...) return select(2,pcall(a,...))
  12.       elseif a or b then
  13.         return select(2,pcall(a or b,...))
  14.       end
  15.     end
  16.   end
  17. })
  18.  
  19. _G.term = mon2
  20.  
  21. local w = 0xffffff
  22. p.addGradientBox(266, 135, 1, 10, w, 255, w, 255, 1)
  23. p.addGradientBox(262, 139, 10, 1, w, 255, w, 255, 1)
  24.  
  25. mon.visible = true
  26. mon.minY = 6 * 9
  27. mon.maxY = 24 * 9 + 4 + 9
  28. mon.minX = 0 * 6
  29. mon.maxX = 68 * 6 + 4 - 17 * 6
  30. mon.rows = math.floor((mon.maxY - mon.minY) / 9)
  31. mon.cols = math.floor((mon.maxX - mon.minX) / 6)
  32. mon.lines = {}
  33. mon.mtrx = {}
  34. for i = 1, mon.rows do
  35.  mon.mtrx[i] = {}
  36. end
  37. mon.scale = 1
  38. mon.curr = 1
  39. mon.curc = 1
  40. mon.cur = p.addText(math.floor((mon.curc - 1) * 6 * mon.scale) + mon.minX + 2, math.floor(((mon.curr - 1) * 9 + 1) * mon.scale) + mon.minY + 2, " ", 0)
  41. mon.colors = {[1] = 0xffffff, [2] = 0xffc125, [4] = 0xff3e96, [8] = 0x836fff, [16] = 0xffff00, [32] = 0x00ff00, [64] = 0xff82ab, [128] = 0x444444, [256] = 0x999999, [512] = 0x009acd, [1024] = 0x9400d3, [2048] = 0x0000ff, [4096] = 0x8b5a2b, [8192] = 0x458b00, [16384] = 0xff0000, [32768] = 0}
  42. mon.stat = p.addText(0,mon.minY-18,"Loading...",w)
  43.  
  44. mon.cur.setColor(0xffffff)
  45.  
  46. mon.bgColor = 0
  47. mon.color = 0xffffff
  48. mon.box = p.addBox(mon.minX, mon.minY, mon.maxX - mon.minX + 1, mon.maxY - mon.minY + 1, mon.bgColor, 0)
  49. mon.box.setZIndex(-1)
  50.  
  51. mon.write = function(s)
  52.   if omon then omon.write(s) end
  53.   for i = 1, #s do
  54.   local x = math.floor((mon.curc - 1) * 6 * mon.scale) + mon.minX + 2
  55.   local y = math.floor((mon.curr - 1) * 9 * mon.scale) + mon.minY + 2
  56.   local j = 1
  57.   local t = mon.mtrx[mon.curr][mon.curc]
  58.   if t then
  59.    t.delete()
  60.   end
  61.   t = p.addText(x, y, s:sub(i,i), mon.color)
  62.   mon.mtrx[mon.curr][mon.curc] = t
  63.   mon.lines[#mon.lines + 1] = t
  64.   mon.curc = mon.curc + 1
  65.   if mon.curc > mon.cols then
  66.    mon.curc = 1
  67.    if mon.curr == mon.rows then
  68.     return
  69.    else
  70.     mon.curr = mon.curr + 1
  71.    end
  72.   end
  73.  end
  74.  mon.cur.setX(math.floor((mon.curc - 1) * 6 * mon.scale) + mon.minX + 2)
  75.  mon.cur.setY(math.floor(((mon.curr - 1) * 9 + 1) * mon.scale) + mon.minY + 2)
  76. end
  77.  
  78. mon.clear = function()
  79.  if omon then omon.clear() end
  80.  for i = 1, #mon.lines do
  81.   mon.lines[i].delete()
  82.   mon.lines[i] = nil
  83.  end
  84. end
  85.  
  86. mon.clearLine = function(n)
  87.  if omon then omon.clearLine() end
  88.  local y = math.floor(((n or mon.curr) - 1) * 9 * mon.scale) + mon.minY + 2
  89.  local i = 1
  90.  while i <= #mon.lines do
  91.   if not mon.lines[i].getY() or mon.lines[i].getY() == y then
  92.    mon.lines[i].delete()
  93.    mon.lines[i] = mon.lines[#mon.lines]
  94.    mon.lines[#mon.lines] = nil
  95.   else
  96.    i = i + 1
  97.   end
  98.  end
  99. end
  100.  
  101. mon.scroll = function()
  102.  if omon then omon.scroll() end
  103.  mon.clearLine(1)
  104.  local i = 1
  105.  while i <= #mon.lines do
  106.   if not mon.lines[i].getY() then
  107.    mon.lines[i].delete()
  108.    mon.lines[i] = mon.lines[#mon.lines]
  109.    mon.lines[#mon.lines] = nil
  110.   else
  111.    mon.lines[i].setY(mon.lines[i].getY() - (9 * mon.scale))
  112.    i = i + 1
  113.   end
  114.  end
  115.  for i = 1, mon.rows - 1 do
  116.   mon.mtrx[i] = mon.mtrx[i + 1]
  117.  end
  118.  mon.mtrx[mon.rows] = {}
  119. end
  120.  
  121. mon.setBackgroundColor = function(n)
  122.  if omon then omon.setBackgroundColor(n) end
  123.  if mon.colors[n] then
  124.   n = mon.colors[n]
  125.  end
  126.  mon.bgColor = n
  127.  mon.box.setColor(n)
  128.  mon.box.setOpacity(n == 0 and 0 or 0.40)
  129. end
  130.  
  131. mon.setBackgroundColour = mon.setBackgroundColor
  132.  
  133. mon.setTextColor = function(n)
  134.  if mon.colors[n] then
  135.   n = mon.colors[n]
  136.  end
  137.  if omon then omon.setTextColor(n) end
  138.  mon.color = n
  139.  mon.cur.setColor(n)
  140. end
  141.  
  142. mon.setTextColour = mon.setTextColor
  143.  
  144. mon.setTextScale = function(n)
  145.  if omon then omon.setTextScale(n) end
  146.  mon.rows = math.floor((mon.maxY - mon.minY - 4)/(9 * n))
  147.  mon.cols = math.floor((mon.maxX - mon.minX - 4)/(6 * n))
  148.  local y = nil
  149.  local x = nil
  150.  if n > mon.scale then
  151.   y = math.floor((mon.rows + 1) * 9 * mon.scale) + mon.minY + 2
  152.   x = math.floor((mon.cols + 1) * 6 * mon.scale) + mon.minX + 2
  153.  end
  154.  local i = 1
  155.  while i < #mon.lines do
  156.   if not mon.lines.getY() or y and (mon.lines[i].getY() >= y or mon.lines[i].getX() >= x) then
  157.    mon.lines[i].delete()
  158.    mon.lines[i] = mon.lines[#mon.lines]
  159.    mon.lines[#mon.lines] = nil
  160.   else
  161.    local ny = (mon.lines[i].getY() - mon.minY - 2) / mon.scale * n
  162.    local nx = (mon.lines[i].getX() - mon.minX - 2) / mon.scale * n
  163.    mon.lines[i].setY(ny)
  164.    mon.lines[i].setX(nx)
  165.    mon.lines[i].setScale(n)
  166.    i = i + 1
  167.   end
  168.  end
  169.  for i = 1, mon.rows do
  170.   if not mon.mtrx[i] then
  171.    mon.mtrx[i] = {}
  172.   end
  173.  end
  174.  mon.scale = n
  175.  mon.cur.setX(math.floor((mon.curc - 1) * 6 * mon.scale) + mon.minX + 2)
  176.  mon.cur.setY(math.floor(((mon.curr - 1) * 9 + 1) * mon.scale) + mon.minY + 2)
  177. end
  178.  
  179. mon.setCursorPos = function(x,y)
  180.  if omon then omon.setCursorPos(x,y) end
  181.  mon.curc = x
  182.  mon.curr = y
  183.  mon.cur.setX(math.floor((mon.curc - 1) * 6 * mon.scale) + mon.minX + 2)
  184.  mon.cur.setY(math.floor(((mon.curr - 1) * 9 + 1) * mon.scale) + mon.minY + 2)
  185. end
  186.  
  187. mon.getCursorPos = function()
  188.  return mon.curc, mon.curr
  189. end
  190.  
  191. mon.getSize = function()
  192.  return mon.cols, mon.rows
  193. end
  194.  
  195. local blink = true
  196. local blinkstate = false
  197. local nextBlink = os.startTimer(0.3)
  198. mon.setCursorBlink = function(b)
  199.  if omon then omon.setCursorBlink(b) end
  200.  blink = b
  201. end
  202.  
  203. local RMON,FMON = mon,{}
  204. local cell = peripheral.wrap("left")
  205.  
  206. local function doBlink()
  207.  blinkstate = not blinkstate
  208.  RMON.cur.setText(blinkstate and blink and "_" or " ")
  209.  nextBlink = os.startTimer(0.3)
  210.  local m = cell.getMaxEnergyStored()
  211.  local t = "["..math.floor(os.clock()).."s] "
  212.  t = t..cell.getEnergyStored().."/"..m
  213.  mon.stat.setText(t.." MJ")
  214. end
  215.  
  216. mon.isColor = function() return true end
  217. mon.isColour = function() return true end
  218.  
  219. local pullEvent = os.pullEvent
  220. local charQueue = ""
  221. os.pullEvent = function(...)
  222.  
  223.  return select(2,pcall(function(...)
  224.  
  225.  while true do
  226.   if charQueue ~= "" then
  227.    local c = charQueue:sub(1,1)
  228.    charQueue = charQueue:sub(2)
  229.    if c == "\\" then
  230.     c = charQueue:sub(1,1)
  231.     charQueue = charQueue:sub(2)
  232.     if c == "\\" then
  233.      return "char",c
  234.     elseif c == "n" then
  235.      return "key",28
  236.     elseif c == "b" then
  237.      return "key",14
  238.     elseif c == "t" then
  239.      error("Terminated")
  240.     elseif c == "r" then
  241.      os.reboot()
  242.     elseif c == "s" then
  243.      os.shutdown()
  244.     end
  245.    end
  246.    return "char",c
  247.   end
  248.   local t = {pullEvent(...)}
  249.   if t[1] == "chat_command" then
  250.    charQueue = charQueue..t[2]
  251.    local c = charQueue:sub(1,1)
  252.    charQueue = charQueue:sub(2)
  253.    if c == "\\" then
  254.     charQueue = c..charQueue
  255.    elseif t[2] == "$$" then
  256. --    term.restore()
  257. --    p.clear() term.clear()
  258. --    term.setCursorPos(1,1)
  259.      mon.visible = not mon.visible
  260.      mon = mon == RMON and FMON or RMON
  261.      if mon == FMON then mon.clear() end
  262.      charQueue = "" return "none"
  263.    else
  264.     return "char",c
  265.    end
  266.   elseif t[1] == "timer" and t[2] == nextBlink then
  267.    doBlink()
  268.   else
  269.    return unpack(t)
  270.   end
  271.  end
  272.  
  273.  end,...))
  274.  
  275. end
  276.  
  277. doBlink()
  278.  
  279. local reboot = os.reboot
  280. os.reboot = function()
  281.  p.clear()
  282.  mon.box.delete()
  283.  mon.cur.delete()
  284.  reboot()
  285. end
  286.  
  287. local shutdown = os.shutdown
  288. os.shutdown = function()
  289.  p.clear()
  290.  shutdown()
  291. end
  292.  
  293. _G.term.setCursorPos(1,1)
  294. _G.term.clear() print("Hello!")
  295. print("Bridge by einsteinK")
Add Comment
Please, Sign In to add comment