Page 1 of 1

[Solved]Strange Console Output

Posted: Sun Jul 05, 2015 7:11 pm
by Dr.Tyler O.
When I execute this section of code the console returns strange characters.

Code: Select all

function love.load()
	for i = 1, 100 do
		os.execute("cls")
		print(i)
	end
end
Image
The purpose of this is for the sake of updating the console to show a percentage without flooding the console with a wall of numbers, I've just simplified it for ease of understanding.

Re: Strange Console Output

Posted: Sun Jul 05, 2015 7:28 pm
by airstruck
Tried using ANSI escape sequences?

Code: Select all

print(string.char(27) .. "[2F")
print(i)
That works fine in bash, just moves cursor up a line and to the beginning of column. Not sure what level of ANSI support cmd.exe has, but you should be able to find a cross-platform solution.

Code: Select all

function sleep(n)  -- seconds
  local t0 = os.clock()
  while os.clock() - t0 <= n do end
end

function love.load()
   print()
   for i = 1, 100 do
      print(string.char(27) .. "[2F")
      print(i)
      sleep(1)
   end
end
Takes 100 seconds to complete, make sure you have a way to kill the process if you don't want to wait.

Re: Strange Console Output

Posted: Sun Jul 05, 2015 8:26 pm
by davisdude
AFAIK, this is a known problem after using os.execute, I had the same problem a while back.

Re: Strange Console Output

Posted: Mon Jul 06, 2015 10:21 pm
by Positive07
In this case you should write to [manual]io.stdout[/manual] instead of using print, you can use it as a file and move to an specific position, delete, rewrite, insert, etc

Re: [Solved]Strange Console Output

Posted: Tue Jul 07, 2015 9:33 am
by airstruck
Positive07 wrote:In this case you should write to [manual]io.stdout[/manual] instead of using print, you can use it as a file and move to an specific position, delete, rewrite, insert, etc
You can? I tried this:

Code: Select all

      io.stdout:write(i)
      io.stdout:seek('cur', -1)
But nothing happened. I've never heard of being able to "move to an specific position, delete, rewrite, insert, etc" with stdout (I've only ever seen it done with escape sequences, never seen stdout treated as a random access file, never believed it was possible). Can you give an example of how you did this?

OP: Can you share how this was resolved?

Re: [Solved]Strange Console Output

Posted: Wed Jul 08, 2015 3:24 am
by Positive07
time thief wrote:-snip-
Oops! You seem to be right, I totally thought you could do that... I think I was wrong :death:

I'm REALLY sorry, my mistake entirely

Re: [Solved]Strange Console Output

Posted: Wed Jul 08, 2015 3:29 pm
by airstruck
Don't be sorry, I was just confused because OP marked this as [solved] right after you posted that. It would be good if people would take the time to leave a note about how their problem was solved to avoid confusing people who find the thread later. Could be that OP settled for printing everything on one line, so not having line breaks was a good enough soultion... who knows.

I assume stdout is an append-only stream on all platforms, though. If it were read-write it could be a security issue, one program could read the output from another.

Re: [Solved]Strange Console Output

Posted: Tue Jul 14, 2015 8:38 pm
by Linkpy
The purpose of this is for the sake of updating the console to show a percentage without flooding the console with a wall of numbers, I've just simplified it for ease of understanding.
Oh, I will ask why you use os.execute xD
There something simpleeeeeeeer for doing this :

Code: Select all

io.write (i .. "%\r")
Short :
The \r do everything !

Long :
The \r is an ANSI code named "Carriage Return". What it does basicly ? It "returns" the "carriage". So, it sets the X position of the cursor to 0 (or 1 if you prefer. To the start of the line). If you want to know, on Windows, when you want to print to a new line, you add a CRLF character combination. CR = Carriage Return = \r : LF = Line Feed = \n. The role of the Line Feed is to add one to the Y position of the cursor ! :emo:

Now, you know all you need. Create the new newgen 3D infinite-world sandbox role-playing-game ! :awesome:

Re: [Solved]Strange Console Output

Posted: Tue Jul 14, 2015 8:56 pm
by airstruck
Linkpy wrote:\r
Ahh, that's clever, I like it. Had to add io.flush() after it to get it to show up. Does this work on Mac, or do they still use \r as a line break?

Re: [Solved]Strange Console Output

Posted: Wed Jul 15, 2015 6:12 am
by Linkpy
You can use io.flush() or even disable the line buffering. And, this will works normally everywhere ! :awesome: