bash prompt and echoing colors inside a function

后端 未结 6 1915
感情败类
感情败类 2020-12-24 00:50

I have this in my .bashrc:

LIGHTGREEN=\"\\[\\033[1;32m\\]\"
LIGHTRED=\"\\[\\033[1;31m\\]\"
WHITE=\"\\[\\033[0;37m\\]\"
RESET=\"\\[\\033[0;00m\\]\"

function          


        
6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-24 01:41

    Here's the coloured exit code portion of my PS1 code:

    color_enabled() {
        local -i colors=$(tput colors 2>/dev/null)
        [[ $? -eq 0 ]] && [[ $colors -gt 2 ]]
    }
    
    BOLD_FORMAT="${BOLD_FORMAT-$(color_enabled && tput bold)}"
    ERROR_FORMAT="${ERROR_FORMAT-$(color_enabled && tput setaf 1)}"
    RESET_FORMAT="${RESET_FORMAT-$(color_enabled && tput sgr0)}"
    
    # Exit code
    PS1='$(exit_code=$?; [[ $exit_code -eq 0 ]] || printf %s $BOLD_FORMAT $ERROR_FORMAT $exit_code $RESET_FORMAT " ")'
    

    Screenshot (with one Subversion repository path anonymized): Color coded output

提交回复
热议问题