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

Text Editor Not Working

Started by PixelFox, 16 April 2015 - 08:32 PM
PixelFox #1
Posted 16 April 2015 - 10:32 PM
Sorry for asking so much. I just am having a problem, simply, I can't create a new line. Also, how would I make it scroll?
The Code:


  -- Variables --
local w, h = term.getSize()
local Name = "Untitled"
local text
local textString = {}
local textNum = 0
local Line = 2

  -- Functions --




  -- Main Code --
for i=1, w do
  paintutils.drawPixel(i, 1, colors.white)
  paintutils.drawPixel(i, h, colors.white)
end

for i=1, h do
  paintutils.drawPixel(1, i, colors.white)
  paintutils.drawPixel(w, i, colors.white)
end

term.setCursorPos(math.floor(w-string.len(Name)) / 2, 1)
term.setTextColor(colors.blue)
term.write(Name)
term.setCursorPos(2, 1)
term.write("File")
term.setCursorPos(2, h)
term.write("Current Line: "..Line-2)
term.setCursorPos(2, 2)
term.setBackgroundColor(colors.black)
text = read()
while true do
  local event, button, xPos, yPos = os.pullEvent()
  if event == "key" and button == keys.enter then
	textString[Line - 2] = text
	Line = Line + 1
	term.setBackgroundColor(colors.white)
	term.setTextColor(colors.blue)
	term.setCursorPos(2, w)
	term.write("Current Line: "..Line-2)
	term.setBackgroundColor(colors.black)
	term.setCursorPos(Line, 2)
	text = read()
  elseif event == "mouse_click" then

  end

end
It crashes when I press enter, I don't get an error, but the cursor goes away, and also, how would I scroll?
Edited on 17 April 2015 - 12:27 AM
flaghacker #2
Posted 16 April 2015 - 11:51 PM
Be sure to post the FULL, EXACT error message next time, that will make helping easier.

1) The parentheses after keys.enter shouldn't be there, it should currently crash when pressing any key.

2) There is a "mouse_scroll" event, see this sitev
http://computercraft.info/wiki/Os.pullEvent
KingofGamesYami #3
Posted 17 April 2015 - 12:26 AM
You're also missing "then" on that line.
PixelFox #4
Posted 17 April 2015 - 01:32 AM
Be sure to post the FULL, EXACT error message next time, that will make helping easier.

1) The parentheses after keys.enter shouldn't be there, it should currently crash when pressing any key.

2) There is a "mouse_scroll" event, see this sitev
http://computercraft...ki/Os.pullEvent
Thanks.
Why would I need mouse_scroll? It's useless for me.

You're also missing "then" on that line.
Okay.


But none of this is helping me, my cursor dissapears when I press "Enter."
Edited on 16 April 2015 - 11:49 PM
Dog #5
Posted 17 April 2015 - 02:18 AM
I'm surprised the code runs. I put it in my editor, indented it, and found it's missing an end at the end.

while w, h = term.getSize()
  local Name = "Untitled"
  local text
  local textString = {}
  local textNum = 0
  local Line  = 2
  for i=1, w do
    paintutils.drawPixel(i, 1, colors.white)
    paintutils.drawPixel(i, 19, colors.white)
  end
  for i=1, h do
    paintutils.drawPixel(1, i, colors.white)
    paintutils.drawPixel(51, i, colors.white)
  end
  term.setCursorPos(math.floor(w-string.len(Name)) / 2, 1)
  term.setTextColor(colors.blue)
  term.write(Name)
  term.setCursorPos(2, 1)
  term.write("File")
  term.setCursorPos(2, 2)
  term.setBackgroundColor(colors.black)
  text = read()
  while true do
    local event, button, xPos, yPos = os.pullEvent()
    if event == "key" and button == keys.enter then
      textString[Line] = text
      Line = Line + 1
      term.setCursorPos(Line, 2)
      text = read()
    elseif e == "mouse_click" then
    end
  end
--# where is the final end?

Notice the missing end on the last line?

A couple of other things:

1. You have 'elseif e == "mouse_click" then' near the end - e should be changed to event (elseif event == "mouse_click" then) - that section will not trigger until you make that change (when you get around to adding code to that portion)
2. I'm guessing you're cursor disappears because after you respond to the enter key you call a read() (although I'm not sure why the cursor doesn't blink)
3. You asked how you would scroll - first you'd need to capture input from the user indicating that they want to scroll - the "mouse_scroll" event (as suggested by flaghacker) is one way to capture that input

Add that missing 'end' to the end of the program and see what happens.
Edited on 17 April 2015 - 12:19 AM
PixelFox #6
Posted 17 April 2015 - 02:28 AM
I'm surprised the code runs. I put it in my editor, indented it, and found it's missing an end at the end.

