58 posts
Location
Mars
Posted 27 August 2012 - 09:06 PM
I am making a new operating system (which will be released soon as alpha :D/>/> ) which opens an input box when you want to rename or make a new file/directory. Here is the input box with an example of the new directory box:
|------------------------------------------------|
|New directory's name: |
| |
|------------------------------------------------|
The read() is on the third line. My problem is that because read() reads the whole line after it's called, it deletes the right |, even if you didn't type anything long enough to reach it. Is there any way I can make it not go past the |, like make it only go 48 spaces? Thanks.
504 posts
Location
Seattle, WA
Posted 27 August 2012 - 10:42 PM
I believe this is in the wrong section, but I help you anyways :D/>/>
I wrote a function a while ago that lets your read a limited amount of characters and can have a replacement character.
However, this function doesn't delete the whole line, rather it only erases the length of the currently entered string.
Here it is:
Spoiler
function limitRead( nLength, cReplaceChar )
term.setCursorBlink( true )
local nLength = nLength or -1 -- -1 is unlimited
local sReturnString = ""
local xPos, yPos = term.getCursorPos()
local while true do
local event, char = os.pullEvent()
if nLength ~= -1 and string.len( sReturnString ) >= nLength then term.setCursorBlink( false ); return sReturnString end -- Length check
if event == "char" then sReturnString = sReturnString .. char
elseif event == "key" and char == 28 then term.setCursorBlink( false ); return sReturnString -- Enter
elseif event == "key" and char == 14 then -- Backspace
term.setCursorPos( xPos, yPos )
term.write( string.rep( " ", string.len( sReturnString ) ) )
sReturnString = string.sub( sReturnString, 1, string.len( sReturnString )-1 )
term.setCursorPos( xPos, yPos )
if not cReplaceChar then term.write( sReturnString )
else term.write( string.rep( cReplaceChar, string.len( sReturnString ) ) ) end
end
term.setCursorPos( xPos, yPos )
term.write( string.rep( " ", string.len( sReturnString ) ) )
term.setCursorPos( xPos, yPos )
if not cReplaceChar then term.write( sReturnString )
else term.write( string.rep( cReplaceChar, string.len( sReturnString ) ) ) end
end
end
If you use this, please give me credit.
Hope I helped! :P/>/>
137 posts
Location
the Netherlands
Posted 27 August 2012 - 10:46 PM
This is the read-function from the rom:
Spoiler
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()
local nScroll = 0
if sx + nPos >= w then
nScroll = (sx + nPos) - w
end
term.setCursorPos( sx, sy )
term.write( string.rep(" ", w - sx + 1) )
term.setCursorPos( sx, sy )
if _sReplaceChar then
term.write( string.rep(_sReplaceChar, 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
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
sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
nPos = nPos - 1
redraw()
end
end
end
end
term.setCursorBlink( false )
term.setCursorPos( w + 1, sy )
print()
return sLine
end
You could try to add a modified edition of this in your program.
In the 'redraw' function, just add some lines that write your '|'
58 posts
Location
Mars
Posted 27 August 2012 - 10:54 PM
I do not understand how I do that…
504 posts
Location
Seattle, WA
Posted 27 August 2012 - 11:59 PM
Y U NO USE MY FUNCTION?
58 posts
Location
Mars
Posted 28 August 2012 - 01:35 AM
Haha, I'm sorry I didn't see that before! Thanks! Gonna test it later.
58 posts
Location
Mars
Posted 28 August 2012 - 09:48 PM
One problem, when it hits 48 characters, it gets bugged up while in the normal read(), it only scrolls so you can put more and scrolls back if you use backspace. I'll try to find a solution…