Jump to content




[Lua][Question] Creating An Editor.


  • You cannot reply to this topic
11 replies to this topic

#1 Henness

  • Members
  • 189 posts

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.

#2 NeverCast

  • Members
  • 400 posts
  • LocationChristchurch, New Zealand

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

#3 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

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.

#4 NeverCast

  • Members
  • 400 posts
  • LocationChristchurch, New Zealand

Posted 24 January 2013 - 01:45 PM

I had completely forgotten that edit was written in Lua

This guy is a stupid!

#5 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 24 January 2013 - 01:57 PM

View PostNeverCast, on 24 January 2013 - 01:45 PM, said:

This guy is a stupid!
Be nice. Self abuse is not funny.....ok, well sometimes it is.

#6 NeverCast

  • Members
  • 400 posts
  • LocationChristchurch, New Zealand

Posted 24 January 2013 - 02:01 PM

Why else would there be hundreds of videos on youtube of people hurting themselves!

#7 Henness

  • Members
  • 189 posts

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 by Henness, 24 January 2013 - 08:03 PM.


#8 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

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.

#9 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

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:

View PostCranium, on 24 January 2013 - 08:09 PM, said:

You can take a look at it from within the CC folder as well.
Where? o.O

Edited by TheOriginalBIT, 24 January 2013 - 08:12 PM.


#10 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

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

#11 Henness

  • Members
  • 189 posts

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 by Henness, 25 January 2013 - 03:01 PM.


#12 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

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.





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users