~/.oh-my-zsh/themes/agnoster.themes 增加


strlen () {
    FOO=$1
    local zero='%([BSUbfksu]|([FB]|){*})'
    LEN=${#${(S%%)FOO//$~zero/}}
    echo $LEN
}

# show right prompt with date ONLY when command is executed
preexec () {
    DATE=$( date +"[%H:%M:%S]" )
    local len_right=$( strlen "$DATE" )
    len_right=$(( $len_right+1 ))
    local right_start=$(($COLUMNS - $len_right))

    local len_cmd=$( strlen "$@" )
    local len_prompt=$(strlen "$PROMPT" )
    local len_left=$(($len_cmd+$len_prompt))

    RDATE="\033[${right_start}C ${DATE}"

    if [ $len_left -lt $right_start ]; then
        # command does not overwrite right prompt
        # ok to move up one line
        #echo -e "\033[1A${RDATE}"

        # Black='\033[30m'        # Black
        # Red='\033[31m'          # Red
        # Green='\033[32m'        # Green
        # Yellow='\033[33m'       # Yellow
        # Blue='\033[34m'         # Blue
        # Purple='\033[35m'       # Purple
        # Cyan='\033[36m'         # Cyan
        # White='\033[37m'        # White

        echo -e "\033[1A\033[36m${RDATE}\033[36m"

    else
        echo -e "${RDATE}"
    fi

}