<commit_before>#!/bin/bash
# Workstation Configurator

    # To setup this script, run:
    #
    #   wget -O ~/.workstation git.io/workstation && . ~/.workstation setup
    #
    # This achieves a few things:
    # - Installation & Setup of Programs (Interactive)
    # - Provides ~/.workstation file which automatically loads function library
    # - Provides ~/.workstation.d directory for auto-loading your own scripts
    #
    # This script has been designed to demonstrate lots of various bash functionality,
    # and to include annotated examples, and references.
    #
    # For help, run:
    #
    #   workstation help


## CONFIGURATION ##

    # Packages to install
    package_list="git ssh tig vim tree xclip xdotool screen"

    # Configure History to automatically save
    export HISTCONTROL=ignoredups:erasedups  # no duplicate entries
    export HISTSIZE=100000                   # big big history
    export HISTFILESIZE=100000               # big big history
    shopt -s histappend                      # append to history, don't overwrite it

    # Save and reload the history after each command finishes
    export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"

    # Configure history to save with timestamp
    export HISTTIMEFORMAT="%Y-%m-%d %T "
    
    # Bash Prompt Colour
    # https://wiki.archlinux.org/index.php/Color_Bash_Prompt
    set_prompt()
    {
        local previous_exit_code=$?
        local blue='\[\e[01;34m\]'
        local red='\[\e[01;31m\]'
        local green='\[\e[01;32m\]'
        local white='\[\e[00m\]'
        local cross='\342\234\227'
        local tick='\342\234\223'
        local git_branch="$(git branch 2> /dev/null | grep '*' | awk '{print $NF}' | sed 's/)//g')"
        PS1=""

        # Tick or a cross depending on the previous exit code
        [[ $previous_exit_code == 0 ]] && PS1+="$green$tick" || PS1+="$red$cross"
        # PS1+="$white|"
        PS1+=" "

        # Host, or user@host depending on user
        [[ $EUID == 0 ]] && PS1+="$red\\h" || PS1+="$green\\u@\\h"
        PS1+="$white:"

        # Working directory
        PS1+="$blue\\w"

        # Green or red git branch, depending on dirty
        if [ -n "$git_branch" ]
        then
            # PS1+="$white|"
            PS1+=" "
            [[ -z $(git status --porcelain) ]] && PS1+="$green" || PS1+="$red"
            PS1+="$git_branch"
        fi

        # Print the special dollar sign, and a space
        PS1+="$white\\\$ "
    }
    PROMPT_COMMAND='set_prompt'

    # Generating Help
    # This saves a list of defined functions to the variable
    # It will be compared to a list we'll make futher down,
    # so we can know which functions were defined.
    [[ "$original_function_list" ]] || original_function_list=$(compgen -A function)


