Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Help with paths and file sorting



1. is there an easy way to take a filespec like:
/home/tyler/dir1/*
and go back a directory? so the spec is /home/tyler/*?
2. is there a way of, once having an array of files obtained by the * globbing operator,
to sort them by size, time, etc? I'm asking this because I'm writing a file manager - here it is below, since in its infancy it's quite small.
I'm hoping that I won't have to move to another language like python - even though I like python - but shell seems more appropriate.
#!/bin/zsh
zmodload zsh/stat
#this is a zsh file manager.
#variables.
typeset -A keys
keys=(
e editspec
g go
p previous
n next
q exit
s size
" " printfile
)
function editspec {
vared -p "filespec: " fs
readfiles
cur=1
printfile
}
## grab a key, and send it off to the handler. for the main loop.
function grabkey {
read -sk1 key
handle $key
}
## main key handler. decide just what to do with this key.
function handle {
if [[ -z $keys[$1] ]]; then
echo "key undefined."
return
fi
$keys[$1]
}
##print current file.
function printfile { echo ${files[$cur]:t} }
## next/previous file.
function next {
len=${#files}
# handle empty dir.
if [[ $len -lt 1 ]];then
echo "empty dir."
return
fi
cur+=1
if [[ $cur -gt $len ]];then
echo "end"
cur=$len
fi
printfile
}
function previous {
len=${#files}
# handle empty dir.
if [[ $len -lt 1 ]];then
echo "empty dir."
return
fi
cur+=-1
if [[ $cur -lt 1 ]];then
echo "beginning"
cur=1
fi
printfile
}
function size {
echo `stat +size $files[$cur]`
}
function getspec {
echo "filespec:"
read fs
if [[ -z $fs ]];then
fs=\*
fi
}
function readfiles {
echo -n "reading files..."
files=($~fs)
echo "done, ${#files} files."
}
## refresh the file list.
function go {
pth=${fs:h}/${files[$cur]:t}
if [[ -d $pth ]];then
fs=$pth/*
echo $fs
readfiles
cur=1
printfile
return
fi
echo "not a dir"
}
getspec
readfiles
typeset -i cur=1
printfile
while :;do
grabkey
done



Messages sorted by: Reverse Date, Date, Thread, Author