Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- User Options
- side = "left"
- username = "gecko"
- -- 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 table.append(t1, t2)
- for i=1,#t2 do
- t1[#t1+i]=t2[i]
- end
- 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 typerowRefresh()
- 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
- term.setCursorPos(cursorCol, cursorRow)
- -- term.setCursorBlink(true)
- 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
- function queueRefresh()
- local qHeight = height-typerowHeight-1
- local qText = getQueueText()
- local qToprow = math.max(0, #qText - qHeight)
- term.setCursorPos(1,1)
- --term.clearLines(math.min(qHeight, #qText))
- for i=1,#qText do
- term.write(qText[i])
- end
- end
- function sendMessage(message)
- local msg = {}
- msg.type = "text"
- msg.sender = username
- msg.text = message
- table.insert(queue, msg)
- queueRefresh()
- end
- -- queue state
- queue = {}
- -- typerow state
- typerow = ""
- typerowHeight = 1
- typerowCursor = 0
- -- init display
- typerowRefresh()
- -- 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.right then
- if typerowCursor < #typerow then
- typerowCursor = typerowCursor+1
- typerowRefresh()
- end
- elseif p1 == keys.left then
- if typerowCursor > 0 then
- typerowCursor = typerowCursor-1
- typerowRefresh()
- end
- elseif p1 == keys.home then
- typerowCursor = 0
- typerowRefresh()
- elseif p1 == keys["end"] then
- typerowCursor = #typerow
- typerowRefresh()
- 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
- typerowRefresh()
- end
- elseif p1 == keys.enter then
- if typerow == "q" then return end
- sendMessage(typerow)
- typerow = ""
- typerowCursor = 0
- typerowRefresh()
- end
- elseif event == "char" then
- -- Printable key pressed
- -- Append char to typerow
- typerow = string.insert(typerow,p1,typerowCursor)
- typerowCursor = typerowCursor+1
- typerowRefresh()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement