Skip to content

Commit fd48ce4

Browse files
authored
feature: Add NPM package segment
- Show NPM package name@version segment, if package.json is detected - Add optional multiline capability (linebreak before and after prompt) - Add optional short CWD display capability - Add optional padding character sequence - Change text color to black, for legibility across more terminal themes - Incorporate change from PR#3 to speed up last_status
1 parent e36aa28 commit fd48ce4

File tree

1 file changed

+80
-13
lines changed

1 file changed

+80
-13
lines changed

theme.bash

Lines changed: 80 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,21 @@
66
# Git code based on https://github.com/joeytwiddle/git-aware-prompt/blob/master/prompt.sh
77
# More info about color codes in https://en.wikipedia.org/wiki/ANSI_escape_code
88

9+
MULTILINE="YES" # set to "YES" to put add a \n before and after the prompt
10+
SHORT_USER_INFO="YES" # set to "YES" to omit the hostname
11+
SHORT_CWD="YES" # set to "YES" to show only the basename of the CWD
12+
PADDING=" " # use this character sequence to pad the segments around the separators
913

1014
PROMPT_CHAR=${POWERLINE_PROMPT_CHAR:=""}
11-
POWERLINE_LEFT_SEPARATOR=" "
12-
POWERLINE_PROMPT="last_status user_info cwd scm"
15+
POWERLINE_LEFT_SEPARATOR="${PADDING}"
16+
POWERLINE_PROMPT="last_status user_info cwd npm scm"
1317

14-
USER_INFO_SSH_CHAR=" "
15-
USER_INFO_PROMPT_COLOR="C B"
18+
USER_INFO_SSH_CHAR="${PADDING}"
19+
USER_INFO_PROMPT_COLOR="C Bl"
1620

17-
SCM_GIT_CHAR=""
21+
NPM_PROMPT_COLOR="Y Bl"
22+
23+
SCM_GIT_CHAR="${PADDING}"
1824
SCM_PROMPT_CLEAN=""
1925
SCM_PROMPT_DIRTY="*"
2026
SCM_PROMPT_AHEAD=""
@@ -27,7 +33,7 @@ SCM_PROMPT_STAGED_COLOR="Y Bl"
2733
SCM_PROMPT_UNSTAGED_COLOR="R Bl"
2834
SCM_PROMPT_COLOR=${SCM_PROMPT_CLEAN_COLOR}
2935

30-
CWD_PROMPT_COLOR="B C"
36+
CWD_PROMPT_COLOR="B Bl"
3137

