zsh-users
 help / color / mirror / code / Atom feed
From: Tyler Spivey <tspivey@pcdesk.net>
To: zsh-users@sunsite.dk
Subject: Help with paths and file sorting
Date: Thu, 21 Jul 2005 15:20:24 -0700	[thread overview]
Message-ID: <20050721222024.GA14060@fast.vc.shawcable.net> (raw)

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


             reply	other threads:[~2005-07-21 22:20 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-07-21 22:20 Tyler Spivey [this message]
2005-07-22  9:16 ` J
2005-07-22  9:44   ` Peter Stephenson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20050721222024.GA14060@fast.vc.shawcable.net \
    --to=tspivey@pcdesk.net \
    --cc=zsh-users@sunsite.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).