by user2426679 · 7y ago · 0 comments
That is exactly what this library does: "tqdm does not require any dependencies (not even curses!), just Python and an environment supporting carriage return \r and line feed \n control characters." -- https://github.com/tqdm/tqdm
Seems unnecessary imo to depend on an external library just to wrap and auto-prettify those 2 stdout commands.
https://i.imgur.com/fvEeaJA.png
My library function is:
import sys def writeToTerminal(txt, line_len=79): # line_len is the max num of characters that can fit onto a terminal line without overflowing to a newline
# overwrite the line with blank spaces
# this takes care of the case where len(txt) is fewer than the [ len(txt) when this function was last called ]
sys.stdout.write( "\r" + " "*line_len )
sys.stdout.flush()
# write the txt, trimming to line_len
sys.stdout.write( "\r" + txt.replace("\t", " ")[:line_len] )
sys.stdout.flush()
Example usage:from time import sleep L = list(range(10)) for c, i in enumerate(L, start=1): writeToTerminal("%i / %i" %(c, len(L))) sleep(1)
It doesn't.
You can indent two spaces to get monospaced, unwrapped text, and that’s all the code formatting you get.
the visual display of this progress indicator is different yes -
This method doesn't need any notion of terminal width because it just prints 'N / N' at the beginning of the current line where the cursor is when called.