while w, h = term.getSize()
  local Name = "Untitled"
  local text
  local textString = {}
  local textNum = 0
  local Line  = 2
  for i=1, w do
	paintutils.drawPixel(i, 1, colors.white)
	paintutils.drawPixel(i, 19, colors.white)
  end
  for i=1, h do
	paintutils.drawPixel(1, i, colors.white)
	paintutils.drawPixel(51, i, colors.white)
  end
  term.setCursorPos(math.floor(w-string.len(Name)) / 2, 1)
  term.setTextColor(colors.blue)
  term.write(Name)
  term.setCursorPos(2, 1)
  term.write("File")
  term.setCursorPos(2, 2)
  term.setBackgroundColor(colors.black)
  text = read()
  while true do
	local event, button, xPos, yPos = os.pullEvent()
	if event == "key" and button == keys.enter then
	  textString[Line] = text
	  Line = Line + 1
	  term.setCursorPos(Line, 2)
	  text = read()
	elseif e == "mouse_click" then
	end
  end
--# where is the final end?

Notice the missing end on the last line?

A couple of other things:

1. You have 'elseif e == "mouse_click" then' near the end - e should be changed to event (elseif event == "mouse_click" then) - that section will not trigger until you make that change (when you get around to adding code to that portion)
2. I'm guessing you're cursor disappears because after you respond to the enter key you call a read() (although I'm not sure why the cursor doesn't blink)
3. You asked how you would scroll - first you'd need to capture input from the user indicating that they want to scroll - the "mouse_scroll" event (as suggested by flaghacker) is one way to capture that input

Add that missing 'end' to the end of the program and see what happens.
Lol, there is no "while w, h = term.getSize()", it's "local w, h = term.getSize"
There is no missing end!


-- Variables --
local w, h = term.getSize()
local Name = "Untitled"
local text
local textString = {}
local textNum = 0
local Line = 2
  -- Functions --

  -- Main Code --
for i=1, w do
  paintutils.drawPixel(i, 1, colors.white)
  paintutils.drawPixel(i, h, colors.white)
end
for i=1, h do
  paintutils.drawPixel(1, i, colors.white)
  paintutils.drawPixel(w, i, colors.white)
end
term.setCursorPos(math.floor(w-string.len(Name)) / 2, 1)
term.setTextColor(colors.blue)
term.write(Name)
term.setCursorPos(2, 1)
term.write("File")
term.setCursorPos(2, h)
term.write("Current Line: "..Line-2)
term.setCursorPos(2, 2)
term.setBackgroundColor(colors.black)
text = read()
while true do
  local event, button, xPos, yPos = os.pullEvent()
  if event == "key" and button == keys.enter then
	    textString[Line - 2] = text
	    Line = Line + 1
	    term.setBackgroundColor(colors.white)
	    term.setTextColor(colors.blue)
	    term.setCursorPos(2, w)
	    term.write("Current Line: "..Line-2)
	    term.setBackgroundColor(colors.black)
	    term.setCursorPos(Line, 2)
	    text = read()
  elseif event == "mouse_click" then
  end
end
This is the true code!
Dog #7
Posted 17 April 2015 - 02:36 AM
HAHAHA - where did I get that from? I thought that was rather odd when I looked at it, but figured it was your program, so I'd let you do things your way. My apologies Lightning, this is not my day apparently.

I see you updated your posted code. You'll still want to fix #1 if you plan on using that part of your code and eventually take a look at #3 for scrolling. Also, with your current approach, eventually the value of Line will be higher than the value of h and your input routine will place the cursor off screen.

FWIW, you shouldn't keep editing the code in the OP, you should post your new code with your reply: If other people come looking for help with a similar problem it's easier to follow the code as it changes, as opposed to just seeing the fixed code and a bunch of discussion about code that isn't posted any longer. Does that make sense?

EDIT: I see you edited your post and fixed #1 - excellent :)/> Are you still having the same problem?
Edited on 17 April 2015 - 12:38 AM
PixelFox #8
Posted 17 April 2015 - 02:40 AM
HAHAHA - where did I get that from? I thought that was rather odd when I looked at it, but figured it was your program, so I'd let you do things your way. My apologies Lightning, this is not my day apparently.

I see you updated your posted code. You'll still want to fix #1 if you plan on using that part of your code and eventually take a look at #3 for scrolling. Also, with your current approach, eventually the value of Line will be higher than the value of h and your input routine will place the cursor off screen.

FWIW, you shouldn't keep editing the code in the OP, you should post your new code with your reply: If other people come looking for help with a similar problem it's easier to follow the code as it changes, as opposed to just seeing the fixed code and a bunch of discussion about code that isn't posted any longer. Does that make sense?

EDIT: I see you edited your post and fixed #1 - excellent :)/> Are you still having the same problem?
Yes, I'm still having the problem. When I press enter, apparently, the code hangs on "if event=="key" and button == keys.enter then", and I can fix the scrolling off the screen, but I need to fix this problem first. My cursor dissapears when I press enter, and I have no choice then to Ctrl+t, and the Line variable does NOT increase!
Edited on 17 April 2015 - 12:43 AM
Dog #9
Posted 17 April 2015 - 02:45 AM
According to your code,the Line variable most certainly *does* increase.

