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

[Lua][Question] Creating An Editor.

Started by Henness, 24 January 2013 - 12:20 PM
Henness #1
Posted 24 January 2013 - 01:20 PM
I would like to create a basic editor, were instead of using read() and getting the users input like that I would like to make it so the user can use the arrow keys to go back to previous lines. lets say I'm asking the user for cords for a turtle, and they enter x and y then while in the middle of entering z they realize that x in incorrect, whats the best way of making it so the user can press the up arrow to go back to x without loosing the input for y and the half way imputed z? Sorry I don't have any code I cant think of any way of doing this without parallel witch I am trying to avoid.
NeverCast #2
Posted 24 January 2013 - 01:43 PM
You would have to make your own read by hooking the key or char events.
Have a table of to be completed fields, ie

prompt = {

x = "Please enter x: ",
y = "Please enter y: ",
z = "Please enter z: ",
}

and perhaps have answers table with x,y,z.
Or you could use a table inside a table for the prompt.

And then have a display loop that displays the message for the prompt that has a nil value.
So when a user presses up it sets the previous prompt to nil and the display loop will display the other prompt and input will be redirected

That is how I'd do it anyway
Cranium #3
Posted 24 January 2013 - 01:43 PM
Well, take a look at the default 'edit' program built into CC. That will give you a good idea on where to start making an editor. You don't have to copy it directly, but it should get you started.
NeverCast #4
Posted 24 January 2013 - 01:45 PM
I had completely forgotten that edit was written in Lua

This guy is a stupid!
Cranium #5
Posted 24 January 2013 - 01:57 PM
This guy is a stupid!
Be nice. Self abuse is not funny…..ok, well sometimes it is.
NeverCast #6
Posted 24 January 2013 - 02:01 PM
Why else would there be hundreds of videos on youtube of people hurting themselves!
Henness #7
Posted 24 January 2013 - 08:00 PM
Is read() part of an api, and if so witch api is it part of?

nvm its part of computer craft, does anyone know how the read function is written?
Edited on 24 January 2013 - 07:03 PM
Cranium #8
Posted 24 January 2013 - 08:09 PM
The read function is basically a loop that takes events. When it receives a character event, it concatenates that character to the new return string. Backspace is harder, because you must remove the last character, and then move the character to a previous position, and type a space. I probably made the explanation ridiculously complicated, but I am too tired to write an example code snippet.
You can take a look at it from within the CC folder as well.
theoriginalbit #9
Posted 24 January 2013 - 08:10 PM
something similar to this


local function read( c )
  local input = ""
  local px, py = term.getCursorPos()
  local w = term.getSize()
  while true do
	local event, p1 = os.pullEvent()
	if event == "char" then
	  input = input..p1
	elseif event == "key" then
	  if p1 == keys.backspace then
		input = input:sub( 1, #input - 1 )
	  elseif p1 == keys.enter then
		return input
	  end
	end
	for i = 1, math.min( #input, w ) do
	  term.setCursorPos( px + i, py )
	  write( c or input:sub( i, i ) )
	end
  end
end

EDIT:

You can take a look at it from within the CC folder as well.
Where? o.O
Edited on 24 January 2013 - 07:12 PM
Cranium #10
Posted 25 January 2013 - 09:23 AM
The read function is listed in the APIs folder if I recall correctly. I can't really remember, so you should be able to find it in one of the files if you keep clicking on all of the programs :P/>
Henness #11
Posted 25 January 2013 - 02:24 PM
I found the read function in the bios file the code can be found here. While Looking at it I noticed a history parameter that does something with the up and down arrow keys, does any one know how to use that or what it does?

nvm I fairly sure that's the previous commands part of craft os.
Edited on 25 January 2013 - 02:01 PM
LBPHacker #12
Posted 25 January 2013 - 11:02 PM
History is just an easy way to repeat commands. Ie. you type "ping" in the Shell, then hit Enter. It runs, returns. Then you don't type it again, but you press Up, and "ping" is there again, and you just have to hit Enter to run it again. It's just a table passed in the 2nd argument.