zsh-users
 help / color / mirror / code / Atom feed
* Help with paths and file sorting
@ 2005-07-21 22:20 Tyler Spivey
  2005-07-22  9:16 ` J
  0 siblings, 1 reply; 3+ messages in thread
From: Tyler Spivey @ 2005-07-21 22:20 UTC (permalink / raw)
  To: zsh-users

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Help with paths and file sorting
  2005-07-21 22:20 Help with paths and file sorting Tyler Spivey
@ 2005-07-22  9:16 ` J
  2005-07-22  9:44   ` Peter Stephenson
  0 siblings, 1 reply; 3+ messages in thread
From: J @ 2005-07-22  9:16 UTC (permalink / raw)
  To: Tyler Spivey; +Cc: zsh-users

> 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/*?

What about
${${var%/*}%/*}/${var##*/} ?
(assuming the filespec is in the variable var)

This take the pattern, chops two path elements off the end, and then
concatenates again the last path element of the pattern.


> 2. is there a way of, once having an array of files obtained by the * globbing operator,
> to sort them by size, time, etc?

Sure.
Suppose you have an array "files" that contain the names of the files
you want to sort.
You can just go along the lines of
tmp=\(\(${(pj:|:)files}\)\)
files=(${~tmp}(<put here any sort qualifiers you want>))

The first line constructs a glob pattern that matches all the file
names that are in the original array ; note the double parentheses
around it are there to force glob evaluation even when the array
contains only one file.
The second lines evaluates the glob pattern the first just
constructed, so you can append any globbing qualifiers you want. The
sort qualifiers are described in the 'globbing qualifiers' of the zsh
manual.

That being said, only use this kind of construct if you strictly want
to keep exactly the same file names in your array. For a file manager,
I'd imagine that you want to keep up-to-date with the files in the
directory, so it may be a simpler and wiser choice to simply re-glob
the files with the sort qualifiers you want, and it's not going to be
any slower anyway.


But there is a caveat :
If one of the filenames have a character in it that can be interpreted
as a pattern for globbing, it will, and that's not what you want. A
solution to that is to quote the ${files} array with a (q) flag, but
then spaces in filenames (or any other special char for that) would be
quoted too in the ${tmp} array, meaning they won't match themselves
because these quotes are not going to be removed by the ${~tmp}
construct :/
So the best I can come up with is to quote every of the glob patterns
manually, like
tmp=\(\(${(pj:|:)${${${${${files//\*/\\\*}//\(/\\\(}//\|/\\\|}//\</\\\<}//\[/\\\?}}\)\)
...but that's awfully ugly. Maybe someone knows of a better solution ?

-- 
J
"If you wish to leave a record of your call,
 please state your messij at the sound of the tone."


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Help with paths and file sorting
  2005-07-22  9:16 ` J
@ 2005-07-22  9:44   ` Peter Stephenson
  0 siblings, 0 replies; 3+ messages in thread
From: Peter Stephenson @ 2005-07-22  9:44 UTC (permalink / raw)
  To: zsh-users

J wrote:
> Suppose you have an array "files" that contain the names of the files
> you want to sort.
> You can just go along the lines of
> tmp=\(\(${(pj:|:)files}\)\)
> files=(${~tmp}(<put here any sort qualifiers you want>))
>...
> But there is a caveat :
> If one of the filenames have a character in it that can be interpreted
> as a pattern for globbing, it will, and that's not what you want. A
> solution to that is to quote the ${files} array with a (q) flag, but
> then spaces in filenames (or any other special char for that) would be
> quoted too in the ${tmp} array, meaning they won't match themselves
> because these quotes are not going to be removed by the ${~tmp}
> construct :/

You can get around this by using eval instead of ~.

eval files="(("${(j.|.)${(q)files}}")(<quals>))"

That's safe because the only part you're not directly in control of,
$files, is fully quoted.  It's probably best if <quals> includes N for
nullglob so that the resulting files array is empty if nothing matches.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

**********************************************************************


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2005-07-22  9:45 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-07-21 22:20 Help with paths and file sorting Tyler Spivey
2005-07-22  9:16 ` J
2005-07-22  9:44   ` Peter Stephenson

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).