892 posts
Location
Where you'd least expect it.
Posted 18 December 2012 - 05:35 AM
Hello! I'm currently making a program that imitates CraftOS, and I was wondering if there would be any way to implement this part of the bios.lua:
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.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
to be part of my code at:
http://www.computerc...e-fake-console/I'm sorry if I'm just asking for code here, but a pro told me to refer here on this subject.
Thanks, Mudkip
8543 posts
Posted 18 December 2012 - 06:34 AM
That looks like part of the read() function. What do you need it to do differently, that using read() won't work?
892 posts
Location
Where you'd least expect it.
Posted 18 December 2012 - 06:38 AM
That looks like part of the read() function. What do you need it to do differently, that using read() won't work?
Wait, what? I just want that function to…. Nevermind, this might be impossible.
Is there any way to implement the history function into a program that just reads the input, not execute it?
Edit: Really, all I want is for the up and down arrow to do the same thing they do in shell, in my program. It would make MUCH more sense if you read my program's thread.
799 posts
Location
Land of Meh
Posted 18 December 2012 - 08:41 AM
The shell program (yes it is a program stored on your computer that is run on startup) can be found in rom/programs/shell.
The shell program uses the read function to read a line from the user, and then executes it.
It achieves the history feature by using the history feature of the read function.
Demonstration on how to use the read feature:
historyTable = {}
while true do
term.clear()
term.setCursorPos(1, 1)
readLine = read(nil, historyTable)
table.insert(historyTable, readLine)
end
This should allow you to use the up and down arrow keys to move between previously typed things.
Hope this helps :P/>
892 posts
Location
Where you'd least expect it.
Posted 18 December 2012 - 08:59 AM
Thank you so much! Updating program! :D/>
7508 posts
Location
Australia
Posted 18 December 2012 - 11:42 AM
That looks like part of the read() function. What do you need it to do differently, that using read() won't work?
read is terminate-able. maybe he doesn't want it terminate-able.
818 posts
Posted 18 December 2012 - 12:30 PM
if so, the originalBIT, pcall would be a much better way
7508 posts
Location
Australia
Posted 18 December 2012 - 12:31 PM
pcall….
yes you can use pcall. but he didnt say to use pcall with read, he said use read.
818 posts
Posted 18 December 2012 - 12:35 PM
pcall….
yes you can use pcall. but he didnt say to use pcall with read, he said use read.
Anyways, that's not the subject, since that wasn't the reason. Back to topic! ;)/>
892 posts
Location
Where you'd least expect it.
Posted 18 December 2012 - 01:21 PM
Thank you so much! Updating program! :D/>
Well it worked, so it is resolved now. :D/>
pcall….
yes you can use pcall. but he didnt say to use pcall with read, he said use read.
Anyways, that's not the subject, since that wasn't the reason. Back to topic! ;)/>
Don't worry, used os.pullEvent = os.pullEventRaw to secure it. ;)/>