wplib-box icon indicating copy to clipboard operation
wplib-box copied to clipboard

Implement a custom prompt, and allow users to modify it

Open mikeschinkel opened this issue 7 years ago • 0 comments

@MickMake I can write the script to generate the prompt, but I need to know how to get it to trigger. I am thinking there should be some script users could modify, maybe a simple a .profile in their home directory?

Here is a strawman starting point for discussion. We can brainstorm what else should be included. See the screenshot for what the code that follows produces:

prompt

#!/usr/bin/env bash
function nox_prompt {
    local reset='\[\e[00m\]'
    local bold='\[\e[01m\]'
    local red='\[\e[31m\]'
    local green='\[\e[32m\]'
    local yellow='\[\e[33m\]'
    local blue='\[\e[34m\]'
    local purple='\[\e[35m\]'
    local cyan='\[\e[36m\]'
    local gray='\[\e[37m\]'
    local bright='\[\e[39m\]'
    local bc="${bold}${cyan}"
    local bg="${bold}${green}"
    local by="${bold}${yellow}"
    local br="${bold}${red}"
    local r="${reset}"
    local git=""
    local dir

    if [ "$PWD" == "/" ]; then
        dir="/"
    else
        dir="\w/"
    fi

    if [ -d "$(pwd)/.git" ]; then
        local branch="$(git branch --no-color | grep "*" | cut -c3-999 2>&1)"
        local hash="$(git log -1 --oneline --no-color|awk '{print $1}' 2>&1)"

        local tag="$(git describe --exact-match "${hash}" 2>&1)"
        if ! [[ "" == "${tag}" || "${tag}" =~ ^fatal ]]; then
            git="${br}tag${r}=${bg}${tag}${r}"
        fi

        if [ "(HEAD detached at" == "${branch:0:17}" ]; then
            if [ "" != "${git}" ]; then
                git=", ${git}"
            fi
            git="${br}HEAD${reset}=${bg}${branch:18:8}${r} (detached)${git}"
        elif [ "" != "${branch}" ] ; then
            git="${git}${br}branch${reset}=${bg}${branch}${r}"
        else
            git=""
        fi

        local remote="$(git remote 2>&1)"
        if ! [[ "" == "${remote}" || "${remote}" =~ ^fatal ]]; then
            if [ "" != "${git}" ]; then
                git="${git}, "
            fi
            git="${git}${br}remote${r}=${bg}${remote}${r}"
        fi

        if [ "" != "${git}" ]; then
            git="${by}GIT:${r} ${git}\n"
        fi
    fi

    dir="${bc}${dir} \n"

    PS1="\n${git}${dir}${reset}${green}\$${reset} "

}

PROMPT_COMMAND=box_prompt

mikeschinkel avatar Sep 10 '18 00:09 mikeschinkel