dot

packages and services management
Log | Files | Refs | README

commit 574be868af22aacc782705456ef6451293da2c86
parent 617729d9061585086a02d4148170ac9a73452e72
Author: josuah <mail@josuah.net>
Date:   Tue,  1 Nov 2016 18:22:59 -0400

Trying to improve speed by writing it in awk.

Diffstat:
Mdvtm/dvtm-status | 2+-
Mshell/.profile | 157++++++++++++++++++++++++++++++++++++-------------------------------------------
2 files changed, 72 insertions(+), 87 deletions(-)

diff --git a/dvtm/dvtm-status b/dvtm/dvtm-status @@ -8,8 +8,8 @@ export CMD_FIFO="/tmp/dvtm-cmd.$$" while true do - printf '\n' status + printf '\n' sleep 10 done > "$STATUS_FIFO" & diff --git a/shell/.profile b/shell/.profile @@ -16,116 +16,101 @@ # PROMPT #------------------------------------------------------------------------------- -trap 'status_line' SIGWINCH +PS1=' +$( + [ "$?" -eq 0 ] && color=32 || color=31 -PS1='$(ps1 "$?")' + # status + status_line + + printf " \033[1;30m>\033[0;%sm>\033[1m>\033[0m " "$color" +)' PS2=' > ' PS3=' > ' ps1() { - local code="$1" color - [ "$code" -eq 0 ] && color=32 || color=31 +} +status_line() +{ local pwd="$(pwd)" [ -z "${pwd##$HOME*}" ] && pwd="~${pwd#$HOME}" - local col="$(stty size)"; + local status="$(status)" + + local col="$(stty size)" col="$((${col#* } - ${#status} - 1))" - # terminal name - printf '\033]0;%s %s\007' "$pwd" + printf '\033[s\033[0;0H\033[K' - status_line + # right part + printf '\033[%sC%s\r' "$col" "$status" - # prompt - printf '\n \033[1;30m>\033[0;%sm>\033[1m>\033[0m ' "$color" -} + # left part + printf ' %s %s' "$pwd" "$(git_status)" -status_line() -{ - local pwd="$(pwd)" - [ -z "${pwd##$HOME*}" ] && pwd="~${pwd#$HOME}" + # terminal name + printf '\033]0;%s %s\007' "$pwd" - printf '\n\n\033[s\033[0;0H\033[K' - printf '\r %s %s\n\033[K\033[u\033[2A' "$pwd" "$(git_status)" + printf '\n\033[K\033[u' } git_status() { git rev-parse &>/dev/null || return 0 - local branch branch_line - local behind=0 ahead=0 untracked=0 conflicts=0 changed=0 staged=0 - - while IFS='' read -r line - do - status="$(printf %s "$line" | sed -r 's/(..).*/\1/')" - # thanks to magicmonty@github for this part - while [ "$status" ] - do - case "$status" in - #two fixed character matches, loop finished - '##' ) branch_line="$line"; break ;; - '??' ) untracked="$((untracked + 1))"; break ;; - U? ) conflicts="$((conflicts + 1))"; break ;; - ?U ) conflicts="$((conflicts + 1))"; break ;; - DD ) conflicts="$((conflicts + 1))"; break ;; - AA ) conflicts="$((conflicts + 1))"; break ;; - - # first loop: two character matches - ?M ) changed="$((changed + 1))" ;; - ?D ) changed="$((changed + 1))" ;; - ?' ') ;; - - # second loop: single character matches - U ) conflicts="$((conflicts + 1))" ;; - ' ' ) ;; - * ) staged="$((staged + 1))" ;; - esac - - status="${status%?}" - - done - done << EOF -$(git status --porcelain -b) -EOF - - branch="${branch_line# ## }" branch="${branch%%...*}" - branch_line="${branch_line%]}" - - if [ -z "${branch_line##*ahead *}" ] - then - ahead="${branch_line#*ahead }" ahead="${ahead%, *}" - fi - - if [ -z "${branch_line##*behind *}" ] - then - behind="${branch_line#*behind }" behind="${behind%, *}" - fi - - printf '\033[1m%s' "${branch#??}" - - if [ "$untracked$conflicts$changed$staged$ahead$behind" = '000000' ] - then - printf ' \033[32m✔\033[0m' && return - fi - - [ "${untracked#0}" ] && printf ' \033[34m?\033[37m%s' "$untracked" - [ "${conflicts#0}" ] && printf ' \033[31m!\033[37m%s' "$conflicts" - [ "${changed#0}" ] && printf ' \033[32m+\033[37m%s' "$changed" - [ "${staged#0}" ] && printf ' \033[33m*\033[37m%s' "$staged" - [ "${behind#0}" ] && printf ' \033[36m↓\033[37m%s' "$behind" - [ "${ahead#0}" ] && printf ' \033[36m↑\033[37m%s' "$ahead" - - printf '\033[0m' + git status --porcelain -b | awk ' + + /^## / { + branch = $0; + sub(/^##/, "", branch); + sub(/\.\.\..*/, "", branch); + + if ($0 ~ /ahead /) { + ahead = $0; + sub(/.*ahead /, "", ahead); + sub(/\].*|, .*/, "", ahead); + } + + if ($0 ~ /behind /) { + behind = $0; + sub(/.*behind /, "", behind); + sub(/\].*|, .*/, "", behind); + } + + m = 1; + } + + m == 0 && /^\?\? / { untracked++; m = 1; } + m == 0 && /^U. / { conflicts++; m = 1; } + m == 0 && /^.U / { conflicts++; m = 1; } + m == 0 && /^DD / { conflicts++; m = 1; } + m == 0 && /^AA / { conflicts++; m = 1; } + m == 0 && /^.M / { changed++; } + m == 0 && /^.D / { changed++; } + m == 0 && /^[^ ]. / { staged++; } + m == 1 { m = 0; } + + END { + printf("\033[1m%s", branch); + + if (untracked + conflicts + changed + staged + behind + ahead \ + == 0) { + printf " \033[32m✔"; + } else { + if (untracked) printf " \033[34m?\033[37m%d", untracked; + if (conflicts) printf " \033[31m!\033[37m%d", conflicts; + if (changed ) printf " \033[32m+\033[37m%d", changed ; + if (staged ) printf " \033[33m*\033[37m%d", staged ; + if (behind ) printf " \033[36m↓\033[37m%d", behind ; + if (ahead ) printf " \033[36m↑\033[37m%d", ahead ; + } + + printf "\033[0m"; + }' } -while true -do - - sleep 10 -done & # ALIAS #-------------------------------------------------------------------------------