[opensuse-factory] new & improved 'spwd' function for bash prompt?
I liked the spwd idea and ended up adjusting it to be a bit more precise. I just did some more testing with it to make sure it handled complicated directories without choking (though if they embed control chars, I don't filter them out... )... But rather than just looking at the number of dirs and displaying the whole path if <=3 elements or ..., like the current one does -- this one uses a progressive shortening, based on the length of the directory names. It aims to keep the total length down to 1/2 the # columns (so 40 on a standard screen)... Despite my many manipulations of the path, It works with dir paths like: dp> ll total 0 drwxrwsr-x 2 6 Jul 1 13:42 ^ difficult dir(to) "parse" ['correctly'], *{maybe}? ~; or #not/ drwxrwsr-x 2 6 Jul 1 14:35 _^ d r(2)s['rt'],*{m}; #r ~ ! \\not?/ The first one is too long to display on an 80 column screen, but it displays (weirdly) when used (it has a line-feed in the dir name): Ishtar:law/dp/^ difficult dir(to) "parse" ['correctly'], *{maybe}? ~; or #not> _ Ishtar:law/dp> cd _\^\ d\ r\(2\)s\[\'rt\'\]\,\*\{m\}\;\ #r\ ~\ \!\ \\\\not\?/ Ishtar:.../_^ d r(2)s['rt'],*{m}; #r ~ ! \not?> _ The cut-down in paths was put in an array: alias int=declare\ -i _e=echo _pf=printf exp=export ret=return exp __dpf__='local -a PF=( "/$1/$2/$3/../\${$[$#-1]}/\${$#}" "/$1/$2/../\${$[$#-1]}/\${$#}" "/$1/../\${$[$#-1]}/\${$#}" "/$1/../\${$#}" ".../\${$#}" "..." )' It tries to use a bit of the first part and some of the last part... Using more if you have dirs like "a/b/c...y/z". The function is: function spwd () { \ (($#)) || { _e "spwd called with null arg"; return 1; }; \ int w=COLUMNS/2 ;\ ( _pf -v _p "%s" "$1" ; export IFS=/ ;\ set $_p; shift; unset IFS ;\ t="${_p#${HOME%${USER}}}"; int tl=${#t} ;\ if (($#<=6 && tl<w));then ((tl<=2)) && \ { _e -En "${_p}";return 0; } ;\ else \ eval "$__dpf__" ;\ int i pfl=${#PF[*]} ;\ for ((i=0; i<pfl; ++i)); do eval \ "_pf -v _pa %s \"${PF[i]}\"" ;\ _p="$(eval "_pf %s \"$_pa\"")" ;\ ((${#_p}<w)) && break; done ;\ fi ;\ _e -En "${_p#${HOME%${USER}}}" ) } ---- I've been using some form for a few years...had a bunch of conditions in the function at one point...looked bad - so I extracted them into an array and loop through that, instead, and use the first one that passes my length test. The aliases aren't really needed if you want to sub in the text, but if you want to use it as is, you should turn on "shopt -s expand_aliases"... -l -- To unsubscribe, e-mail: opensuse-factory+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse-factory+owner@opensuse.org
participants (1)
-
Linda Walsh