ROFLCOPTER

Posted on January 15, 2014

Update

Discovered how to perfectly run curses cross-platform


I’ve always loved colors in terminals/consoles/shells. Ncurses seemed like a nice tool for writing those elaborate command line interfaces, but lo and behold there’s no color support in windows! Great. Now what? Well despair not if you plan on using python, because Colorama provides all necessary color support (okay, not your 256-color support, but it enables escape characters). This means that you can set the foreground and background colors to a predefined although sufficient amount of colors, and choose from 3 different styles (dim, normal, bright).

So I wrote a simple python script that seems to be cross-platform and that shows an animated roflcopter that alternates colors and styles (in a  terminal). Of course it’s very silly, but I’m looking forward to integrating colorama with curses, maybe enabling curses (ncurses) to support colors on windows.

GitHub repo

And here’s the script itself (bless.py):

# -*- coding: utf-8 -*-
import time
import os
import colorama
from colorama import init, Fore, Back, Style
from random import choice
from random import randint
import platform

if (platform.system() is 'Windows'):
    init(autoreset=True)

copter = """
| ROFL:ROFL:R        |
|         _^___      |
|      __/   [] \    |
|LOL===__        \   |
|        \________]  |
|         I   I      |
|        --------/   |
"""
#copter2 = open(os.path.join(os.path.dirname(__file__), "roflcopter",
                           #"copter2.txt")).read()

copter2 = """
|         L:ROFL:ROFL|
|         _^___      |
| L    __/   [] \    |
| O ===__        \   |
| L      \________]  |
|         I   I      |
|        --------/   |
"""

pos = lambda y, x: '\x1b[%d;%dH' % (y, x)
MINY, MAXY = 1, 24
MINX, MAXX = 1, 80

FORES = [Fore.RED, Fore.GREEN, Fore.YELLOW,
        Fore.BLUE, Fore.MAGENTA, Fore.CYAN, Fore.WHITE]
BACKS = [Back.BLACK, Back.RED, Back.GREEN, Back.YELLOW,
        Back.BLUE, Back.MAGENTA, Back.CYAN, Back.WHITE]
STYLES = [Style.DIM, Style.NORMAL, Style.BRIGHT]

FORE = 0
STYLE = Style.NORMAL
BACK = 0
SUBCOUNT = 0

frame = True

def InitAll():
    print "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"

InitAll()

while (True):
    time.sleep(0.1)
    if (frame is True):
        print(pos(MINY, MINX) + FORES[FORE] + STYLE + BACKS[BACK] + copter)
        frame = False
    else:
        print(pos(MINY, MINX) + FORES[FORE] + STYLE + BACKS[BACK] + copter2)
        frame = True
    SUBCOUNT += 1
    if (SUBCOUNT >= 30):
        SUBCOUNT = 0
        FORE = randint(0, len(FORES) - 1)
        STYLE = choice(STYLES)
        BACK = randint(0, len(BACKS) - 1)
        if (BACK is not 0):
            while (BACK is FORE + 1):
                BACK = randint(0, len(BACKS) - 1)