Last active
November 24, 2019 14:25
-
-
Save danilin-em/35a92da221ac9df51f65c05df39c231f to your computer and use it in GitHub Desktop.
Animation for long string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" Animation for long string | |
Usage: | |
myapp.py: | |
----------------------------------------------------------------------------------- | |
line = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod' | |
idx = 0 | |
while True: | |
idx, short_line = line_animation(line, idx, 5) | |
print(short_line, end='\r') | |
time.sleep(0.2) | |
----------------------------------------------------------------------------------- | |
Now see animation: | |
$ python3 myapp.py | |
Lorem ipsu | |
""" | |
def line_animation(line, idx, limit=10): | |
line_start = 0 | |
line_end = len(line) | |
line = (' ' * (limit - 1)) + line | |
if limit+idx > line_end: | |
line = line + ' ' | |
short_line = line[line_start+idx:limit+idx] | |
idx += 1 | |
if idx > (line_end + limit) - 1: | |
idx = 0 | |
short_line = short_line+(' '*(limit-len(short_line))) | |
return (idx, short_line) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment