Advertisement
Guest User

net1

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