Created
December 13, 2012 20:59
-
-
Save omnidan/4279793 to your computer and use it in GitHub Desktop.
Simple python module to create a simple typing animation with text in python and to change the color in unix shells.
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
from sys import stdout | |
from time import sleep | |
def type(text, nl=True, delay=0.01, amount=1): | |
l = list(text) | |
if amount > 1: | |
nl = [] | |
i = 0 | |
e = "" | |
for c in l: | |
if i == amount: | |
nl.append(e) | |
e = "" | |
i = 0 | |
e += c | |
i += 1 | |
nl.append(e) | |
l = nl | |
for c in l: | |
stdout.write(c) | |
stdout.flush() | |
sleep(delay) | |
if nl: stdout.write("\n") | |
stdout.flush() | |
def color(code, bold=False): | |
stdout.write("\x1b["); | |
if bold: stdout.write("1") | |
else: stdout.write("0") | |
stdout.write(";%dm" % code) | |
stdout.flush() | |
if __name__ == "__main__": | |
color(36, True) | |
type(""" | |
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris ut nisi neque. Nulla facilisi. Donec fringilla dignissim cursus. Phasellus in eros quis purus viverra rutrum. Cras vel mi non ante vehicula blandit. Suspendisse eleifend rutrum tortor a tincidunt. Sed condimentum, sapien eu consectetur porttitor, turpis augue luctus justo, in varius libero nunc eu purus. | |
""") | |
type(""" | |
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean rhoncus vehicula nisl sed congue. Pellentesque ante magna, cursus ac tincidunt ut, rutrum nec sem. Sed vulputate ullamcorper tempus. Maecenas felis mi, congue et cursus at, mollis quis nisi. Donec vestibulum aliquam est id gravida. Nullam tincidunt, sapien eu convallis laoreet, nisi metus viverra nisl, non porta magna tellus et urna. Praesent enim risus, hendrerit vitae porttitor cursus, mollis sit amet elit. | |
""", delay=0.001) | |
type(""" | |
Cras et augue neque. Donec at lorem eget lorem sodales fermentum vel vel metus. Cras non nisl a orci consequat eleifend eget vitae enim. Quisque condimentum laoreet dignissim. Sed semper nisl sed est dignissim in vestibulum eros accumsan. Fusce porttitor, nibh ut commodo varius, ante leo malesuada tortor, a tempor arcu odio et erat. Sed eget metus ac libero euismod accumsan. Sed pharetra libero id eros imperdiet placerat. Suspendisse potenti. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Curabitur mattis lacinia ante, vel faucibus ligula elementum ut. Vivamus ultricies dui eu nisl pretium vel dapibus est tempor. Suspendisse potenti. Proin ac nunc enim, nec iaculis enim. | |
""", amount=5) | |
color(0) | |
type("Reset color.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment