0

I am working on a text based operating system in Python and I am wondering if it would be possible to update an already printed string on the same line. There doesn't seem to be anything on this other than this which doesn't really answer my question.

What I was thinking was something like:

Fri 13 May 6:00:34 PM

Then updating the same text with:

Fri 13 May 6:00:35 PM

This is a portion of my current code:

def desktop():
    now = datetime.today()
    print(f"{now:%c}")
    help = input(bcolors.OKCYAN + '''
    /help for a list of commands
    ''')
    if help == '/help':
        #This goes to a function that shows a list of possible commands
        get_help()
    else:
        pass

Is this even possible? If so how?

Many Thanks,

mrt

mrt
  • 65
  • 2
  • 11

3 Answers3

2

You might be able to do this in a limited way by writing out '\b' to erase and rewrite.

import sys

def foo()
    sys.stdout.write('foo')
    sys.stdout.flush()
    sys.stdout.write('\b\b\b')
    sys.stdout.write('bar')
    sys.stdout.flush()

I'm doing a similar thing on the command line, on a Mac. And this has worked so far, as long as I haven't already written out a newline. (Hence sys.stdout.write and not print.)

Paul
  • 3,009
  • 16
  • 33
0

The short answer, no. It's going to be extremely difficult, if not impossible, to do this using print functions for anything other than the last printed line, which seems to be the case here.

You could, however, look into TUI (Textual User Interfaces), such as https://github.com/Textualize/textual or https://github.com/bczsalba/pytermgui .

Dominik Stańczak
  • 2,046
  • 15
  • 27
0

You can have a look at this. Basically, you overwrite the previous line. I am not sure if this is what you are looking for.

chomprrr
  • 406
  • 4
  • 15