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

Can someone help me out?

Started by Matthew, 05 April 2013 - 08:16 AM
Matthew #1
Posted 05 April 2013 - 10:16 AM
I'm new to ComputerCraft so bare with me,

1. How can I center input = read()? Because my text is all centered except for when the user inputs data. Or is there another way to prompt the user for an answer (simple yes/no with an if statement)
2. Is there a simpler way to insert a new line without doing print(" ")?

Thank you ^_^/>
Telokis #2
Posted 05 April 2013 - 10:20 AM
Hey !
I don't know if it is what you mean but you should take a look at term.setCursorPos(x, y) !
OmegaVest #3
Posted 05 April 2013 - 10:33 AM
Actually, the easiest(or most useful) way to center input is to make your own read function that uses chars or keys and an pullEvent loop. Everything else is either imperfect or destroys whatever GUI you happen to have.

And, for new lines, yeah, setCursorPos is the only alternative. For whatever reason escape characters don't always work with term.write. Or any write, for that matter.
Kingdaro #4
Posted 05 April 2013 - 10:33 AM
1. For that, you would need to make your own read() function. This is an edit of the vanilla read() that centers text, but also clears the entire current line (because it has to.)

Spoiler

function centerRead( _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 )
		]]
		
		local sReplace = _sCustomReplaceChar or _sReplaceChar
		
		term.clearLine()
		term.setCursorPos(w/2 - #sLine/2, sy)
		term.write(sReplace and string.rep(sReplace, #sLine) or sLine)
	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

2. It would probably help to know that \n represents the newline character, and therefore, doing write('\n\n\n') would make three new lines, and move the cursor down three times.

Note that this only works with print and write and not term.write, and when using this with print, it will print one more newline than you have provided, since print automatically makes a newline.

There's also the method of simply using term.setCursorPos() to print where you want your text.
Matthew #5
Posted 05 April 2013 - 10:39 AM
Thank you :)/>