Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function read( _sReplaceChar, _tHistory, _fEventCallback, _fRedraw )
- term.setCursorBlink( true )
- local sLine = ""
- local nHistoryPos = nil
- local nPos = 0
- if _sReplaceChar then
- _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
- end
- local w, h = term.getSize()
- local sx, sy = term.getCursorPos()
- local function redraw2( _sCustomReplaceChar )
- local nScroll = 0
- if sx + nPos >= w then
- nScroll = (sx + nPos) - w
- end
- term.setCursorPos( sx, sy )
- local sReplace = _sCustomReplaceChar or _sReplaceChar
- if sReplace then
- term.write( string.rep(sReplace, string.len(sLine) - nScroll) )
- else
- term.write( string.sub( sLine, nScroll + 1 ) )
- end
- term.setCursorPos( sx + nPos - nScroll, sy )
- end
- local redraw
- if _fRedraw then
- redraw = function(cc)
- local pos, line = _fRedraw(cc, redraw2, nPos, sLine, sx, sy)
- nPos = pos or nPos
- sLine = line or sLine
- if red then
- redraw()
- end
- end
- else
- redraw = redraw2
- end
- while true do
- local sEvent, param = os.pullEvent()
- if sEvent == "char" then
- sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
- nPos = nPos + 1
- redraw()
- elseif sEvent == "key" then
- if param == keys.enter then
- -- Enter
- break
- elseif param == keys.left then
- -- Left
- if nPos > 0 then
- nPos = nPos - 1
- redraw()
- end
- elseif param == keys.right then
- -- Right
- if nPos < string.len(sLine) then
- nPos = nPos + 1
- redraw()
- end
- elseif param == keys.up or param == keys.down then
- -- Up or down
- if _tHistory then
- redraw(" ");
- if param == keys.up then
- -- Up
- if nHistoryPos == nil then
- if #_tHistory > 0 then
- nHistoryPos = #_tHistory
- end
- elseif nHistoryPos > 1 then
- nHistoryPos = nHistoryPos - 1
- end
- else
- -- Down
- if nHistoryPos == #_tHistory then
- nHistoryPos = nil
- elseif nHistoryPos ~= nil then
- nHistoryPos = nHistoryPos + 1
- end
- end
- if nHistoryPos then
- sLine = _tHistory[nHistoryPos]
- nPos = string.len( sLine )
- else
- sLine = ""
- nPos = 0
- end
- redraw()
- end
- elseif param == keys.backspace then
- -- Backspace
- if nPos > 0 then
- redraw(" ");
- sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
- nPos = nPos - 1
- redraw()
- end
- elseif param == keys.home then
- -- Home
- nPos = 0
- redraw()
- elseif param == keys.delete then
- if nPos < string.len(sLine) then
- redraw(" ");
- sLine = string.sub( sLine, 1, nPos ) .. string.sub( sLine, nPos + 2 )
- redraw()
- end
- elseif param == keys["end"] then
- -- End
- nPos = string.len(sLine)
- redraw()
- elseif type(_fEventCallback) == "function" then
- local pos, line = _fEventCallback(sEvent, param, nPos, sLine, redraw, sx, sy)
- nPos = pos or nPos
- sLine = line or sLine
- redraw()
- end
- end
- end
- term.setCursorBlink( false )
- term.setCursorPos( w + 1, sy )
- print()
- return sLine
- end
- function setColor(c1, c2)
- if term.isColor() then
- if c1 then term.setTextColor(c1) end
- if c2 then term.setBackgroundColor(c2) end
- end
- end
- function printColored(...)
- writeColored(..., "\n")
- end
- function writeColored(...)
- local tmp = ""
- for k, v in ipairs({...}) do
- tmp = tmp..v
- end
- for match in tmp:gmatch("[^§]+") do
- local c = match:sub(1,1):lower()
- local c2, c3
- local text = match:sub(2)
- if c == "§" then
- write("§"..text)
- else
- local colors = "0123456789abcdef"
- for a = 1, colors:len() do
- if c == colors:sub(a,a) then
- c2 = 2^(a - 1)
- end
- end
- text = text:sub(2)
- c = match:sub(2,2):lower()
- for a = 1, colors:len() do
- if c == colors:sub(a,a) then
- c3 = 2^(a - 1)
- end
- end
- setColor(c2, c3)
- write(text)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement