Why does my python blessings print statement not print in the same spot?

427 views Asked by At

I am using blessings (or blessed) to try to print in the same spot in a for-loop. However, I find that this only works if I print 3 above the bottom of the terminal, but if I print 1 or two lines above the bottom it prints a new line for each iteration...

For y=term.height-3:

from blessed import Terminal

term = Terminal()
print()
print()
for i in range(0,999999999):

    with term.location(y=term.height-2):

        print('Here is the bottom.')

This code prints this:

Here is the bottom.

It does that for every iteration.

BUT, if I change the print location to

with term.location(y=term.height-1):

It does this

Here is the bottom.
Here is the bottom.
Here is the bottom.
Here is the bottom.
Here is the bottom.
Here is the bottom.
Here is the bottom.
Here is the bottom.
Here is the bottom.
Here is the bottom.
Here is the bottom.
Here is the bottom.
Here is the bottom.

and so on and so on.. why is this?

1

There are 1 answers

2
IQbrod On

Height of terminal is exclusive: [0,N] => N+1
Let's take N = 3 for simplicity

0 >
1 >
2 >
3 >

term.height = 4
term.height - 1 = 3
results in

0 > hidden to keep height = 4 and because you wrote on line 4
1 > 
2 >
3 > (3,1)
4 > Bottom is here

Now let's do it again, we have this

1 > 
2 >
3 > (3,1)
4 > Bottom is here

In height results in

[1] 0 > 
[2] 1 >
[3] 2 > (3,1)
[4] 3 > Bottom is here

And you print at line 3

[1] 0 > hidden to keep height = 4 and because you wrote on line 4
[2] 1 >
[3] 2 > (3,1)
[4] 3 > (3,1)
[5] 4 > Bottom is here