This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Owlupus's profile picture

[Lua] Multiple Line Input

Started by Owlupus, 06 November 2012 - 05:30 AM
Owlupus #1
Posted 06 November 2012 - 06:30 AM
I am trying to make a program that prints things - very basic. When the program asks for input of what the player wants to be printed, you can only type one line. Pressing enter submits the text. When the page is printed, the document simply has text on the first line that goes off the page. I want to be able to type on multiple lines.
Lyqyd #2
Posted 06 November 2012 - 06:41 AM
Are you looking for something like the edit program or just more of a multi-line read()? Either way, check out whichever one is more like the functionality you're looking for and copy and modify as appropriate.
YoYoYonnY #3
Posted 14 November 2012 - 03:30 AM
here is the code you are using whit "read()":
if you are deleting 37 till 41 then you wont exit when using enter(you can also replace keys.enter whit another key, or replace break for something else, like your function)





function read( _sReplaceChar, _tHistory )
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 redraw( _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

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()
end
end
end

term.setCursorBlink( false )
term.setCursorPos( w + 1, sy )
print()

return sLine
end
remiX #4
Posted 14 November 2012 - 06:02 AM
Yeah or you can use the edit program and manipulate a 'letter' program from it :P/>/>
Sammich Lord #5
Posted 14 November 2012 - 06:21 AM
Yeah or you can use the edit program and manipulate a 'letter' program from it :P/>/>
I have seen a ton of scripts do that. They would just shell.run the script with the program to edit then once you save it, it will go back to the program you ran it from.