Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- User Options
- side = "left"
- username = "gecko"
- protocol = "sk"
- -- Initialize terminal
- term.setBackgroundColor(colours.black)
- term.clear()
- width, height = term.getSize()
- term.setCursorPos(1,height)
- term.setCursorBlink(true)
- -- Initialize rednet
- rednet.open(side)
- -- Helpers
- function dprint(text)
- term.clear()
- term.setCursorPos(1,1)
- print(text)
- os.pullEvent("terminate")
- end
- function table.append(t1, t2)
- local length = #t1
- for i=1,#t2 do
- t1[length+i]=t2[i]
- end
- end
- function table.contains(t, e)
- for _, value in pairs(t) do
- if value == e then return true end
- end
- return false
- end
- function string.insert(s,a,i)
- return string.sub(s,1,i)..a..string.sub(s,i+1,-1)
- end
- function term.clearLines(n)
- local cx,cy = term.getCursorPos()
- for i=cy,cy+n-1 do
- term.setCursorPos(1,i)
- term.clearLine()
- end
- term.setCursorPos(cx,cy)
- end
- function getTextHeight(text,endLineDrop)
- -- Calculates number of lines text would take to
- -- print. endLineDrop controls wether text that
- -- ends on the end of a line will count as
- -- another row or not.
- local nLines = (#text)/width
- if endLineDrop then
- return 1+math.floor(nLines)
- else
- return math.ceil(nLines)
- end
- end
- function getTextLines(text)
- local tLines = {}
- local nLines = getTextHeight(text, false)
- for i=1,nLines do
- local line = string.sub(text,(i-1)*width+1,math.min(i*width,#text))
- table.insert(tLines, line)
- end
- return tLines
- end
- -- Supporting Functions
- function timestamp()
- return "[D"..os.day().." "..textutils.formatTime(os.time(), true).."]"
- end
- function getMessageText(msg)
- if msg.type == "text" then
- return "["..msg.sender.."] "..msg.text
- end
- end
- function refresh()
- term.clear()
- typerowHeight = getTextHeight(typerow, true)
- local cursorRow = height-(typerowHeight-1)+math.floor(typerowCursor/width)
- local cursorCol = 1+(typerowCursor%width)
- term.setCursorPos(1,height-typerowHeight)
- --term.clearLines(typerowHeight+1)
- term.write(string.rep("-",width))
- for i=1,typerowHeight do
- term.setCursorPos(1,height-typerowHeight+i)
- term.write(string.sub(typerow,(i-1)*width+1,math.min(i*width,#typerow)))
- end
- queueRefresh()
- term.setCursorPos(cursorCol, cursorRow)
- end
- function getQueueSize()
- local size = 0
- for i=1,#queue do
- local text = getMessageText(queue[i])
- local msgHeight = getTextHeight(text, false)
- size = size + msgHeight
- end
- return size
- end
- function getQueueText()
- local qText = {}
- for i=1,#queue do
- local text = getMessageText(queue[i])
- local tLines = getTextLines(text)
- table.append(qText, tLines)
- end
- return qText
- end
- leq = ""
- function queueRefresh()
- local qHeight = height-typerowHeight-1
- local qText = getQueueText()
- local qToprow = math.max(0, #qText - qHeight)+1
- term.setCursorPos(1,1)
- --term.clearLines(math.min(qHeight, #qText))
- for i=qToprow,#qText do
- print(qText[i])
- end
- leq = qText[#qText]
- end
- function pushMessage(msg)
- table.insert(queue, msg)
- refresh()
- end
- -- queue state
- queue = {}
- -- typerow state
- typerow = ""
- typerowHeight = 1
- typerowCursor = 0
- -- init
- refresh()
- users = {}
- rednet.broadcast({type="login",name=username},"sk")
- -- Main event loop
- while true do
- -- Get next event
- local event, p1, p2, p3 = os.pullEventRaw()
- -- Event switch case
- if event == "terminate" then
- -- Termination signal sent (Ctrl+T)
- -- Logoff and exit program
- return
- elseif event == "key" then
- -- Key pressed
- -- Handle special keys, printable keys handled in char
- if p1 == keys.one then
- --dprint()
- elseif p1 == keys.right then
- if typerowCursor < #typerow then
- typerowCursor = typerowCursor+1
- refresh()
- end
- elseif p1 == keys.left then
- if typerowCursor > 0 then
- typerowCursor = typerowCursor-1
- refresh()
- end
- elseif p1 == keys.home then
- typerowCursor = 0
- refresh()
- elseif p1 == keys["end"] then
- typerowCursor = #typerow
- refresh()
- elseif p1 == keys.backspace then
- if typerowCursor > 0 then
- typerow = string.sub(typerow,1,typerowCursor-1)..string.sub(typerow,typerowCursor+1,-1)
- typerowCursor = typerowCursor-1
- refresh()
- end
- elseif p1 == keys.enter then
- if typerow == "q" then return end
- if typerow ~= "" then
- pushMessage({type="text",sender="username",text=typerow})
- sendMessage
- typerow = ""
- typerowCursor = 0
- end
- end
- elseif event == "char" then
- -- Printable key pressed
- -- Append char to typerow
- typerow = string.insert(typerow,p1,typerowCursor)
- typerowCursor = typerowCursor+1
- refresh()
- elseif table.contains({"key_up","mouse_scroll","mouse_click","mouse_drag","mouse_up"},event) then
- -- nothing
- elseif event == "rednet_message" then
- if users[p1] == nil then
- if p2.type == "ack" then
- users[p1] = p2.name
- elseif p2.type == "login":
- rednet.send(p1, {type="ack",name=username} , protocol)
- users[p1] = p2.name
- end
- else
- if p2.type == "message" then
- pushMessage({type="text",sender=users[p1],text=p2.content})
- end
- end
- else
- pushMessage("event", table.concat({event,p1,p2,p3}," "))
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement