From 363c259713a635317ad2ade376fd1386c9b471ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Justen=20=28=40turicas=29?= Date: Tue, 10 Oct 2017 21:07:16 -0300 Subject: [PATCH 1/2] Replace \x08 with \b --- lab-spinner/spinner_await.py | 4 ++-- lab-spinner/spinner_curio.py | 4 ++-- lab-spinner/spinner_thread.py | 4 ++-- lab-spinner/spinner_yield.py | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lab-spinner/spinner_await.py b/lab-spinner/spinner_await.py index f4d481e..ee23e17 100644 --- a/lab-spinner/spinner_await.py +++ b/lab-spinner/spinner_await.py @@ -17,12 +17,12 @@ async def spin(msg): # <1> status = char + ' ' + msg write(status) flush() - write('\x08' * len(status)) + write('\b' * len(status)) try: await asyncio.sleep(.1) # <2> except asyncio.CancelledError: # <3> break - write(' ' * len(status) + '\x08' * len(status)) + write(' ' * len(status) + '\b' * len(status)) async def slow_function(): # <4> diff --git a/lab-spinner/spinner_curio.py b/lab-spinner/spinner_curio.py index 0af06d8..7e39c58 100755 --- a/lab-spinner/spinner_curio.py +++ b/lab-spinner/spinner_curio.py @@ -20,12 +20,12 @@ async def spin(msg): # <1> status = char + ' ' + msg write(status) flush() - write('\x08' * len(status)) + write('\b' * len(status)) try: await curio.sleep(.1) # <2> except curio.CancelledError: # <3> break - write(' ' * len(status) + '\x08' * len(status)) + write(' ' * len(status) + '\b' * len(status)) async def slow_function(): # <4> diff --git a/lab-spinner/spinner_thread.py b/lab-spinner/spinner_thread.py index 9935bc5..05b7b1a 100755 --- a/lab-spinner/spinner_thread.py +++ b/lab-spinner/spinner_thread.py @@ -18,10 +18,10 @@ def spin(msg, done): # <1> status = char + ' ' + msg write(status) flush() - write('\x08' * len(status)) + write('\b' * len(status)) if done.wait(.1): # <2> break # <3> - write(' ' * len(status) + '\x08' * len(status)) + write(' ' * len(status) + '\b' * len(status)) def slow_function(): # <4> diff --git a/lab-spinner/spinner_yield.py b/lab-spinner/spinner_yield.py index c6f94fc..df53df5 100755 --- a/lab-spinner/spinner_yield.py +++ b/lab-spinner/spinner_yield.py @@ -18,12 +18,12 @@ def spin(msg): status = char + ' ' + msg write(status) flush() - write('\x08' * len(status)) + write('\b' * len(status)) try: yield from asyncio.sleep(.1) # <2> except asyncio.CancelledError: # <3> break - write(' ' * len(status) + '\x08' * len(status)) + write(' ' * len(status) + '\b' * len(status)) @asyncio.coroutine # <4> From 42b1ac344ee62bcf2d198d7c62a5e1fcb3f74d3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Justen=20=28=40turicas=29?= Date: Tue, 10 Oct 2017 21:34:08 -0300 Subject: [PATCH 2/2] Use \r + print(flush=True) instead of stdout.flush --- lab-spinner/spinner_await.py | 8 ++------ lab-spinner/spinner_curio.py | 8 ++------ lab-spinner/spinner_thread.py | 8 ++------ lab-spinner/spinner_yield.py | 8 ++------ 4 files changed, 8 insertions(+), 24 deletions(-) diff --git a/lab-spinner/spinner_await.py b/lab-spinner/spinner_await.py index ee23e17..49a43ea 100644 --- a/lab-spinner/spinner_await.py +++ b/lab-spinner/spinner_await.py @@ -8,21 +8,17 @@ import asyncio import itertools -import sys async def spin(msg): # <1> - write, flush = sys.stdout.write, sys.stdout.flush for char in itertools.cycle('|/—\\'): status = char + ' ' + msg - write(status) - flush() - write('\b' * len(status)) + print('\r' + status, end='', flush=True) try: await asyncio.sleep(.1) # <2> except asyncio.CancelledError: # <3> break - write(' ' * len(status) + '\b' * len(status)) + print('\r', end='', flush=True) async def slow_function(): # <4> diff --git a/lab-spinner/spinner_curio.py b/lab-spinner/spinner_curio.py index 7e39c58..f1b4f65 100755 --- a/lab-spinner/spinner_curio.py +++ b/lab-spinner/spinner_curio.py @@ -11,21 +11,17 @@ import curio import itertools -import sys async def spin(msg): # <1> - write, flush = sys.stdout.write, sys.stdout.flush for char in itertools.cycle('|/-\\'): status = char + ' ' + msg - write(status) - flush() - write('\b' * len(status)) + print('\r' + status, end='', flush=True) try: await curio.sleep(.1) # <2> except curio.CancelledError: # <3> break - write(' ' * len(status) + '\b' * len(status)) + print('\r', end='') async def slow_function(): # <4> diff --git a/lab-spinner/spinner_thread.py b/lab-spinner/spinner_thread.py index 05b7b1a..d8dd923 100755 --- a/lab-spinner/spinner_thread.py +++ b/lab-spinner/spinner_thread.py @@ -9,19 +9,15 @@ import threading import itertools import time -import sys def spin(msg, done): # <1> - write, flush = sys.stdout.write, sys.stdout.flush for char in itertools.cycle('|/-\\'): status = char + ' ' + msg - write(status) - flush() - write('\b' * len(status)) + print('\r' + status, end='', flush=True) if done.wait(.1): # <2> break # <3> - write(' ' * len(status) + '\b' * len(status)) + print('\r', end='') def slow_function(): # <4> diff --git a/lab-spinner/spinner_yield.py b/lab-spinner/spinner_yield.py index df53df5..009ab03 100755 --- a/lab-spinner/spinner_yield.py +++ b/lab-spinner/spinner_yield.py @@ -8,22 +8,18 @@ import asyncio import itertools -import sys @asyncio.coroutine # <1> def spin(msg): - write, flush = sys.stdout.write, sys.stdout.flush for char in itertools.cycle('|/-\\'): status = char + ' ' + msg - write(status) - flush() - write('\b' * len(status)) + print('\r' + status, end='', flush=True) try: yield from asyncio.sleep(.1) # <2> except asyncio.CancelledError: # <3> break - write(' ' * len(status) + '\b' * len(status)) + print('\r', end='') @asyncio.coroutine # <4>