独自のプロンプト

バリエーションA

# [2021.08.29] 追加
#####################
# 独自のprompt
#####################

function smart_pwd {
    local pwdmaxlen=$(( $COLUMNS-35 ))
    local trunc_symbol=".."
    local dir=${PWD##*/}
    local tmp=""
    pwdmaxlen=$(( ( pwdmaxlen < ${#dir} ) ? ${#dir} : pwdmaxlen ))
    if [[ $PWD == $HOME* ]]; then
        SMART_PWD="~${PWD#${HOME}}"
    else
        SMART_PWD="$PWD"
    fi
    # set title
    printf "\033]0;%s\007" "${USER}@${HOSTNAME}: ${SMART_PWD}"

    SMART_PWD_TRUNC=""
    local pwdoffset=$(( ${#SMART_PWD} - pwdmaxlen ))
    if [ ${pwdoffset} -gt "0" ]
    then
        tmp=${SMART_PWD:$pwdoffset:$pwdmaxlen}
        tmp=${trunc_symbol}/${tmp#*/}
        if [ "${#tmp}" -lt "${#SMART_PWD}" ]; then
            SMART_PWD_TRUNC="${trunc_symbol}"
            SMART_PWD="${tmp:${#trunc_symbol}}"
        fi
    fi
}

source ~/.git-prompt.sh

PROMPT_COMMAND="smart_pwd"
PS1_SEC_1="\[\e[$[COLUMNS-19]C\]\[\033[30m\033[47m\D{%a %b %d %T}\r\033[0m\]"
PS1_SEC_2="\[\033[48;5;17m\033[38;5;117m\]\u"
PS1_SEC_3="\[\033[1;37;42m\]@ \[\033[1;37;42m\]\h \[\033[48;5;232m\033[38;5;201m\]"
PS1_SEC_5="\[\033[38;5;86m\] "
PS1_SEC_7="\[\033[0m\]\n\[\033[38;5;41m\]"
PS1_SEC_9="\[\033[38;5;208m\]\$ \[\033[0m\]"


PS1=${PS1_SEC_1}${PS1_SEC_3}'${SMART_PWD_TRUNC}'${PS1_SEC_5}'${SMART_PWD}'${PS1_SEC_7}'$(__git_ps1 "(%s)")'${PS1_SEC_9}


#####################
# shellの再起動
#####################

alias bashrl='exec $SHELL -l'



バリエーションB

#####################
# 独自のprompt
#####################

function smart_pwd {
    local pwdmaxlen=$(( $COLUMNS-35 ))
    local trunc_symbol=".."
    local dir=${PWD##*/}
    local tmp=""
    pwdmaxlen=$(( ( pwdmaxlen < ${#dir} ) ? ${#dir} : pwdmaxlen ))
    if [[ $PWD == $HOME* ]]; then
        SMART_PWD="~${PWD#${HOME}}"
    else
        SMART_PWD="$PWD"
    fi
    # set title
    printf "\033]0;%s\007" "${USER}@${HOSTNAME}: ${SMART_PWD}"

    SMART_PWD_TRUNC=""
    local pwdoffset=$(( ${#SMART_PWD} - pwdmaxlen ))
    if [ ${pwdoffset} -gt "0" ]
    then
        tmp=${SMART_PWD:$pwdoffset:$pwdmaxlen}
        tmp=${trunc_symbol}/${tmp#*/}
        if [ "${#tmp}" -lt "${#SMART_PWD}" ]; then
            SMART_PWD_TRUNC="${trunc_symbol}"
            SMART_PWD="${tmp:${#trunc_symbol}}"
        fi
    fi
}

# git-prompt.shの中に__git_ps1があるので、インクルードする
# ファイルパスは環境によって違う可能性あり。
# https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh をダウンロードでもよい
source ~/.git-prompt.sh

PROMPT_COMMAND="smart_pwd"
PS1_SEC_1="\[\e[$[COLUMNS-19]C\]\[\033[48;5;55m\033[38;5;220m\D{%a %b %d %T}"
PS1_SEC_2="\r\033[0m\]\[\033[48;5;17m\033[38;5;117m\]\u\[\033[38;5;114m\]"
PS1_SEC_3="@\[\033[38;5;117m\]\h \[\033[48;5;232m\033[38;5;201m\]"
# PS1_SEC_4=$SMART_PWD_TRUNC
PS1_SEC_5="\[\033[38;5;86m\]"
# PS1_SEC_6=$SMART_PWD
PS1_SEC_7="\[\033[0m\]\n\[\033[38;5;41m\]"
# PS1_SEC_8=$(__git_ps1 "(%s)")
PS1_SEC_9="\[\033[38;5;208m\]\$ \[\033[0m\]"


PS1=${PS1_SEC_1}${PS1_SEC_2}${PS1_SEC_3}'${SMART_PWD_TRUNC}'${PS1_SEC_5}'${SMART_PWD}'${PS1_SEC_7}'$(__git_ps1 "(%s)")'${PS1_SEC_9}


#####################
# shellの再起動
#####################

alias bashrl='exec $SHELL -l'