diff --git a/examples/http/download.py b/examples/http/download.py index 29189c2..9af7569 100644 --- a/examples/http/download.py +++ b/examples/http/download.py @@ -44,6 +44,7 @@ import appier_console BIG_BUCK_URL = "http://download.blender.org/peach/bigbuckbunny_movies/big_buck_bunny_1080p_h264.mov" +BIG_BUCK_NAME = "big_buck_bunny_1080p_h264.mov" url = sys.argv[1] if len(sys.argv) > 1 else BIG_BUCK_URL name = os.path.basename(appier.legacy.urlparse(url).path) @@ -58,10 +59,11 @@ def copy(input, name, buffer_size = 16384): finally: output.close() -with appier_console.ctx_http_callbacks("big_buck_bunny_1080p_h264.mov") as callbacks: +with appier_console.ctx_http_callbacks(BIG_BUCK_NAME) as callbacks: contents, _response = appier.get( url, handle = True, + silent = True, redirect = True, retry = 0, use_file = True, diff --git a/src/appier_console/base.py b/src/appier_console/base.py index aceabe0..33db7f7 100644 --- a/src/appier_console/base.py +++ b/src/appier_console/base.py @@ -103,6 +103,7 @@ def __init__( color = None, template = "{{spinner}}", stream = sys.stdout, + end_newline = False, *args, **kwargs ): threading.Thread.__init__(self, *args, **kwargs) @@ -111,6 +112,7 @@ def __init__( self.color = color self.template = template self.stream = stream + self.end_newline = end_newline def run(self): threading.Thread.run(self) @@ -130,20 +132,34 @@ def run(self): index = 0 is_first = True - while self.running: + while True: value = index % len(frames) if is_first: is_first = False else: self.stream.write(CLEAR_LINE + "\r") replacer = frames[value] if color: replacer = color + replacer + COLOR_RESET + template = appier.legacy.str(self.template) label = template.replace("{{spinner}}", replacer) + + # writes the current label (text) to the output stream + # and runs the flush operation (required to ensure that + # the data contents are properly set in the stream) self.stream.write(label) self.stream.flush() + + # in case the running flag is not longer set breaks + # the current loop (nothing remaining to be done) + if not self.running: break + + # sleeps for the amount of time in the interval and + # then increments the current loop cycle index time.sleep(interval) index += 1 - self.stream.write(CLEAR_LINE + "\r") + if self.end_newline: self.stream.write("\n") + else: self.stream.write(CLEAR_LINE + "\r") + self.stream.flush() def stop(self): diff --git a/src/appier_console/http.py b/src/appier_console/http.py index 717a4d0..7f349a4 100644 --- a/src/appier_console/http.py +++ b/src/appier_console/http.py @@ -55,10 +55,11 @@ def ctx_http_callbacks( name, console_threshold = CONSOLE_THRESHOLD, - text_threshold = TEXT_THRESHOLD + text_threshold = TEXT_THRESHOLD, + end_newline = True ): - with base.ctx_loader() as loader: + with base.ctx_loader(end_newline = end_newline) as loader: status = dict( length = -1, @@ -70,10 +71,10 @@ def ctx_http_callbacks( ) def callback_init(connection): - loader.set_template("[%s] Establishing connection " % name) + loader.set_template("{{spinner}} [%s] Establishing connection " % name) def callback_open(connection): - loader.set_template("[%s] Connection established " % name) + loader.set_template("{{spinner}} [%s] Connection established " % name) def callback_headers(headers): _length = headers.get("content-length", None)