Skip to content

Commit 44e7403

Browse files
committed
Improves shell prompt with git and nvm info
Enhances the shell prompt to display Git branch and status information, as well as the currently active Node.js version managed by NVM. - Sources `.git-prompt-support` to provide git status in the prompt. - Modifies `gitlogp` to allow reverse chronological listing. - Uses helper functions to obtain the basename of the nvm symlink target and parent directory to support both Windows and Linux environments. - Changes git status indicators from parenthesis to curly braces.
1 parent 9ed235c commit 44e7403

File tree

4 files changed

+92
-33
lines changed

4 files changed

+92
-33
lines changed

.git-prompt-support

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/bin/env bash
12
# bash/zsh git prompt support
23
#
34
# Copyright (C) 2006,2007 Shawn O. Pearce <[email protected]>
@@ -321,11 +322,11 @@ __git_sequencer_status ()
321322
elif __git_eread "$g/sequencer/todo" todo
322323
then
323324
case "$todo" in
324-
p[\ \ ]|pick[\ \ ]*)
325+
p[[:space:]]|pick[[:space:]]*)
325326
r="|CHERRY-PICKING"
326327
return 0
327328
;;
328-
revert[\ \ ]*)
329+
revert[[:space:]]*)
329330
r="|REVERTING"
330331
return 0
331332
;;

src/bash.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ shopt -s histverify
1414
# Git completion support
1515
# shellcheck disable=SC1091
1616
source "$DEVROOT/dev-tools/.git-completion-support"
17+
# shellcheck disable=SC1091
18+
source "$DEVROOT/dev-tools/.git-prompt-support"
1719

1820
# Command Prompt
1921
# shellcheck disable=SC1091

src/functions.sh

Lines changed: 57 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,34 @@ gitlog() {
9292
' | column -ts '|'
9393
}
9494

95-
95+
# pretty one line git log of current repository with merge and side branch indicators
9696
gitlogp() {
97-
entries=$1
98-
branch="$2"
99-
[ -n "$entries" ] && entries="-${entries#-}"
97+
entries=""
98+
branch=""
99+
reverseFlag=0
100+
101+
# parse named args
102+
while [ $# -gt 0 ]; do
103+
case "$1" in
104+
-n|--entries)
105+
entries="-$2"
106+
shift 2
107+
;;
108+
-b|--branch)
109+
branch="$2"
110+
shift 2
111+
;;
112+
-r|--reverse)
113+
reverseFlag=1
114+
shift
115+
;;
116+
*)
117+
echo "Unknown option: $1"
118+
echo "Usage: gitlog [-n N] [-b branch] [-r]"
119+
return 1
120+
;;
121+
esac
122+
done
100123

