Skip to content

Instantly share code, notes, and snippets.

@RecNes
Last active March 19, 2020 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RecNes/076d2a47244b23df15a60c09e001be20 to your computer and use it in GitHub Desktop.
Save RecNes/076d2a47244b23df15a60c09e001be20 to your computer and use it in GitHub Desktop.
import os
import time
def get_console_width():
"""
Returns the width of console.
NOTE: The below implementation works only on Linux-based operating systems.
If you wish to use it on another OS, please make sure to modify it appropriately.
"""
return int(os.popen('stty size').read().split()[1])
try:
console_width = get_console_width()
except:
console_width = 80
global dot_len
global old_time
dot_len = 0
old_time = time.time()
def processing(dot_count=5, dot_type='.'):
"""
Use this method in a loop.
"""
global dot_len
global old_time
if dot_len >= dot_count:
dot_len = 0
if int(time.time() - old_time) >= 1:
dot_len += 1
old_time = time.time()
processing_text = "Processing{}".format(dot_type * dot_len)
empty_spaces = console_width - len(processing_text)
full_line_text = "{}\r".format(processing_text, " " * (empty_spaces - 1))
print(full_line_text[0:console_width], end='', flush=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment