From 2591009372fb27194fd19633b9119bbefa0bf536 Mon Sep 17 00:00:00 2001 From: James Dixon Date: Mon, 25 May 2020 23:34:14 -0400 Subject: [PATCH] more cleanup --- bash/bashrc | 97 ++++++++++++++++++++++++++--------------------------- 1 file changed, 48 insertions(+), 49 deletions(-) diff --git a/bash/bashrc b/bash/bashrc index 4d126c4..377b46c 100644 --- a/bash/bashrc +++ b/bash/bashrc @@ -68,28 +68,33 @@ alias venvac='source venv/bin/activate' # COLORS # ------ # color vars using tput or ANSI/VT100 Control sequences -if command -v "tput" > /dev/null 2>&1 ; then - black="\[$(tput setaf 0)\]" - red="\[$(tput setaf 1)\]" - green="\[$(tput setaf 2)\]" - yellow="\[$(tput setaf 3)\]" - blue="\[$(tput setaf 4)\]" - purple="\[$(tput setaf 5)\]" - cyan="\[$(tput setaf 6)\]" - white="\[$(tput setaf 7)\]" - reset="\[$(tput sgr0)\]" - bold="\[$(tput bold)\]" +# check if tput is available +if [ -x "$(command -v tput)" ]; then + num_colors=$(tput colors) + if [ -n "$num_colors" ] && [ "$num_colors" -ge 8 ]; then + black="\[$(tput setaf 0)\]"; unesc_black="$(tput setaf 0)" + red="\[$(tput setaf 1)\]"; unesc_red="$(tput setaf 1)" + green="\[$(tput setaf 2)\]"; unesc_green="$(tput setaf 2)" + yellow="\[$(tput setaf 3)\]"; unesc_yellow="$(tput setaf 3)" + blue="\[$(tput setaf 4)\]"; unesc_blue="$(tput setaf 4)" + purple="\[$(tput setaf 5)\]"; unesc_purple="$(tput setaf 5)" + cyan="\[$(tput setaf 6)\]"; unesc_cyan="$(tput setaf 6)" + white="\[$(tput setaf 7)\]"; unesc_white="$(tput setaf 7)" + reset="\[$(tput sgr0)\]"; unesc_reset="$(tput sgr0)" + bold="\[$(tput bold)\]"; unesc_bold="$(tput bold)" + fi +# fallback to ANSI esacpe codes else - black="\[\033[0;30\]" - red="\[\033[1;31\]" - green="\[\033[1;32\]" - yellow="\[\033[1;33\]" - blue="\[\033[1;34\]" - purple="\[\033[1;35\]" - cyan="\[\033[1;36\]" - white="\[\033[1;37\]" - reset="\[\033[0m\]" - bold="\[\033[1m\]" + black="\[\033[0;30m\]"; unesc_black="\033[0;30m" + red="\[\033[1;31m\]"; unesc_red="\033[1;31m" + green="\[\033[1;32m\]"; unesc_green="\033[1;32m" + yellow="\[\033[1;33m\]"; unesc_yellow="\033[1;33m" + blue="\[\033[1;34m\]"; unesc_blue="\033[1;34m" + purple="\[\033[1;35m\]"; unesc_purple="\033[1;35m" + cyan="\[\033[1;36m\]"; unesc_cyan="\033[1;36m" + white="\[\033[1;37m\]"; unesc_white="\033[1;37m" + reset="\[\033[0m\]"; unesc_reset="\033[0m" + bold="\[\033[1m\]"; unesc_bold="\033[1m" fi # FUNCTIONS @@ -168,50 +173,44 @@ lg() { lazygit "$*"; } # git prompt function parse_git() { - # ways to get branches - # git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/' - # git rev-parse --abbrev-ref HEAD 2> /dev/null | sed -e 's/.*\/\(.*\)/\1/' - BRANCH="$(git rev-parse --abbrev-ref HEAD 2> /dev/null)" STATUS="$(git status 2> /dev/null)" - if [[ $? -ne 0 ]]; then return; else printf "$(tput sgr0)$(tput bold)%s" ":(${BRANCH})["; fi - - if echo "${STATUS}" | grep -c "nothing to commit" &> /dev/null; then printf "$(tput setaf 4)%s" "="; else printf ""; fi - - if echo "${STATUS}" | grep -c "renamed:" &> /dev/null; then printf "$(tput setaf 1)%s" ">"; else printf ""; fi - if echo "${STATUS}" | grep -c "deleted:" &> /dev/null; then printf "$(tput setaf 1)%s" "-"; else printf ""; fi - if echo "${STATUS}" | grep -c "new file:" &> /dev/null; then printf "$(tput setaf 2)%s" "+"; else printf ""; fi - - if echo "${STATUS}" | grep -c "branch is ahead:" &> /dev/null; then printf "$(tput setaf 3)%s" "!"; else printf ""; fi - if echo "${STATUS}" | grep -c "Untracked files:" &> /dev/null; then printf "$(tput setaf 3)%s" "?"; else printf ""; fi - if echo "${STATUS}" | grep -c "modified:" &> /dev/null; then printf "$(tput setaf 3)%s" "*"; else printf ""; fi - - printf "$(tput setaf 7)%s" "]" + # colors are unescaped because the whole function should be escaped in PS1 + if [[ $? -ne 0 ]]; then return; else printf "${unesc_reset}${unesc_bold}%s" ":(${BRANCH})["; fi + if echo "${STATUS}" | grep -c "nothing to commit" &> /dev/null; then printf "${unesc_blue}%s" "="; else printf ""; fi + if echo "${STATUS}" | grep -c "renamed:" &> /dev/null; then printf "${unesc_red}%s" ">"; else printf ""; fi + if echo "${STATUS}" | grep -c "deleted:" &> /dev/null; then printf "${unesc_red}%s" "-"; else printf ""; fi + if echo "${STATUS}" | grep -c "new file:" &> /dev/null; then printf "${unesc_green}%s" "+"; else printf ""; fi + if echo "${STATUS}" | grep -c "branch is ahead:" &> /dev/null; then printf "${unesc_yellow}%s" "!"; else printf ""; fi + if echo "${STATUS}" | grep -c "Untracked files:" &> /dev/null; then printf "${unesc_yellow}%s" "?"; else printf ""; fi + if echo "${STATUS}" | grep -c "modified:" &> /dev/null; then printf "${unesc_yellow}%s" "*"; else printf ""; fi + printf "${unesc_white}%s" "]" } # PROMPTS # ------- # start prompt string -PS1="${bold}" +# PS1="${bold}" # ***git prompts*** # PS1+="${blue}\w\[\$(parse_git)\] ${cyan}\\$ " -PS1+="${white}[\t] ${blue}\w\[\$(parse_git)\] ${green}\\$ " -# PS1+="${purple}\u${yellow}@${cyan}\h${white}:${blue}\w\[\$(parse_git)\]${yellow} \\$ " +# PS1+="${white}[\t] ${blue}\W\[\$(parse_git)\] ${green}\\$ " +# PS1+="${purple}\u${yellow}@${cyan}\h${white}:${blue}\W\[\$(parse_git)\]${green}\\$ " # PS1+="\n${cyan}\u ${white}at ${yellow}\h ${white}in ${blue}\w ${white}on \[\$(parse_git)\]\n${yellow}\\$ " # ***non git prompts*** -# PS1+="${blue}\w ${yellow}\\$ " -# PS1+="${purple}\u${yellow}@${cyan}\h${white}:${blue}\w ${yellow}\\$ " - -# ***non-color prompts*** -# PS1="\w \\$ " -# PS1="[\u@\h:\w] \\$ " -# PS1="\u@\h:\w \\$ " +# PS1+="${blue}\W ${yellow}\\$ " +# PS1+="${purple}\u${yellow}@${cyan}\h${white}:${blue}\W ${yellow}\\$ " # end prompt string -PS1+="${reset}" +# PS1+="${reset}" + +# ***non-color prompts*** +# PS1="\W \\$ " +# PS1="[\u@\h:\W]\\$ " +PS1="\u@\h:\W\\$ " + # BASH AUTOCOMPLETION # -------------------