101124
fmt='%h|%p|%cd|%an|%(describe:tags)|%s'
102125
datearg="--date=format:%m-%d-%y %H:%M"
@@ -105,7 +128,7 @@ gitlogp() {
105128
git log "$branch" --format="$fmt" "$datearg" $entries
106129
else
107130
git log --format="$fmt" "$datearg" $entries
108-
fi | awk -F'|' '
131+
fi | awk -F'|' -v reverseFlag=$reverseFlag '
109132
{
110133
commit=$1
111134
parents=$2
@@ -123,18 +146,12 @@ gitlogp() {
123146
124147
n=split(parents, arr, " ")
125148
parentCount[commit]=n
149+
for (i=1; i<=n; i++) if (arr[i] != "") parentOf[commit]=parentOf[commit] " " arr[i]
126150
127-
for (i=1; i<=n; i++) {
128-
if (arr[i] != "") parentOf[commit]=parentOf[commit] " " arr[i]
129-
}
130-
131-
# mark the *second parent* of merges
132-
if (n > 1) {
133-
markBranch[arr[2]]=1
134-
}
151+
if (n > 1) markBranch[arr[2]]=1
135152
}
136153
END {
137-
# propagate [side] marks backwards
154+
# propagate [side]
138155
changed=1
139156
while (changed) {
140157
changed=0
@@ -150,20 +167,33 @@ gitlogp() {
150167
}
151168
}
152169
153-
for (i=1; i<=NR; i++) {
154-
c=order[i]
155-
156-
# decide marker
157-
if (parentCount[c] > 1) {
158-
flag="[merge]"
159-
} else if (c in markBranch) {
160-
flag="[side]"
161-
} else {
162-
flag=" "
170+
# decide iteration order
171+
if (reverseFlag) {
172+
for (i=NR; i>=1; i--) {
173+
c=order[i]
174+
if (parentCount[c] > 1) {
175+
flag="[merge]"
176+
} else if (c in markBranch) {
177+
flag="[side]"
178+
} else {
179+
flag=" "
180+
}
181+
printf "%-8s %-7s | %-20s | %-14s | %-15s | %-25s | %s\n", \
182+
c, flag, parentsOf[c], dateOf[c], authorOf[c], tagOf[c], subjectOf[c]
183+
}
184+
} else {
185+
for (i=1; i<=NR; i++) {
186+
c=order[i]
187+
if (parentCount[c] > 1) {
188+
flag="[merge]"
189+
} else if (c in markBranch) {
190+
flag="[side]"
191+
} else {
192+
flag=" "
193+
}
194+
printf "%-8s %-7s | %-20s | %-14s | %-15s | %-25s | %s\n", \
195+
c, flag, parentsOf[c], dateOf[c], authorOf[c], tagOf[c], subjectOf[c]
163196
}
164-
165-
printf "%-8s %-7s %-20s %-14s %-15s %-25s %s\n", \
166-
c, flag, parentsOf[c], dateOf[c], authorOf[c], tagOf[c], subjectOf[c]
167197
}
168198
}
169199
'

src/prompt.sh

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,42 @@ Time12h="\T"
3030
PathShort="\w"
3131
Date="\d"
3232

33+
# obtain the folder name of the selected node version (from nvm symlink)
34+
get_symlink_basename() {
35+
# Check if NVM_SYMLINK is set
36+
if [ -z "$NVM_SYMLINK" ]; then
37+
echo "NVM_SYMLINK is not set."
38+
return 1
39+
fi
40+
41+
# Get the actual target path of the symlink
42+
local target
43+
target=$(readlink "$NVM_SYMLINK")
44+
45+
# Extract the basename of the folder
46+
local version="${target##*/}" # Get the last part after the last '/'
47+
48+
# Echo the extracted version
49+
echo "$version"
50+
}
51+
52+
# Helper: get parent directory name (portable)
53+
get_parent_dirname() {
54+
local dir
55+
dir="${NVM_BIN%/*}"
56+
echo "${dir##*/}"
57+
}
58+
3359
# test if NVM_SYMLINK exists
3460
if [[ -L "$NVM_SYMLINK" ]]; then
3561
# prompt for use in windows
3662
echo " -- windows prompt"
37-
export PS1=$Cyan$Time12h$Color_Off$Purple' <$(basename $(readlink "$NVM_SYMLINK"))>'$Color_Off'$(git branch &>/dev/null;\
63+
export PS1=$Cyan$Time12h$Color_Off$Purple' <$(get_symlink_basename)>'$Color_Off'$(git branch &>/dev/null;\
3864
if [ $? -eq 0 ]; then \
3965
echo "$(echo `git status` | grep "nothing to commit" > /dev/null 2>&1; \
4066
if [ "$?" -eq "0" ]; then \
4167
# Clean repository - nothing to commit
42-
echo "'$Green'"$(__git_ps1 " (%s)"); \
68+
echo "'$Green'"$(__git_ps1 " {%s}"); \
4369
else \
4470
# Changes to working tree
4571
echo "'$Red'"$(__git_ps1 " {%s}"); \
@@ -54,12 +80,12 @@ fi
5480
if [ -d "$NVM_DIR" ]; then
5581
echo " -- linux prompt"
5682
# prompt for use in linux
57-
export PS1=$Cyan$Time12h$Color_Off$Purple' <$(basename $(dirname "$NVM_BIN"))>'$Color_Off'$(git branch &>/dev/null;\
83+
export PS1=$Cyan$Time12h$Color_Off$Purple' <$(get_parent_dirname)>'$Color_Off'$(git branch &>/dev/null;\
5884
if [ $? -eq 0 ]; then \
5985
echo "$(echo `git status` | grep "nothing to commit" > /dev/null 2>&1; \
6086
if [ "$?" -eq "0" ]; then \
6187
# Clean repository - nothing to commit
62-
echo "'$Green'"$(__git_ps1 " (%s)"); \
88+
echo "'$Green'"$(__git_ps1 " {%s}"); \
6389
else \
6490
# Changes to working tree
6591
echo "'$Red'"$(__git_ps1 " {%s}"); \

0 commit comments

Comments
 (0)