|
8 | 8 | // EFFECT: n/a |
9 | 9 | // INVARIANT: script is deterministic |
10 | 10 | // COMPLEXITY: O(1) |
11 | | -export const renderPromptScript = (): string => |
12 | | - `docker_git_branch() { git rev-parse --abbrev-ref HEAD 2>/dev/null; } |
| 11 | +const dockerGitPromptScript = `docker_git_branch() { git rev-parse --abbrev-ref HEAD 2>/dev/null; } |
| 12 | +docker_git_short_pwd() { |
| 13 | + local full_path |
| 14 | + full_path="\${PWD:-}" |
| 15 | + if [[ -z "$full_path" ]]; then |
| 16 | + printf "%s" "?" |
| 17 | + return |
| 18 | + fi |
| 19 | +
|
| 20 | + local display="$full_path" |
| 21 | + if [[ -n "\${HOME:-}" && "$full_path" == "$HOME" ]]; then |
| 22 | + display="~" |
| 23 | + elif [[ -n "\${HOME:-}" && "$full_path" == "$HOME/"* ]]; then |
| 24 | + display="~/\${full_path#$HOME/}" |
| 25 | + fi |
| 26 | +
|
| 27 | + if [[ "$display" == "~" || "$display" == "/" ]]; then |
| 28 | + printf "%s" "$display" |
| 29 | + return |
| 30 | + fi |
| 31 | +
|
| 32 | + local prefix="" |
| 33 | + local body="$display" |
| 34 | + if [[ "$body" == "~/"* ]]; then |
| 35 | + prefix="~/" |
| 36 | + body="\${body#~/}" |
| 37 | + elif [[ "$body" == /* ]]; then |
| 38 | + prefix="/" |
| 39 | + body="\${body#/}" |
| 40 | + fi |
| 41 | +
|
| 42 | + local result="$prefix" |
| 43 | + local segment="" |
| 44 | + local rest="$body" |
| 45 | + while [[ "$rest" == */* ]]; do |
| 46 | + segment="\${rest%%/*}" |
| 47 | + rest="\${rest#*/}" |
| 48 | + if [[ -n "$segment" ]]; then |
| 49 | + result+="\${segment:0:1}/" |
| 50 | + fi |
| 51 | + done |
| 52 | +
|
| 53 | + if [[ -n "$rest" ]]; then |
| 54 | + result+="$rest" |
| 55 | + elif [[ "$result" == "~/" ]]; then |
| 56 | + result="~" |
| 57 | + elif [[ -z "$result" ]]; then |
| 58 | + result="/" |
| 59 | + fi |
| 60 | +
|
| 61 | + printf "%s" "$result" |
| 62 | +} |
13 | 63 | docker_git_prompt_apply() { |
14 | 64 | local b |
15 | 65 | b="$(docker_git_branch)" |
16 | | - local base="[\\t] \\w" |
| 66 | + local short_pwd |
| 67 | + short_pwd="$(docker_git_short_pwd)" |
| 68 | + local base="[\\t] $short_pwd" |
17 | 69 | if [ -n "$b" ]; then |
18 | 70 | PS1="\${base} (\${b})> " |
19 | 71 | else |
|
26 | 78 | PROMPT_COMMAND="docker_git_prompt_apply" |
27 | 79 | fi` |
28 | 80 |
|
| 81 | +export const renderPromptScript = (): string => dockerGitPromptScript |
| 82 | + |
29 | 83 | // CHANGE: enable bash completion for interactive shells |
30 | 84 | // WHY: allow tab completion for CLI tools in SSH terminals |
31 | 85 | // QUOTE(ТЗ): "А почему у меня не работает автодополенние в терминале?" |
@@ -124,10 +178,68 @@ zstyle ':completion:*' tag-order builtins commands aliases reserved-words functi |
124 | 178 |
|
125 | 179 | autoload -Uz add-zsh-hook |
126 | 180 | docker_git_branch() { git rev-parse --abbrev-ref HEAD 2>/dev/null; } |
| 181 | +docker_git_short_pwd() { |
| 182 | + local full_path="\${PWD:-}" |
| 183 | + if [[ -z "$full_path" ]]; then |
| 184 | + print -r -- "?" |
| 185 | + return |
| 186 | + fi |
| 187 | +
|
| 188 | + local display="$full_path" |
| 189 | + if [[ -n "\${HOME:-}" && "$full_path" == "$HOME" ]]; then |
| 190 | + display="~" |
| 191 | + elif [[ -n "\${HOME:-}" && "$full_path" == "$HOME/"* ]]; then |
| 192 | + display="~/\${full_path#$HOME/}" |
| 193 | + fi |
| 194 | +
|
| 195 | + if [[ "$display" == "~" || "$display" == "/" ]]; then |
| 196 | + print -r -- "$display" |
| 197 | + return |
| 198 | + fi |
| 199 | +
|
| 200 | + local prefix="" |
| 201 | + local body="$display" |
| 202 | + if [[ "$body" == "~/"* ]]; then |
| 203 | + prefix="~/" |
| 204 | + body="\${body#~/}" |
| 205 | + elif [[ "$body" == /* ]]; then |
| 206 | + prefix="/" |
| 207 | + body="\${body#/}" |
| 208 | + fi |
| 209 | +
|
| 210 | + local -a parts |
| 211 | + local result="$prefix" |
| 212 | + parts=(\${(s:/:)body}) |
| 213 | + local total=\${#parts[@]} |
| 214 | + local idx=1 |
| 215 | + local part="" |
| 216 | + for part in "\${parts[@]}"; do |
| 217 | + if [[ -z "$part" ]]; then |
| 218 | + ((idx++)) |
| 219 | + continue |
| 220 | + fi |
| 221 | + if (( idx < total )); then |
| 222 | + result+="\${part[1,1]}/" |
| 223 | + else |
| 224 | + result+="$part" |
| 225 | + fi |
| 226 | + ((idx++)) |
| 227 | + done |
| 228 | +
|
| 229 | + if [[ -z "$result" ]]; then |
| 230 | + result="/" |
| 231 | + elif [[ "$result" == "~/" ]]; then |
| 232 | + result="~" |
| 233 | + fi |
| 234 | +
|
| 235 | + print -r -- "$result" |
| 236 | +} |
127 | 237 | docker_git_prompt_apply() { |
128 | 238 | local b |
129 | 239 | b="$(docker_git_branch)" |
130 | | - local base="[%*] %~" |
| 240 | + local short_pwd |
| 241 | + short_pwd="$(docker_git_short_pwd)" |
| 242 | + local base="[%*] $short_pwd" |
131 | 243 | if [[ -n "$b" ]]; then |
132 | 244 | PROMPT="$base ($b)> " |
133 | 245 | else |
|
0 commit comments