<commit_before>import sys
from terminal import *

def main():
    # Define grid
    (width, height) = get_term_size()
    grid = [(width-5)*[0] for i in range(height-5)]

    # Make initial grid be Conway's R-pentomino
    grid[len(grid)/2 - 1][len(grid[0])/2] = 1
    grid[len(grid)/2 + 0][len(grid[0])/2] = 1
    grid[len(grid)/2 + 1][len(grid[0])/2] = 1
    grid[len(grid)/2 - 1][len(grid[0])/2 + 1] = 1
    grid[len(grid)/2 + 0][len(grid[0])/2 - 1] = 1

    # Draw initial grid
    update_screen(grid)

    # Read initial config
    read_initial_conf(grid)

    # Step through grid
    prompt = ('ITER %d: Type anything to continue, the number of steps to ' + 
              'perform (or quit to exit): ')
    iter_step = 1
    update_screen(grid)
    while True:
        # Wait for user
        play = raw_input('%s' % (prompt % iter_step))
        if play == 'quit':
            break
        try:
            batch_steps = int(play)
        except:
            batch_steps = 1
            pass
        for i in range(batch_steps):
            # Define auxiliary grid matrix
            new_grid = [len(grid[0])*[0] for i in range(len(grid))]
            # Update grid
            next_step(grid, new_grid)
            grid, new_grid = new_grid, grid
            # Print updated grid
            update_screen(grid)
        iter_step += batch_steps


def update_screen(grid):
    """ update_screen: Takes the grid and updates the terminal to display it.
    (Making this function more efficient and informative is a to-do)
    """
    clear_terminal()
    print bcolors.RED + ' GAME OF LIFE' + bcolors.ENDC
    print bcolors.YELLOW + '-'*( len(grid[0]) + 5)+ bcolors.ENDC
    print
    for i, line in enumerate(grid):
        print bcolors.BLUE + '%3d ' % i + bcolors.ENDC,
        for element in line:
            if element:
                sys.stdout.write(bcolors.RED + str(element) + bcolors.ENDC)
            else:
                sys.stdout.write('0')
        print
    print '-' * ( len(grid[0]) + 5 )

def read_initial_conf(grid):
    """ read_initial_conf: Reads coordinates from the user to configure the
    initial game's grid.
    It takes an already defined grid and modifies it according to user input.
    (It shouldn't let invalid input leak)
    """
    done = False
    prompt = 'CONFIG %d: Type coordinates to toggle (or start to finish) %s: '
    config_step = 0
    last_coord = ''
    while True:
        # While input isn't valid, try reading and parsing it
        coord = []
        sys.stdout.write(prompt % (config_step, last_coord))
        while coord == []:
            # Read user's command
            cmd = raw_input()
            # Break if user is finished
            if cmd == 'start':
                done = True
                break
            try:
                cmd = cmd.split()
                coord = [int(cmd[i]) for i in range(2)]
            except:
                sys.stdout.write(bcolors.RED + '[Invalid input] %s' %
                                 (prompt % (config_step, last_coord)) 
                                 + bcolors.ENDC)
            last_coord = str(coord)
            config_step += 1
        # Total break if user is finished
        if done:
            break
        # Update grid (it actually toggles the grid position provided)
        grid [coord[1]][coord[0]] = (grid[coord[1]][coord[0]] + 1) % 2
        update_screen(grid)
<commit_msg>Minor UI tweaks and code clean-up.<commit_after>import sys
from terminal import *

def main():
    # Define grid
    (width, height) = get_term_size()
    grid = [(width-5)*[0] for i in range(height-5)]

    # Make initial grid be Conway's R-pentomino
    grid[len(grid)/2 - 1][len(grid[0])/2] = 1
    grid[len(grid)/2 + 0][len(grid[0])/2] = 1
    grid[len(grid)/2 + 1][len(grid[0])/2] = 1
    grid[len(grid)/2 - 1][len(grid[0])/2 + 1] = 1
    grid[len(grid)/2 + 0][len(grid[0])/2 - 1] = 1

    # Draw initial grid
    update_screen(grid)

    # Read initial config
    read_initial_conf(grid)

    # Step through grid
    prompt = ('ITER %d: Type anything to continue, the number of steps to ' + 
              'perform (or quit to exit): ')
    iter_step = 1
    update_screen(grid)
    while True:
        # Wait for user
        play = raw_input('%s' % (prompt % iter_step))
        if play == 'quit':
            break
        try:
            batch_steps = int(play)
        except:
            batch_steps = 1
            pass
        for i in range(batch_steps):
            # Define auxiliary grid matrix
            new_grid = [len(grid[0])*[0] for i in range(len(grid))]
            # Update grid
            next_step(grid, new_grid)
            grid, new_grid = new_grid, grid
            # Print updated grid
            update_screen(grid)
        iter_step += batch_steps


def update_screen(grid):
    """ update_screen: Takes the grid and updates the terminal to display it.
    (Making this function more efficient and informative is a to-do)
    """
    clear_terminal()
    print bcolors.RED + ' GAME OF LIFE' + bcolors.ENDC
    print bcolors.YELLOW + '-'*( len(grid[0]) + 5) + bcolors.ENDC
    print
    for i, line in enumerate(grid):
        print bcolors.BLUE + '%3d ' % i + bcolors.ENDC,
        for element in line:
            if element:
                sys.stdout.write(bcolors.RED + str(element) + bcolors.ENDC)
            else:
                sys.stdout.write('0')
        print
    print bcolors.YELLOW + '-' * ( len(grid[0]) + 5 ) + bcolors.ENDC

def read_initial_conf(grid):
    """ read_initial_conf: Reads coordinates from the user to configure the
    initial game's grid.
    It takes an already defined grid and modifies it according to user input.
    (It shouldn't let invalid input leak)
    """
    done = False
    prompt = 'CONFIG %d: Type coordinates to toggle (or start to finish) %s: '
    config_step = 0
    last_coord = ''
    while True:
        # While input isn't valid, try reading and parsing it
        coord = []
        sys.stdout.write(prompt % (config_step, last_coord))
        while coord == []:
            # Read user's command
            cmd = raw_input()
            # Break if user is finished
            if cmd == 'start' or cmd == '':
                done = True
                break
            try:
                cmd = cmd.split()
                coord = [int(cmd[i]) for i in range(2)]
            except:
                sys.stdout.write(bcolors.RED + '[Invalid input] %s' %
                                 (prompt % (config_step, last_coord)) 
                                 + bcolors.ENDC)
            last_coord = str(coord)
            config_step += 1
        # Total break if user is finished
        if done:
            break
        # Update grid (it actually toggles the grid position provided)
        grid [coord[1]][coord[0]] = (grid[coord[1]][coord[0]] + 1) % 2
        update_screen(grid)

{'fraction_non_alphanumeric': 0.11373173202141514, 'fraction_numerical': 0.014180292287657359, 'mean_word_length': 1.8432743726861374, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '25520', 'n_tokens_mistral': 2165, 'n_tokens_neox': 2057, 'n_words': 629}