## MISC FUNCTIONS ##

    # Writes a log to ~/.workstation.log/
    # Usage: echo <message> | log [<logfile name>]
    # eg: echo "hello world" | log
    # eg: echo "hello world" | log mylog.log
    log()
    {
        local message
        # Read message from STDIN
        read message
        # If dir does not exist, create it
        [ -d ~/.workstation.log ] || mkdir ~/.workstation.log
        # Set provide default value for first argument
        [ -z "$1" ] && log_basename='workstation.log' || log_basename="$1"
        # Concatenate dir with basename
        local log_path=~/.workstation.log/"$log_basename"
        # Create logfile if it doesn't exist
        touch "$log_path"
        # Get current time in ISO8601 format
        # eg: 2014-08-29T19:01:46+10:00
        local date=$(date +%F\T%T%z | sed 's/^.\{22\}/&:/')
        # Get process ID
        local pid=$$
        # Append message to log file
        echo "$date $pid $message" >> "$log_path"
    }

    # Executes a command repeatedly
    # usage: repeat <command> [<frequency in seconds>]
    #   CTRL+c to quit
    # eg: repeat date
    # Note that this is similar to `watch`
    repeat()
    {
        echo "TODO. fix me"; return
        local command=$1           # command is first arg
        local frequency=${2:-1}    # frequency is second arg or "1"

        clear

        trap ctrl_c INT            # catch ctrl+c keypress
        function ctrl_c
        {
            clear
            tput cnorm              # put the cursor back to normal
            return
        }

        while [ true ]              # Repeat forever
        do
            tput civis              # hide the cursor
            tput cup 0 0            # put cursor at top-left
            $command                # execute the command
            sleep $frequency        # wait for frequency
        done
    }

    # Displays "Press [ENTER] to cancel..."
    # Returns:
    #   true after a 4 second timeout
    #   false if the user presses enter
    # eg:
    #   echo "about to do the thing"
    #   if enter_to_cancel
    #   then
    #       echo "doing the thing"
    #   fi
    enter_to_cancel()
    {
        # Display the message
        echo -n "Press [ENTER] to cancel"
        # Give them some time to read
        local i
        for i in 1 2 3 4
        do<commit_msg>updated with workstation push<commit_after>#!/bin/bash
# Workstation Configurator

    # To setup this script, run:
    #
    #   wget -O ~/.workstation git.io/workstation && . ~/.workstation setup
    #
    # This achieves a few things:
    # - Installation & Setup of Programs (Interactive)
    # - Provides ~/.workstation file which automatically loads function library
    # - Provides ~/.workstation.d directory for auto-loading your own scripts
    #
    # This script has been designed to demonstrate lots of various bash functionality,
    # and to include annotated examples, and references.
    #
    # For help, run:
    #
    #   workstation help


## CONFIGURATION ##

    # Packages to install
    package_list="git ssh tig vim tree xclip xdotool screen"

    # Configure History to automatically save
    export HISTCONTROL=ignoredups:erasedups  # no duplicate entries
    export HISTSIZE=100000                   # big big history
    export HISTFILESIZE=100000               # big big history
    shopt -s histappend                      # append to history, don't overwrite it

    # Save and reload the history after each command finishes
    export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"

    # Configure history to save with timestamp
    export HISTTIMEFORMAT="%Y-%m-%d %T "
    
    # Bash Prompt Colour
    # https://wiki.archlinux.org/index.php/Color_Bash_Prompt
    set_prompt()
    {
        local previous_exit_code=$?
        local blue='\[\e[01;34m\]'
        local red='\[\e[01;31m\]'
        local green='\[\e[01;32m\]'
        local white='\[\e[00m\]'
        local cross='\342\234\227'
        local tick='\342\234\223'
        local git_branch="$(git branch 2> /dev/null | grep '*' | awk '{print $NF}' | sed 's/)//g')"
        PS1=""

        # Tick or a cross depending on the previous exit code
        [[ $previous_exit_code == 0 ]] && PS1+="$green$tick" || PS1+="$red$cross"
        # PS1+="$white|"
        PS1+=" "

        # Host, or user@host depending on user
        [[ $EUID == 0 ]] && PS1+="$red\\h" || PS1+="$green\\u@\\h"
        PS1+="$white:"

        # Working directory
        PS1+="$blue\\w"

        # Green or red git branch, depending on dirty
        if [ -n "$git_branch" ]
        then
            # PS1+="$white|"
            PS1+=" "
            [[ -z $(git status --porcelain) ]] && PS1+="$green" || PS1+="$red"
            PS1+="$git_branch"
        fi

        # Print the special dollar sign, and a space
        PS1+="$white\\\$ "
    }
    PROMPT_COMMAND='set_prompt'

    # Generating Help
    # This saves a list of defined functions to the variable
    # It will be compared to a list we'll make futher down,
    # so we can know which functions were defined.
    [[ "$original_function_list" ]] || original_function_list=$(compgen -A function)


## MISC FUNCTIONS ##

    # Writes a log to ~/.workstation.log/
    # Usage: echo <message> | log [<logfile name>]
    # eg: echo "hello world" | log
    # eg: echo "hello world" | log mylog.log
    log()
    {
        local message
        # Read message from STDIN
        read message
        # If dir does not exist, create it
        [ -d ~/.workstation.log ] || mkdir ~/.workstation.log
        # Set provide default value for first argument
        [ -z "$1" ] && log_basename='workstation.log' || log_basename="$1"
        # Concatenate dir with basename
        local log_path=~/.workstation.log/"$log_basename"
        # Create logfile if it doesn't exist
        touch "$log_path"
        # Get current time in ISO8601 format
        # eg: 2014-08-29T19:01:46+10:00
        local date=$(date +%F\T%T%z | sed 's/^.\{22\}/&:/')
        # Get process ID
        local pid=$$
        # Append message to log file
        echo "$date $pid $message" >> "$log_path"
    }

    # Executes a command repeatedly
    # usage: repeat <command> [<frequency in seconds>]
    #   CTRL+c to quit
    # eg: repeat date
    # Note that this is similar to `watch`
    repeat()
    {
        echo "TODO. fix me"; return
        local command=$1           # command is first arg
        local frequency=${2:-1}    # frequency is second arg or "1"

        clear

        trap ctrl_c INT            # catch ctrl+c keypress
        function ctrl_c
        {
            clear
            tput cnorm              # put the cursor back to normal
            return
        }

        while [ true ]              # Repeat forever
        do
            tput civis              # hide the cursor
            tput cup 0 0            # put cursor at top-left
            $command                # execute the command
            sleep $frequency        # wait for frequency
        done
    }
    
    # Watches a directory (recursively) for changes
    # If any files within the directory change, executes command
    # Usage: execute_on_change <dir> <command>
    # eg: execute_on_change /tmp "echo yep"
    execute_on_change()
    {
        local path="$1"
        local command="$2"
        local current_hash=""
        local new_hash=""
        while [[ true ]]
        do
            new_hash="$(find "$path" -type f | md5sum)"
            if [[ $old_hash != $new_hash ]]
            then           
                $command
                old_hash=$new_hash
            fi
            sleep 2
            echo "hash: $new_hash"
        done
    }

    # Displays "Press [ENTER] to cancel..."
    # Returns:
    #   true after a 4 second timeout
    #   false if the user presses enter
    # eg:
    #   echo "about to do the thing"
    #   if enter_to_cancel
    #   then
    #       echo "doing the thing"
    #   fi
    enter_to_cancel()
    {
        # Display the message
        echo -n "Press [ENTER] to cancel"
        # Give them some time to read
        local i
        for i in 1 2 3 4
        do
{'fraction_non_alphanumeric': 0.10985020426690877, 'fraction_numerical': 0.018338629142078983, 'mean_word_length': 1.8318766066838046, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1723748', 'n_tokens_mistral': 3430, 'n_tokens_neox': 3208, 'n_words': 1066}