From ba76c45683b735cb9b27c10964b66b1c4f610d31 Mon Sep 17 00:00:00 2001 From: Lucas Date: Mon, 9 Dec 2019 11:03:41 +0000 Subject: [PATCH] Fix shgit shell style and reenable it --- dotfiles/shrc | 10 ++++++++-- utils/shgit | 30 ++++++++++++++++-------------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/dotfiles/shrc b/dotfiles/shrc index 0675bbf..2f3fec0 100644 --- a/dotfiles/shrc +++ b/dotfiles/shrc @@ -30,11 +30,17 @@ if [ -d ~/local/man ]; then alias man="/usr/bin/man -m ~/local/man" fi +PS1= +if [ -f ~/code/lucas/env/utils/shgit ]; then + . ~/code/lucas/env/utils/shgit + export PS1='$(shgit)' +fi + case $SHELL in /bin/mksh) - PS1='${HOSTNAME:=$(hostname -s)}\$ ' + PS1=$PS1'${HOSTNAME:=$(hostname -s)}\$ ' ;; -*) PS1='\h\$ ' +*) PS1=$PS1'\h\$ ' ;; esac export PS1 diff --git a/utils/shgit b/utils/shgit index c113d86..dcf0dac 100644 --- a/utils/shgit +++ b/utils/shgit @@ -1,4 +1,4 @@ -# env +# shgit # Written in 2019 by Lucas # CC0 1.0 Universal/Public domain - No rights reserved # @@ -9,20 +9,24 @@ # Dedication along with this software. If not, see # . -shgit() { - if ! gitdir=$(git rev-parse --git-dir 2>/dev/null) \ - || [ -z "$gitdir" ]; then - return - fi +shgit() +{ + : ${SHGIT_BEGIN:=} ${SHGIT_END:= } ${SHGIT_SEP:=:} + + gitdir=$(git rev-parse --git-dir 2>/dev/null) && [ -n "$gitdir" ] || + return symbolic=$(git symbolic-ref --short HEAD 2>/dev/null) - hash=$(git rev-parse --short HEAD 2>/dev/null) - name=${symbolic:-${hash:-"?"}} + name=${symbolic:-} + if [ -z "$name" ]; then + hash=$(git rev-parse --short HEAD 2>/dev/null) + name=${hash:-"?"} + fi rb= rbdir="$gitdir/rebase-merge" if [ -d "$rbdir" ]; then - rb="REBASE" + rb="${SHGIT_SEP}REBASE" read -r name <"$rbdir/head-name" name=${name##*/} [ -f "$rbdir/interactive" ] && rb="$rb-i" @@ -32,9 +36,7 @@ shgit() { fi status= - if [ -z "$(git status --porcelain)" ]; then - status="*" - fi + [ -n "$(git status --porcelain)" ] && status="${SHGIT_SEP}*" commits=$(git rev-list --left-right --count @{upstream}...HEAD \ 2>/dev/null | { @@ -42,8 +44,8 @@ shgit() { IFS=" " read -r behind ahead [ ${behind:-0} -gt 0 ] && t=$t"v$behind" [ ${ahead:-0} -gt 0 ] && t=$t"^$ahead" - printf "%s" "$t" + [ -n "$t" ] && printf "%s" "$SHGIT_SEP" "$t" }) - printf "%s" "$name${rb:+:$rb}${status:+:$status}${commits:+:$commits}" + printf "%s%s%s" "$SHGIT_BEGIN" "$name$rb$status$commits" "$SHGIT_END" }