Snippets

Created by Samuel Laurén
declare -A COLORS
COLORS[black]=30
COLORS[red]=31
COLORS[green]=32
COLORS[yellow]=33
COLORS[blue]=34
COLORS[magenta]=35
COLORS[cyan]=36
COLORS[white]=37

function color { # [-n/-e/-b] -> color -> string
    local ln="\n" es="" ee="" bright=""
    while [[ ! " ${!COLORS[@]} " =~ " $1 " ]] && [[ $# -ne 0 ]]; do
        case "$1" in
            "-n")
                ln=""
                ;;
            "-e")
                es="\["
                ee="\]"
                ;;
            "-b")
                bright=";1"
                ;;
            *)
                echo "Error: unknown argument \"$1\"" >&2
                return 1
                ;;
        esac
        shift
    done
    [[ $# -ne 2 ]] && { echo "Error: missing argument" >&2; return 1; }
    printf "%b\e[%d%sm%b%b%b\e[0m%b%b" \
           "$es" "${COLORS[$1]}" "$bright" "$ee" "$2" "$es" "$ee" "$ln"
}

declare -ra PATH_PALETTE=( 21 56 89 160 202 205 201 165 135 69 )

function color-path {
    local path="${1:1}"
    [[ -z "$path" ]] && { color -n -b -e "black" "/"; return; }
    local -a parts
    local -i len index
    IFS="/" read -a parts <<< "$path"
    len="${#parts[@]}"
    for n in "${!parts[@]}"; do
        color -n -b -e "black" "/"
        index="$(( n % len ))"
        printf "\[\e[38;5;%dm\]%s\[\e[0m\]" "${PATH_PALETTE[$index]}" "${parts[$n]}"
    done
}

function git-status-string {
    local -i modified=0 added=0 deleted=0 renamed=0 copied=0 untracked=0
    while IFS=" " read state name; do
        case "$state" in
            'M')
                modified=$((modified + 1))
                ;;
            'A')
                added=$((added + 1))
                ;;
            'D')
                deleted=$((deleted + 1))
                ;;
            'R')
                renamed=$((renamed + 1))
                ;;
            'C')
                copied=$((copied + 1))
                ;;
            '??')
                untracked=$((untracked + 1))
                ;;
        esac
    done < <(git -C "$1" status --porcelain)
    [[ "$modified" -gt 0 ]] && color -n -b -e "yellow" "🅼 $modified"
    [[ "$added" -gt 0 ]] && color -n -e "green" "🅰 $added"
    [[ "$deleted" -gt 0 ]] && color -n -e "red" "🅳 $deleted"
    [[ "$renamed" -gt 0 ]] && color -n -e "blue" "🆁 $renamed"
    [[ "$copied" -gt 0 ]] && color -n -b -e "green" "🅲 $copied"
    [[ "$untracked" -gt 0 ]] && color -n -b -e "cyan" "🆄 $untracked"
}

function reset-prompt {
    local code="$?" \
          count="$(jobs -p | wc -l)" \
          status=""
    [[ "$code" -ne 0 ]] && status="$status $(color -n -e "red" "※ $code")"
    [[ "$count" -gt 0 ]] && status="$status $(color -n -e "yellow" "🅹 $count")"
    [[ -n "$RANGER_LEVEL" ]] && status="$status $(color -n -e "green" "🆁") "
    [[ -d ".git" ]] && status="$status $(git-status-string "$PWD")"
    [[ ! -z "$status" ]] && status="$(color -n -b -e "black" "[")${status:1}$(color -n -b -e "black" "]"):"
    PS1="\u:$status$(color-path "$PWD") $(color -n -b -e magenta "»") "
    history -a; history -c; history -r
}

PROMPT_COMMAND="reset-prompt"

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.