In the same while loop you have
Line = Line + 1
then you set the cursor position based on Line
term.setCursorPos(Line,2)

Give me some time, I'm testing your code right now…

EDIT:

OK, I tested your code and it works as expected. You press enter, then you type something and press enter again. Then the program waits for you to press enter before it'll let you type again.

And to correct myself from before, your cursor pos for reading will eventually be greater than w (not h) and will be off the right of the screen.
Edited on 17 April 2015 - 12:49 AM
PixelFox #10
Posted 17 April 2015 - 12:02 PM
According to your code,the Line variable most certainly *does* increase.

In the same while loop you have
Line = Line + 1
then you set the cursor position based on Line
term.setCursorPos(Line,2)

Give me some time, I'm testing your code right now…

EDIT:

OK, I tested your code and it works as expected. You press enter, then you type something and press enter again. Then the program waits for you to press enter before it'll let you type again.

And to correct myself from before, your cursor pos for reading will eventually be greater than w (not h) and will be off the right of the screen.
Thanks, but, how do I make it so I only have to press enter once?
Bomb Bloke #11
Posted 17 April 2015 - 01:01 PM
By not using the read() function. Instead, pull "char" events, and deal with the incoming characters yourself.
PixelFox #12
Posted 17 April 2015 - 08:33 PM
By not using the read() function. Instead, pull "char" events, and deal with the incoming characters yourself.
UH, What? I've got "elseif event == "char" then" but, after that, i'm completely dumbfounded.

NEVERMIND, I know how to, thanks though.
Edited on 17 April 2015 - 06:35 PM
Dog #13
Posted 17 April 2015 - 09:02 PM
Well, I can give you a super basic outline of what to do, but ultimately you'll need to do something a fair bit more complex for a full-blown text editor. The following will allow you to enter text character by character and press enter to start a new line. This doesn't do any word wrapping, allow for arrow keys, backspace, etc., but it should give you a very basic idea of some of the things you'll need to do…

term.clear()
local xCurPos = 1   --# starting x cursor position
local yCurPos = 1   --# starting y cursor position
local thisLine = "" --# variable we'll use to store a line of text as it's entered.
local lines = { }   --# table of 'lines of text'
term.setCursorBlink(true) --# so we can see the cursor
while true do
  term.setCursorPos(xCurPos, yCurPos) --# set the cursor position so the cursor is in the right place
  local event, data = os.pullEvent()
  if event == "key" and data == keys.enter then --# if the user pressed 'enter' then...
    lines[#lines + 1] = thisLine --# ...add the current line to the table of lines
    thisLine = ""                --# clear the thisLine variable (so it's ready for the next line)
    xCurPos = 1                  --# set our x cursor position to the beginning of the line
    yCurPos = yCurPos + 1        --# increment our y cursor position
  elseif event == "char" then    --# if the user types a character then...
    term.write(data)             --# ...write the character to screen
    xCurPos = xCurPos + 1        --# increment the x cursor position
    thisLine = thisLine .. data  --# concatenate thisLine with our new character
  end
end

EDIT: You can also take a look at the built in read() function in CC to get some ideas and/or take a look at theoriginalbit's custom read function. Both of those examples include support for arrows, backspace, delete, etc.
Edited on 17 April 2015 - 07:09 PM
PixelFox #14
Posted 19 April 2015 - 06:16 PM
Well, I can give you a super basic outline of what to do, but ultimately you'll need to do something a fair bit more complex for a full-blown text editor. The following will allow you to enter text character by character and press enter to start a new line. This doesn't do any word wrapping, allow for arrow keys, backspace, etc., but it should give you a very basic idea of some of the things you'll need to do…

term.clear()
local xCurPos = 1   --# starting x cursor position
local yCurPos = 1   --# starting y cursor position
local thisLine = "" --# variable we'll use to store a line of text as it's entered.
local lines = { }   --# table of 'lines of text'
term.setCursorBlink(true) --# so we can see the cursor
while true do
  term.setCursorPos(xCurPos, yCurPos) --# set the cursor position so the cursor is in the right place
  local event, data = os.pullEvent()
  if event == "key" and data == keys.enter then --# if the user pressed 'enter' then...
	lines[#lines + 1] = thisLine --# ...add the current line to the table of lines
	thisLine = ""				--# clear the thisLine variable (so it's ready for the next line)
	xCurPos = 1				  --# set our x cursor position to the beginning of the line
	yCurPos = yCurPos + 1		--# increment our y cursor position
  elseif event == "char" then	--# if the user types a character then...
	term.write(data)			 --# ...write the character to screen
	xCurPos = xCurPos + 1		--# increment the x cursor position
	thisLine = thisLine .. data  --# concatenate thisLine with our new character
  end
end

EDIT: You can also take a look at the built in read() function in CC to get some ideas and/or take a look at theoriginalbit's custom read function. Both of those examples include support for arrows, backspace, delete, etc.
I know. I already made it.