Browse Source

Add 'shrinky' mode for path in window title

Alois Mahdal 6 years ago
parent
commit
c8724b2cd7
1 changed files with 70 additions and 1 deletions
  1. 70
    1
      dotfiles/bash/post.bashrc

+ 70
- 1
dotfiles/bash/post.bashrc View File

@@ -100,6 +100,7 @@ __bashum__wdir() {
100 100
     #
101 101
     local mode=${1:-normal}
102 102
     case $mode:$PWD in
103
+        shrinky:*)              __bashum__shrinkypath "$PWD" ;;
103 104
         normal:*)               echo "${PWD/$HOME/\~}" ;;
104 105
         short:$HOME)            echo "~" ;;
105 106
         short:$HOME/??????????) echo "${PWD/$HOME/\~}" ;;
@@ -109,6 +110,74 @@ __bashum__wdir() {
109 110
     esac
110 111
 }
111 112
 
113
+__bashum__shrinkypath() {
114
+    #
115
+    # Convert path $1 to small path
116
+    #
117
+    local path=$1
118
+    case $path in
119
+        $HOME)      echo "~"; return ;;
120
+        $HOME/*)    echo -n "~"; __bashum__shrink_relpath "${path#$HOME/}" ;;
121
+        *)          echo "$path"; return ;;
122
+    esac
123
+}
124
+
125
+__bashum__shrink_relpath() {
126
+    #
127
+    # Shrink relative path according to rules
128
+    #
129
+    # Rules are:
130
+    #
131
+    #  * if path has 7 chars or less, it's left as is and printed;
132
+    #  * else if there is only one element, it's left as is, and path is printed'
133
+    #  * else if there are three or more elements, elements
134
+    #    between first and last are replaced by '...',
135
+    #  * if the first element has 3 chars or less, it's left as is
136
+    #    and path is printed,
137
+    #  * else, the first element is
138
+    #      * broken into words by period,
139
+    #      * and each word, if longer than 2 characters,
140
+    #        is shortened to its first character followed by '...'
141
+    #      * and the shortened elements are joined back by period,
142
+    #  * finally the path is printed (first element shortened. '...', and
143
+    #    last element intact)
144
+    #
145
+    local path=$1
146
+    local elems
147
+    local frst
148
+    local last
149
+    local elnum
150
+    test "${#path}" -le 7 && echo -n "$path" && return 0
151
+    elems=(${path//// }); elnum=${#elems[@]}
152
+    frst=${elems[0]}
153
+    last=${elems[$((elnum-1))]}
154
+    case $elnum in
155
+        0) true ;;      # hardly possible (caught by -le 7 above)
156
+        1) echo -n "$path"; return ;;
157
+        2) __bashum__shrink_elem "$frst"; echo -n "/$last" ;;
158
+        *) __bashum__shrink_elem "$frst"; echo -n "/…/$last" ;;
159
+    esac
160
+}
161
+
162
+__bashum__shrink_elem() {
163
+    #
164
+    # Shrink element by words
165
+    #
166
+    local buff=$1
167
+    local word
168
+    local words
169
+    local head=true
170
+    words=(${buff//./ }); wnum=${#words[@]}
171
+    test "$wnum" -eq 0 && echo -n "$buff" && return 0
172
+    for word in "${words[@]}"; do
173
+        $head || { echo -n .; head=false; }
174
+        case $word in
175
+            ?|??)   echo -n "$word" ;;
176
+            *)      echo -n "${word:0:1}…" ;;
177
+        esac
178
+    done
179
+}
180
+
112 181
 #shellcheck disable=SC2016
113 182
 __bashum__mkpc() {
114 183
     #
@@ -121,7 +190,7 @@ __bashum__mkpc() {
121 190
             echo -n   "$(__bashum__mkicon) "
122 191
             echo -n   '$(__bashum__lastrv)'
123 192
             echo -n   "$(__bashum__mkhostid)"
124
-            echo -n   '$(__bashum__wdir short)'
193
+            echo -n   '$(__bashum__wdir shrinky)'
125 194
             echo -n   '\$'
126 195
             echo -n '\007"'
127 196
             ;;