[Solved]Strange Console Output

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Dr.Tyler O.
Citizen
Posts: 58
Joined: Tue Jul 29, 2014 9:17 am
Location: United States

[Solved]Strange Console Output

Post 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.
Last edited by Dr.Tyler O. on Mon Jul 06, 2015 11:54 pm, edited 1 time in total.
Any topic I have ever posted that I felt did not require a .love file, although they were requested, never required one so I would appreciate if you did not ask me to supply one unless you think that I am ignorant for not doing so.
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Strange Console Output

Post 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.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Strange Console Output

Post by davisdude »

AFAIK, this is a known problem after using os.execute, I had the same problem a while back.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Strange Console Output

Post 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
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: [Solved]Strange Console Output

Post 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?
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: [Solved]Strange Console Output

Post 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
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: [Solved]Strange Console Output

Post 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.
User avatar
Linkpy
Party member
Posts: 102
Joined: Fri Aug 29, 2014 6:05 pm
Location: France
Contact:

Re: [Solved]Strange Console Output

Post 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:
Founder of NeoShadow Studio. Currently working on the project "Sirami".
github / linkpy
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: [Solved]Strange Console Output

Post 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?
User avatar
Linkpy
Party member
Posts: 102
Joined: Fri Aug 29, 2014 6:05 pm
Location: France
Contact:

Re: [Solved]Strange Console Output

Post by Linkpy »

You can use io.flush() or even disable the line buffering. And, this will works normally everywhere ! :awesome:
Founder of NeoShadow Studio. Currently working on the project "Sirami".
github / linkpy
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 5 guests