Skip to content

Commit 7bbcfe8

Browse files
implemented diesire#8
1 parent e50c3a9 commit 7bbcfe8

File tree

1 file changed

+75
-11
lines changed

1 file changed

+75
-11
lines changed

theme.bash

Lines changed: 75 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,25 @@
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+
readonly MULTILINE="YES" # set to "YES" to put add a \n before and after the prompt
10+
readonly SHORT_USER_INFO="YES" # set to "YES" to omit the hostname
11+
readonly SHORT_CWD="YES" # set to "YES" to show only the basename of the CWD
12+
readonly PADDING=" " # use this character sequence to pad the segments around the separators
913

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

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

1721
readonly VENV_PROMPT_COLOR="M Y"
1822
readonly PYTHON_SYMBOL=''
1923

2024
readonly SCM_GIT_CHAR=""
25+
readonly NPM_PROMPT_COLOR="Y Bl"
26+
27+
readonly SCM_GIT_CHAR="${PADDING}"
2128
readonly SCM_PROMPT_CLEAN=""
2229
readonly SCM_PROMPT_DIRTY="*"
2330
readonly SCM_PROMPT_AHEAD=""
@@ -31,7 +38,7 @@ readonly SCM_PROMPT_UNSTAGED_COLOR="R Bl"
3138
readonly SCM_PROMPT_COLOR=${SCM_PROMPT_CLEAN_COLOR}
3239

3340
readonly CWD_PROMPT_COLOR="B C"
34-
41+
readonly CWD_PROMPT_COLOR="B Bl"
3542
readonly STATUS_PROMPT_COLOR="Bl R B"
3643
readonly STATUS_PROMPT_ERROR=""
3744
readonly STATUS_PROMPT_ERROR_COLOR="Bl R B"
@@ -81,16 +88,70 @@ function __color {
8188
function __powerline_user_info_prompt {
8289
local user_info=""
8390
local color=${USER_INFO_PROMPT_COLOR}
91+
local hostname_escape="@\\h"
92+
[[ "$SHORT_USER_INFO" == "YES" ]] && hostname_escape=""
8493
if [[ -n "${SSH_CLIENT}" ]]; then
85-
user_info="${USER_INFO_SSH_CHAR}\u@\h"
94+
user_info="${USER_INFO_SSH_CHAR}\u${hostname_escape}"
8695
else
87-
user_info="\u@\h"
96+
user_info="\u${hostname_escape}"
8897
fi
89-
[[ -n "${user_info}" ]] && echo "${user_info}|${color}"
98+
[[ -n "${user_info}" ]] && echo "${PADDING}${user_info}${PADDING}|${color}"
9099
}
91100

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

96157
function __powerline_venv_prompt {
@@ -177,7 +238,7 @@ function __powerline_scm_prompt {
177238
[[ -n "$git_behind" ]] && scm_info+="${SCM_PROMPT_BEHIND}${git_behind_count}"
178239
[[ -n "$git_ahead" ]] && scm_info+="${SCM_PROMPT_AHEAD}${git_ahead_count}"
179240

180-
[[ -n "${scm_info}" ]] && echo "${scm_info}|${color}"
241+
[[ -n "${scm_info}" ]] && echo "${scm_info}${PADDING}|${color}"
181242
}
182243

183244
function __powerline_left_segment {
@@ -216,6 +277,7 @@ function __powerline_last_status_prompt {
216277
function __powerline_prompt_command {
217278
local last_status="$?" ## always the first
218279
local separator_char="${POWERLINE_PROMPT_CHAR}"
280+
[[ "$MULTILINE" == "YES" ]] && separator_char="${POWERLINE_LEFT_SEPARATOR}"
219281

220282
LEFT_PROMPT=""
221283
SEGMENTS_AT_LEFT=0
@@ -228,7 +290,9 @@ function __powerline_prompt_command {
228290
done
229291

230292
[[ -n "${LEFT_PROMPT}" ]] && LEFT_PROMPT+="$(__color - ${LAST_SEGMENT_COLOR})${separator_char}$(__color)"
231-
PS1="${LEFT_PROMPT} "
293+
[[ "$MULTILINE" == "YES" ]] \
294+
&& PS1="\n${LEFT_PROMPT}\n\$" \
295+
|| PS1="${LEFT_PROMPT} "
232296

233297
## cleanup ##
234298
unset LAST_SEGMENT_COLOR \

0 commit comments

Comments
 (0)