Skip to content

Commit a03617c

Browse files
committed
feat: Add smart path shortening to terminal prompt
1 parent 83264c4 commit a03617c

1 file changed

Lines changed: 27 additions & 2 deletions

File tree

src/plugins/terminal/scripts/init-alpine.sh

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,31 @@ export TERM=xterm-256color
9292
SHELL=/bin/bash
9393
export PIP_BREAK_SYSTEM_PACKAGES=1
9494
95+
# Smart path shortening function (fish-style: ~/p/s/components)
96+
_shorten_path() {
97+
local path="${PWD/#$HOME/\~}"
98+
[[ "$path" == "~" ]] && echo "~" && return
99+
100+
local parts result=""
101+
IFS='/' read -ra parts <<< "$path"
102+
local len=${#parts[@]}
103+
104+
for ((i=0; i<len; i++)); do
105+
[[ -z "${parts[i]}" ]] && continue
106+
if [[ $i -lt $((len-1)) ]]; then
107+
result+="${parts[i]:0:1}/"
108+
else
109+
result+="${parts[i]}"
110+
fi
111+
done
112+
113+
[[ "$path" == /* ]] && echo "/$result" || echo "$result"
114+
}
115+
116+
# Update prompt vars before each command
117+
PROMPT_COMMAND='_PS1_PATH=$(_shorten_path); _PS1_EXIT=$?'
118+
119+
95120
# Display MOTD if available
96121
if [ -s /etc/acode_motd ]; then
97122
cat /etc/acode_motd
@@ -120,12 +145,12 @@ fi
120145

121146
# Add PS1 only if not already present
122147
if ! grep -q 'PS1=' "$PREFIX/alpine/initrc"; then
123-
echo 'PS1="\[\033[1;32m\]\u\[\033[0m\]@localhost \w \$ "' >> "$PREFIX/alpine/initrc"
148+
echo 'PS1="\[\033[1;32m\]\u\[\033[0m\]@localhost \[\033[1;34m\]\$_PS1_PATH\[\033[0m\] \[\$([ \$_PS1_EXIT -ne 0 ] && echo \"\033[31m\")\]\$\[\033[0m\] "' >> "$PREFIX/alpine/initrc"
124149
fi
125150

126151
chmod +x "$PREFIX/alpine/initrc"
127152

128-
#actual souce
153+
#actual source
129154
#everytime a terminal is started initrc will run
130155
"$PREFIX/axs" -c "bash --rcfile /initrc -i"
131156

0 commit comments

Comments
 (0)