hg.sh
1 #!/usr/bin/env bash 2 3 current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 4 source $current_dir/utils.sh 5 6 IFS=' ' read -r -a hide_status <<< $(get_tmux_option "@dracula-hg-disable-status" "false") 7 IFS=' ' read -r -a current_symbol <<< $(get_tmux_option "@dracula-hg-show-current-symbol" "✓") 8 IFS=' ' read -r -a diff_symbol <<< $(get_tmux_option "@dracula-hg-show-diff-symbol" "!") 9 IFS=' ' read -r -a no_repo_message <<< $(get_tmux_option "@dracula-hg-no-repo-message" "") 10 IFS=' ' read -r -a no_untracked_files <<< $(get_tmux_option "@dracula-hg-no-untracked-files" "false") 11 12 # Get added, modified, and removed files from hg status 13 getChanges() 14 { 15 declare -i added=0; 16 declare -i deleted=0; 17 declare -i modified=0; 18 declare -i removed=0; 19 declare -i untracked=0; 20 21 for i in $(hg -R $path status -admru) 22 do 23 case $i in 24 'A') 25 added+=1 26 ;; 27 '!') 28 deleted+=1 29 ;; 30 'M') 31 modified+=1 32 ;; 33 'R') 34 removed+=1 35 ;; 36 '?') 37 untracked+=1 38 ;; 39 40 esac 41 done 42 43 output="" 44 [ $added -gt 0 ] && output+="${added}A" 45 [ $modified -gt 0 ] && output+=" ${modified}M" 46 [ $deleted -gt 0 ] && output+=" ${deleted}D" 47 [ $removed -gt 0 ] && output+=" ${removed}R" 48 [ $no_untracked_files == "false" -a $untracked -gt 0 ] && output+=" ${untracked}?" 49 50 echo $output 51 } 52 53 54 # getting the #{pane_current_path} from dracula.sh is no longer possible 55 getPaneDir() 56 { 57 nextone="false" 58 for i in $(tmux list-panes -F "#{pane_active} #{pane_current_path}"); 59 do 60 if [ "$nextone" == "true" ]; then 61 echo $i 62 return 63 fi 64 if [ "$i" == "1" ]; then 65 nextone="true" 66 fi 67 done 68 } 69 70 71 # check if the current or diff symbol is empty to remove ugly padding 72 checkEmptySymbol() 73 { 74 symbol=$1 75 if [ "$symbol" == "" ]; then 76 echo "true" 77 else 78 echo "false" 79 fi 80 } 81 82 # check to see if the current repo is not up to date with HEAD 83 checkForChanges() 84 { 85 [ $no_untracked_files == "false" ] && no_untracked="-u" || no_untracked="" 86 if [ "$(checkForHgDir)" == "true" ]; then 87 if [ "$(hg -R $path status -admr $no_untracked)" != "" ]; then 88 echo "true" 89 else 90 echo "false" 91 fi 92 else 93 echo "false" 94 fi 95 } 96 97 # check if a hg repo exists in the directory 98 checkForHgDir() 99 { 100 if [ "$(hg -R $path branch)" != "" ]; then 101 echo "true" 102 else 103 echo "false" 104 fi 105 } 106 107 # return branch name if there is one 108 getBranch() 109 { 110 if [ $(checkForHgDir) == "true" ]; then 111 echo $(hg -R $path branch) 112 else 113 echo $no_repo_message 114 fi 115 } 116 117 # return the final message for the status bar 118 getMessage() 119 { 120 if [ $(checkForHgDir) == "true" ]; then 121 branch="$(getBranch)" 122 output="" 123 124 if [ $(checkForChanges) == "true" ]; then 125 126 changes="$(getChanges)" 127 128 if [ "${hide_status}" == "false" ]; then 129 if [ $(checkEmptySymbol $diff_symbol) == "true" ]; then 130 output=$(echo "${changes} $branch") 131 else 132 output=$(echo "$diff_symbol ${changes} $branch") 133 fi 134 else 135 if [ $(checkEmptySymbol $diff_symbol) == "true" ]; then 136 output=$(echo "$branch") 137 else 138 output=$(echo "$diff_symbol $branch") 139 fi 140 fi 141 142 else 143 if [ $(checkEmptySymbol $current_symbol) == "true" ]; then 144 output=$(echo "$branch") 145 else 146 output=$(echo "$current_symbol $branch") 147 fi 148 fi 149 150 echo "$output" 151 else 152 echo $no_repo_message 153 fi 154 } 155 156 main() 157 { 158 path=$(getPaneDir) 159 getMessage 160 } 161 162 #run main driver program 163 main