#!/bin/bash

# playGameOfLifeTxt: play text files generated by game of life, using empty line as delimiter

# usage: playGameOfLifeTxt 50.txt 50b.txt 20.txt
#        sleep "0.3" playGameOfLifeTxt 20.txt

tput civis
# give me my cursor back
function cleanup() {
    tput cnorm
}
trap cleanup EXIT

# var
[[ -z "$sleep" ]] && sleep="0.03"

# main file by file
while [ $# -gt 0 ]; do

    clear
    #tput civis
    file="$1"
    lastLine="$(cat "$file" | wc -l)"
    cLines="0"

    # line by line, empty line is separator
    
    while read -r line; do
        (( cLines+=1 ))
        if [[ -z "$line" ]] ; then # if empty line

            sleep "$sleep"
            if (( cLines == lastLine )); then                
                echo
            else
                
                tput civis # This is slow
                tput cup 0 0
                
            fi
            
        else
            echo "$line"
        fi

    done < "$file"

shift
done