Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --
- -- Modified Read
- -- By Gravity Score
- --
- local w, h = term.getSize()
- local modifiedRead = function(properties)
- -- Properties:
- -- - replaceCharacter
- -- - displayLength
- -- - maxLength
- -- - onEvent
- -- - startingText
- local text = ""
- local startX, startY = term.getCursorPos()
- local pos = 0
- if not properties then
- properties = {}
- end
- if properties.displayLength then
- properties.displayLength = math.min(properties.displayLength, w - 2)
- end
- if properties.startingText then
- text = properties.startingText
- pos = text:len()
- end
- local draw = function(replaceCharacter)
- local scroll = 0
- if properties.displayLength and pos > properties.displayLength then
- scroll = pos - properties.displayLength
- end
- local repl = replaceCharacter or properties.replaceCharacter
- term.setTextColor(theme.text)
- term.setCursorPos(startX, startY)
- if repl then
- term.write(string.rep(repl:sub(1, 1), text:len() - scroll))
- else
- term.write(text:sub(scroll + 1))
- end
- term.setCursorPos(startX + pos - scroll, startY)
- end
- term.setCursorBlink(true)
- draw()
- while true do
- local event, key, x, y, param4, param5 = os.pullEvent()
- if properties.onEvent then
- -- Actions:
- -- - exit (bool)
- -- - text
- term.setCursorBlink(false)
- local action = properties.onEvent(text, event, key, x, y, param4, param5)
- if action then
- if action.text then
- draw(" ")
- text = action.text
- pos = text:len()
- end if action.exit then
- break
- end
- end
- draw()
- end
- term.setCursorBlink(true)
- if event == "char" then
- local canType = true
- if properties.maxLength and text:len() >= properties.maxLength then
- canType = false
- end
- if canType then
- text = text:sub(1, pos) .. key .. text:sub(pos + 1, -1)
- pos = pos + 1
- draw()
- end
- elseif event == "key" then
- if key == keys.enter then
- break
- elseif key == keys.left and pos > 0 then
- pos = pos - 1
- draw()
- elseif key == keys.right and pos < text:len() then
- pos = pos + 1
- draw()
- elseif key == keys.backspace and pos > 0 then
- draw(" ")
- text = text:sub(1, pos - 1) .. text:sub(pos + 1, -1)
- pos = pos - 1
- draw()
- elseif key == keys.delete and pos < text:len() then
- draw(" ")
- text = text:sub(1, pos) .. text:sub(pos + 2, -1)
- draw()
- elseif key == keys.home then
- pos = 0
- draw()
- elseif key == keys["end"] then
- pos = text:len()
- draw()
- end
- elseif event == "mouse_click" then
- local scroll = 0
- if properties.displayLength and pos > properties.displayLength then
- scroll = pos - properties.displayLength
- end
- if y == startY and x >= startX and x <= math.min(startX + text:len(), startX + (properties.displayLength or 10000)) then
- pos = x - startX + scroll
- draw()
- elseif y == startY then
- if x < startX then
- pos = scroll
- draw()
- elseif x > math.min(startX + text:len(), startX + (properties.displayLength or 10000)) then
- pos = text:len()
- draw()
- end
- end
- end
- end
- term.setCursorBlink(false)
- print("")
- return text
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement