Skip to content

Commit 9fe1ec8

Browse files
authored
feat: Add smart path shortening to terminal prompt (#1767)
1 parent ed4c753 commit 9fe1ec8

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

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

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,39 @@ export TERM=xterm-256color
161161
SHELL=/bin/bash
162162
export PIP_BREAK_SYSTEM_PACKAGES=1
163163
164+
# Smart path shortening function (fish-style: ~/p/s/components)
165+
_shorten_path() {
166+
local path="$PWD"
167+
168+
if [[ "$HOME" != "/" && "$path" == "$HOME" ]]; then
169+
echo "~"
170+
return
171+
elif [[ "$HOME" != "/" && "$path" == "$HOME/"* ]]; then
172+
path="~${path#$HOME}"
173+
fi
174+
175+
[[ "$path" == "~" ]] && echo "~" && return
176+
177+
local parts result=""
178+
IFS='/' read -ra parts <<< "$path"
179+
local len=${#parts[@]}
180+
181+
for ((i=0; i<len; i++)); do
182+
[[ -z "${parts[i]}" ]] && continue
183+
if [[ $i -lt $((len-1)) ]]; then
184+
result+="${parts[i]:0:1}/"
185+
else
186+
result+="${parts[i]}"
187+
fi
188+
done
189+
190+
[[ "$path" == /* ]] && echo "/$result" || echo "$result"
191+
}
192+
193+
# Update prompt vars before each command
194+
PROMPT_COMMAND='_PS1_PATH=$(_shorten_path); _PS1_EXIT=$?'
195+
196+
164197
# Display MOTD if available
165198
if [ -s /etc/acode_motd ]; then
166199
cat /etc/acode_motd
@@ -189,12 +222,15 @@ fi
189222

190223
# Add PS1 only if not already present
191224
if ! grep -q 'PS1=' "$PREFIX/alpine/initrc"; then
192-
echo 'PS1="\[\033[1;32m\]\u\[\033[0m\]@localhost \w \$ "' >> "$PREFIX/alpine/initrc"
225+
# Smart path shortening (fish-style: ~/p/s/components)
226+
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"
227+
# Simple prompt (uncomment below and comment above if you prefer full paths)
228+
# echo 'PS1="\[\033[1;32m\]\u\[\033[0m\]@localhost \[\033[1;34m\]\w\[\033[0m\] \$ "' >> "$PREFIX/alpine/initrc"
193229
fi
194230

195231
chmod +x "$PREFIX/alpine/initrc"
196232

197-
#actual souce
233+
#actual source
198234
#everytime a terminal is started initrc will run
199235
"$PREFIX/axs" -c "bash --rcfile /initrc -i"
200236

0 commit comments

Comments
 (0)