Advertisement
Guest User

net1

a guest
Apr 21st, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.44 KB | None | 0 0
  1. -- User Options
  2. side = "left"
  3. username = "gecko"
  4.  
  5. -- Initialize terminal
  6. term.setBackgroundColor(colours.black)
  7. term.clear()
  8. width, height = term.getSize()
  9. term.setCursorPos(1,height)
  10. term.setCursorBlink(true)
  11.  
  12. -- Initialize rednet
  13. rednet.open(side)
  14.  
  15. -- Helpers
  16.  
  17. function table.append(t1, t2)
  18.   for i=1,#t2 do
  19.     t1[#t1+i]=t2[i]
  20.   end
  21. end
  22.  
  23. function string.insert(s,a,i)
  24.   return string.sub(s,1,i)..a..string.sub(s,i+1,-1)
  25. end
  26.  
  27. function term.clearLines(n)
  28.   local cx,cy = term.getCursorPos()
  29.   for i=cy,cy+n-1 do
  30.     term.setCursorPos(1,i)
  31.     term.clearLine()
  32.   end
  33.   term.setCursorPos(cx,cy)
  34. end
  35.  
  36. function getTextHeight(text,endLineDrop)
  37.   -- Calculates number of lines text would take to
  38.   -- print. endLineDrop controls wether text that
  39.   -- ends on the end of a line will count as
  40.   -- another row or not.
  41.   local nLines = (#text)/width
  42.   if endLineDrop then
  43.     return 1+math.floor(nLines)
  44.   else
  45.     return math.ceil(nLines)
  46.   end
  47. end
  48.  
  49. function getTextLines(text)
  50.   local tLines = {}
  51.   local nLines = getTextHeight(text, false)
  52.   for i=1,nLines do
  53.     local line = string.sub(text,(i-1)*width+1,math.min(i*width,#text))
  54.     table.insert(tLines, line)
  55.   end
  56.   return tLines
  57. end
  58.  
  59. -- Supporting Functions
  60.  
  61. function timestamp()
  62.   return "[D"..os.day().." "..textutils.formatTime(os.time(), true).."]"
  63. end
  64.  
  65. function getMessageText(msg)
  66.   if msg.type == "text" then
  67.     return "["..msg.sender.."] "..msg.text
  68.   end
  69. end
  70.  
  71. function typerowRefresh()
  72.   typerowHeight = getTextHeight(typerow, true)
  73.   local cursorRow = height-(typerowHeight-1)+math.floor(typerowCursor/width)
  74.   local cursorCol = 1+(typerowCursor%width)
  75.  
  76.   term.setCursorPos(1,height-typerowHeight)
  77.   term.clearLines(typerowHeight+1)
  78.  
  79.   term.write(string.rep("-",width))
  80.   for i=1,typerowHeight do
  81.     term.setCursorPos(1,height-typerowHeight+i)
  82.     term.write(string.sub(typerow,(i-1)*width+1,math.min(i*width,#typerow)))
  83.   end
  84.   term.setCursorPos(cursorCol, cursorRow)
  85.   -- term.setCursorBlink(true)
  86. end
  87.  
  88. function getQueueSize()
  89.   local size = 0
  90.   for i=1,#queue do
  91.     local text = getMessageText(queue[i])
  92.     local msgHeight = getTextHeight(text, false)
  93.     size = size + msgHeight
  94.   end
  95.   return size
  96. end
  97.  
  98. function getQueueText()
  99.   local qText = {}
  100.   for i=1,#queue do
  101.     local text = getMessageText(queue[i])
  102.     local tLines = getTextLines(text)
  103.     table.append(qText, tLines)
  104.   end
  105.   return qText
  106. end
  107.  
  108. function queueRefresh()
  109.   local qHeight = height-typerowHeight-1
  110.   local qText = getQueueText()
  111.   local qToprow = math.max(0, #qText - qHeight)
  112.  
  113.   term.setCursorPos(1,1)
  114.   --term.clearLines(math.min(qHeight, #qText))
  115.   for i=1,#qText do
  116.     term.write(qText[i])
  117.   end
  118. end
  119.  
  120. function sendMessage(message)
  121.   local msg = {}
  122.   msg.type = "text"
  123.   msg.sender = username
  124.   msg.text = message
  125.   table.insert(queue, msg)
  126.   queueRefresh()
  127. end
  128.  
  129. -- queue state
  130. queue = {}
  131.  
  132. -- typerow state
  133. typerow = ""
  134. typerowHeight = 1
  135. typerowCursor = 0
  136.  
  137. -- init display
  138. typerowRefresh()
  139.  
  140. -- Main event loop
  141. while true do
  142.   -- Get next event
  143.   local event, p1, p2, p3 = os.pullEventRaw()
  144.   -- Event switch case
  145.   if event == "terminate" then
  146.     -- Termination signal sent (Ctrl+T)
  147.     -- Logoff and exit program
  148.     return
  149.   elseif event == "key" then
  150.     -- Key pressed
  151.     -- Handle special keys, printable keys handled in char
  152.     if p1 == keys.right then
  153.       if typerowCursor < #typerow then
  154.         typerowCursor = typerowCursor+1
  155.         typerowRefresh()
  156.       end
  157.     elseif p1 == keys.left then
  158.       if typerowCursor > 0 then
  159.         typerowCursor = typerowCursor-1
  160.         typerowRefresh()
  161.       end
  162.     elseif p1 == keys.home then
  163.       typerowCursor = 0
  164.       typerowRefresh()
  165.     elseif p1 == keys["end"] then
  166.       typerowCursor = #typerow
  167.       typerowRefresh()
  168.     elseif p1 == keys.backspace then
  169.       if typerowCursor > 0 then
  170.         typerow = string.sub(typerow,1,typerowCursor-1)..string.sub(typerow,typerowCursor+1,-1)
  171.         typerowCursor = typerowCursor-1
  172.         typerowRefresh()
  173.       end
  174.     elseif p1 == keys.enter then
  175.       if typerow == "q" then return end
  176.       sendMessage(typerow)
  177.       typerow = ""
  178.       typerowCursor = 0
  179.       typerowRefresh()
  180.     end
  181.   elseif event == "char" then
  182.     -- Printable key pressed
  183.     -- Append char to typerow    
  184.     typerow = string.insert(typerow,p1,typerowCursor)
  185.     typerowCursor = typerowCursor+1
  186.     typerowRefresh()
  187.   end
  188. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement