I would like to be able to wrap input from a user, instead of having it on one line and going on forever.
Like when you print something and it word wraps, I would like to be able to have them type data that would be word wrapped, and depending on the size of the terminal... Is there a way to do this using read() or something?
[Lua][Help]Text wrapping when typing.
Started by ChaddJackson12, Dec 05 2012 05:02 PM
utility lua help
3 replies to this topic
#1
Posted 05 December 2012 - 05:02 PM
#2
Posted 05 December 2012 - 05:20 PM
I don't know of a way to do this with read(), but as far as I know, print() word wraps the string given. Theoretically, you could use os.pullEvent() to create your own read().
Except that this only allows you to type and backspace, and it only reads from the topleft of the screen, but you can adapt it to fit your purposes, it's only an example.
local function wrapRead()
local input = ''
while true do
term.clear()
term.setCursorPos(1,1)
print(input)
local ev, p1 = os.pullEvent()
if ev == 'char' then
input = input..p1
elseif ev == 'key' then
if p1 == keys.backspace then
input = input:sub(1, #input-1)
elseif p1 == keys.enter then
break
end
end
end
return input
end
Except that this only allows you to type and backspace, and it only reads from the topleft of the screen, but you can adapt it to fit your purposes, it's only an example.
#3
Posted 05 December 2012 - 05:22 PM
Kingdaro, on 05 December 2012 - 05:20 PM, said:
I don't know of a way to do this with read(), but as far as I know, print() word wraps the string given. Theoretically, you could use os.pullEvent() to create your own read().
Except that this only allows you to type and backspace, and it only reads from the topleft of the screen, but you can adapt it to fit your purposes, it's only an example.
local function wrapRead() local input = '' while true do term.clear() term.setCursorPos(1,1) print(input) local ev, p1 = os.pullEvent() if ev == 'char' then input = input..p1 elseif ev == 'key' then if p1 == keys.backspace then input = input:sub(1, #input-1) elseif p1 == keys.enter then break end end end return input end
Except that this only allows you to type and backspace, and it only reads from the topleft of the screen, but you can adapt it to fit your purposes, it's only an example.
#4
Posted 05 December 2012 - 05:40 PM
ChaddJackson12, on 05 December 2012 - 05:22 PM, said:
Kingdaro, on 05 December 2012 - 05:20 PM, said:
I don't know of a way to do this with read(), but as far as I know, print() word wraps the string given. Theoretically, you could use os.pullEvent() to create your own read().
Except that this only allows you to type and backspace, and it only reads from the topleft of the screen, but you can adapt it to fit your purposes, it's only an example.
local function wrapRead() local input = '' while true do term.clear() term.setCursorPos(1,1) print(input) local ev, p1 = os.pullEvent() if ev == 'char' then input = input..p1 elseif ev == 'key' then if p1 == keys.backspace then input = input:sub(1, #input-1) elseif p1 == keys.enter then break end end end return input end
Except that this only allows you to type and backspace, and it only reads from the topleft of the screen, but you can adapt it to fit your purposes, it's only an example.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











