diff --git a/lib/svtplay/output.py b/lib/svtplay/output.py index 527f1b1..87adfe9 100644 --- a/lib/svtplay/output.py +++ b/lib/svtplay/output.py @@ -1,7 +1,30 @@ import sys +import os from svtplay.log import log +progress_stream = sys.stderr + +def progress(byte, total, extra = ""): + """ Print some info about how much we have downloaded """ + ratio = float(byte) / total + percent = round(ratio*100, 2) + tlen = str(len(str(total))) + fmt = "Downloaded %"+tlen+"dkB of %dkB bytes (% 3.2f%%)" + progresstr = fmt % (byte >> 10, total >> 10, percent) + + columns = int(os.getenv("COLUMNS", "80")) + if len(progresstr) < columns - 13: + p = int((columns - len(progresstr) - 3) * ratio) + q = int((columns - len(progresstr) - 3) * (1 - ratio)) + progresstr = "[" + ("#" * p) + (" " * q) + "] " + progresstr + progress_stream.write(progresstr + ' ' + extra + '\r') + + if byte >= total: + progress_stream.write('\n') + + progress_stream.flush() + def progressbar(total, pos, msg=""): """ Given a total and a progress position, output a progress bar