3238
STATUS_PROMPT_COLOR="Bl R B"
3339
STATUS_PROMPT_ERROR=""
@@ -78,16 +84,70 @@ function __color {
7884
function __powerline_user_info_prompt {
7985
local user_info=""
8086
local color=${USER_INFO_PROMPT_COLOR}
87+
local hostname_escape="@\\h"
88+
[[ "$SHORT_USER_INFO" == "YES" ]] && hostname_escape=""
8189
if [[ -n "${SSH_CLIENT}" ]]; then
82-
user_info="${USER_INFO_SSH_CHAR}\u@\h"
90+
user_info="${USER_INFO_SSH_CHAR}\u${hostname_escape}"
8391
else
84-
user_info="\u@\h"
92+
user_info="\u${hostname_escape}"
8593
fi
86-
[[ -n "${user_info}" ]] && echo "${user_info}|${color}"
94+
[[ -n "${user_info}" ]] && echo "${PADDING}${user_info}${PADDING}|${color}"
8795
}
8896

8997
function __powerline_cwd_prompt {
90-
echo "\w|${CWD_PROMPT_COLOR}"
98+
local cwd_escape="\\w"
99+
[[ "$SHORT_CWD" == "YES" ]] && cwd_escape="\\W"
100+
echo "${cwd_escape}${PADDING}|${CWD_PROMPT_COLOR}"
101+
}
102+
103+
function __powerline_npm_prompt {
104+
npm_package_file_name="package.json"
105+
npm_package_file=""
106+
git_top_level=""
107+
npm_name=""
108+
npm_version=""
109+
110+
find_git_top_level() {
111+
git_top_level=$(git rev-parse --show-toplevel 2> /dev/null)
112+
return 0
113+
}
114+
115+
find_npm_package_file() {
116+
npm_package_file="$npm_package_file_name"
117+
if [ -n "$git_top_level" ]; then
118+
git_top_level=$(sed 's/\([A-Za-z]\):/\/\L\1/' <<< "${git_top_level}")
119+
npm_package_file="${git_top_level}/${npm_package_file}"
120+
fi
121+
if [ ! -f "$npm_package_file" ]; then
122+
npm_package_file=""
123+
return 1
124+
fi
125+
}
126+
127+
find_npm_name() {
128+
if [[ -n "$npm_package_file" ]]; then
129+
npm_name=$(awk -F '"' '/name/ {print $4}' $npm_package_file)
130+
fi
131+
}
132+
133+
find_npm_version() {
134+
if [[ -n "$npm_package_file" ]]; then
135+
npm_version=$(awk -F '"' '/version/ {print $4}' $npm_package_file)
136+
fi
137+
}
138+
139+
local color
140+
local npm_info
141+
142+
find_git_top_level && find_npm_package_file && find_npm_name && find_npm_version
143+
144+
# not in NPM package
145+
[[ -z "$npm_package_file" ]] && return
146+
147+
npm_info="${npm_name}@${npm_version}"
148+
color=${NPM_PROMPT_COLOR}
149+
150+
[[ "$npm_info" != "@" ]] && echo "${npm_info}${PADDING}|${color}"
91151
}
92152

93153
function __powerline_scm_prompt {
@@ -168,7 +228,7 @@ function __powerline_scm_prompt {
168228
[[ -n "$git_behind" ]] && scm_info+="${SCM_PROMPT_BEHIND}${git_behind_count}"
169229
[[ -n "$git_ahead" ]] && scm_info+="${SCM_PROMPT_AHEAD}${git_ahead_count}"
170230

171-
[[ -n "${scm_info}" ]] && echo "${scm_info}|${color}"
231+
[[ -n "${scm_info}" ]] && echo "${scm_info}${PADDING}|${color}"
172232
}
173233

174234
function __powerline_left_segment {
@@ -195,9 +255,11 @@ function __powerline_left_segment {
195255

196256
function __powerline_last_status_prompt {
197257
local symbols=()
258+
local stopped_jobs
259+
read -N1 stopped_jobs < <(jobs -sp)
198260
[[ $last_status -ne 0 ]] && symbols+="$(__color ${STATUS_PROMPT_ERROR_COLOR})${STATUS_PROMPT_ERROR}"
199261
[[ $UID -eq 0 ]] && symbols+="$(__color ${STATUS_PROMPT_ROOT_COLOR})${STATUS_PROMPT_ROOT}"
200-
[[ $(jobs -l | wc -l) -gt 0 ]] && symbols+="$(__color ${STATUS_PROMPT_JOBS_COLOR})${STATUS_PROMPT_JOBS}"
262+
[[ ! -z "$stopped_jobs" ]] && symbols+="$(__color ${STATUS_PROMPT_JOBS_COLOR})${STATUS_PROMPT_JOBS}"
201263

202264
[[ -n "$symbols" ]] && echo "$symbols|${STATUS_PROMPT_COLOR}"
203265
}
@@ -206,6 +268,8 @@ function __powerline_prompt_command {
206268
local last_status="$?" ## always the first
207269
local separator_char="${POWERLINE_PROMPT_CHAR}"
208270

271+
[[ "$MULTILINE" == "YES" ]] && separator_char="${POWERLINE_LEFT_SEPARATOR}"
272+
209273
LEFT_PROMPT=""
210274
SEGMENTS_AT_LEFT=0
211275
LAST_SEGMENT_COLOR=""
@@ -217,7 +281,10 @@ function __powerline_prompt_command {
217281
done
218282

219283
[[ -n "${LEFT_PROMPT}" ]] && LEFT_PROMPT+="$(__color - ${LAST_SEGMENT_COLOR})${separator_char}$(__color)"
220-
PS1="${LEFT_PROMPT} "
284+
285+
[[ "$MULTILINE" == "YES" ]] \
286+
&& PS1="\n${LEFT_PROMPT}\n\$" \
287+
|| PS1="${LEFT_PROMPT} "
221288

222289
## cleanup ##
223290
unset LAST_SEGMENT_COLOR \

0 commit comments

Comments
 (0)