-1

i have a thread that calculates the time every 1 second to get the total elapsed time. This works great but i need to make it stay on the same line even after printing another line. Currently it is doing this:

[~] 00:00:02
ok #this is a test print to see if it stays on the same line and of course it dosen't.
[~] 00:00:08
def total_elapsed_time(start):
    while True:
        elapsed_time = time.time() - start
        print(colorama.Fore.LIGHTBLUE_EX + "[~] " + time.strftime("%H:%M:%S", time.gmtime(elapsed_time)), end="\r")
        time.sleep(1) #updates every second

this is my code. Ive been reading about \r \n and \r\n and \r does the job partially but i still need it to stay on the same line always. I'm not sure if this is even possible although i have seen it done in other languages so i think it is.

coke
  • 7
  • 4

1 Answers1

0

You can use \r to rewrite the bottom line.

If you want to print something else (like "ok") "above" the timestamp:

  • erase the bottom line with the timestamp first (you may have to overwrite it with spaces and then use \r again if the next thing isn't long enough), then
  • print the other thing (the "ok"), then
  • write the timestamp again below it on a new line.

For example,

>>> from datetime import datetime
>>> import time
>>> for c in 'abcdefg':
...   print(datetime.now(), end='\r')  # timestamp
...   time.sleep(1)
...   print(' '*80, end='\r')  # overwrite timestamp line with spaces
...   print(c)  # print something else "above" the next timestamp
...
a
b
c
d
e
f
g
>>>

The end result has no timestamp, but you can see it while it's looping. Try it.

If you don't start out scrolled to the bottom, the timestamp line "moves" but once you print enough lines to fill the terminal, the bottom line will stay in place at the bottom. You can also use this to display progress bars and spinners and things like that.

Note also that you can use the backspace character to back up one character instead of the carriage return to back up to the beginning of the line.

gilch
  • 10,813
  • 1
  • 23
  • 28
  • can you provide a piece of code... ngl that was very confusing. – coke May 31 '20 at 05:07
  • thats not what i needed, it seems like it does what you said but i need it to keep the timestamp on top and not the letters, not sure why but after re arranging the statements i still couldnt get it to keep the timestamp on top – coke May 31 '20 at 06:13
  • `\r` only works on the last line, sorry. Your question only said it had to be on the "same line". If you want to edit arbitrary locations in the console, you really do need something like curses. – gilch May 31 '20 at 06:15
  • There are special "escape sequences" to move the cursor around the terminal, similar to the color changing sequences that colorama produces. But how it works depends on the terminal type, unlike the `\r` trick that works on most terminals. To do it right you'd pretty much have to re-implement curses, so you might as well just use it. – gilch May 31 '20 at 06:26