A simple script that prettifies the 'dirs -v' output and provides regex directory switching

This commit is contained in:
Derek Wyatt 2012-03-16 11:22:43 -04:00
commit f354ade247
2 changed files with 78 additions and 0 deletions

View file

@ -0,0 +1,36 @@
## Regex Matching in the Directory Stack for ZSH ##
Adds `ss` and `csd` to the list of directory manipulation functions. Zsh (or at
least the one I'm using) is mainting the directory stack for me all the time,
but I don't like how it looks when you type type `dirs -v` and I think the whole
`+n` and `-n` thing is just annoying. I also don't know why the current
directory is shown. The current directory is the current directory, so I'm not
interested in it.
So I've simplified it to look "better" (IMO) with `ss` and the `csd` function
will accept a regular expression.
Usage:
# show stack
> ss
1) ~
2) /tmp
3) /tmp/this/has a/space in it
4) /tmp/src/main/scala/package/name/here
5) /tmp/src/test/scala/package/name/here
# change to stacked directory
> csd 4
/tmp/src/main/scala/package/name/here
>
# change to stacked directory by regex
> csd test
/tmp/src/test/scala/package/name/here
>
# change to stacked directory by more interesting regex
> csd src.*main
/tmp/src/main/scala/package/name/here
>

View file

@ -0,0 +1,42 @@
function limitStringToWidthByMidpoint
{
local string="$1"
local width=$2
if (( ${#string} > $width )); then
local splitnum=$((collimit/2))
echo "$(echo $string | cut -c1-$splitnum) ... $(echo $string | cut -c$((${#string}-$splitnum))-)"
else
echo $string
fi
}
function ss
{
local c=1
local collimit=$((${COLUMNS-80}-10))
dirs -p | tail -n+2 | \
while read f
do
echo "$c) "$(limitStringToWidthByMidpoint $f $collimit)
((c=c+1))
done
}
function csd
{
local num="${1-}"
if ! echo "$num" | grep -q '^[0-9][0-9]*$'; then
local re="$num"
local num=$(dirs -p | tail -n+2 | grep -n $re | head | cut -f1 -d:)
if [ -z $num ]; then
echo "'$re' matched nothing" 1>&2
return 1
fi
elif [ $num -lt 1 ]; then
echo "usage: csd <number greater than 0 | regex>" 1>&2
return 1
fi
cd +$num
return $?
}