1

I have string that goes over multiple lines leading the terminal to scroll to the latest printed line. But I want to stay in the first line, so that I will be able to see the first lines but not the latest. Is that possible? e.g.:

for i in range(100):
    print(f"hello {i}")

and I want to see hello 0 but the output should stay in the same shape

einmalundweg
  • 83
  • 1
  • 6
  • 1
    I'm not sure that the marked answer is a duplicate. This user wants to prevent his terminal from scrolling to the bottom of his long output. I don't think that is possible. – Jonathon Reinhart Jan 30 '20 at 12:57
  • This is nothing the outputting program can manipulate so easily. Maybe you need to tweak the settings of your terminal. – glglgl Jan 30 '20 at 13:01
  • 1
    When the bottom line of your terminal is reached, should the program stop printing? You possibly can add a wrap-around to `print` that keeps count. – Jongware Jan 30 '20 at 13:26

2 Answers2

1

You can set defscrollback 100 if you're using GNU. Otherwise, this should help:

class More(object):
def __init__(self, num_lines):
    self.num_lines = num_lines
def __ror__(self, other):
    s = str(other).split("\n")
    for i in range(0, len(s), self.num_lines):
        print(*s[i: i + self.num_lines], sep="\n")
        input("Press <Enter> for more")
more = More(num_lines=30)  
"\n".join(map(str, range(100))) | more
0

I think this question actually is not about python but about the terminal emulator that you use. In most terminal emulators you can simply scroll up a line after you launched your application so that the output won't scroll down automatically. Just do the following:

  1. Open the terminal
  2. Start your python script
  3. Scroll up before the large output starts
  4. Hopefully your terminal stays in the position.

Alternatively, you can pipe the output of the programm to less, so that you can scroll page-wise:

ps x | less
MiH
  • 115
  • 2
  • 13