#!/usr/bin/env python

import random
import time
import signal
import sys
import pygame
import curses
from curses import wrapper

def signal_handler(signal, frame):
    sys.exit(0);

signal.signal(signal.SIGINT, signal_handler)

def main(stdscr):
    screen_height = curses.LINES
    screen_width = curses.COLS - 1

    lines = []
    for j in range(screen_height):
        line = ''.join(['/' if random.random() < 0.1 else ' ' for i in range(screen_width)]) + '\n'
        lines.append(line)
    
    try:
        pygame.mixer.pre_init(44100, -16, 2, 2048)
        pygame.mixer.init()
        pygame.mixer.music.load('rain.wav')
        pygame.mixer.music.play(loops=-1)
    except:
        pass

    while True:
        stdscr.erase()

        for i in range(0, len(lines)-1):
            stdscr.addstr(i, 0, lines[i])
        stdscr.refresh()

        for i in reversed(range(1, len(lines))):
            lines[i] = lines[i-1]
            lines[i] = lines[i][:len(lines[i]) - 1]
            lines[i] = lines[i][1:] + lines[i][0] + '\n'
            #lines[i] = lines[i][screen_width - 1] + lines[i][:screen_width-1]

        lines[0] = ''.join(['/' if random.random() < 0.1 else ' ' for i in range(screen_width)]) + '\n'

        time.sleep(0.15)

wrapper(main)
