--index switch #48

Closed
wants to merge 5 commits into
from

Projects

None yet

4 participants

@malexmave

I added a new Switch, --index, to index all subdirectories of the current dir. Might be useful if you have a massive folder structure you want indexed, but dont want to go through them by hand.

Known bug: Folders lower in the folder structure seem to rated higher, probably due to the way z's ranking and counting works.

Feel free to improve or reject this, I just thought this to be a useful feature.

malexmave added some commits May 10, 2012
@malexmave malexmave Added --index switch to index all subdirectories of the current dir.
Known bug: Directories lower in the directory tree will receive a higher rating, probably due to the way the _z --add command works.

Caution is advised when using this, but I think this or something similar would probably be a good feature for z, as long as you know what you are getting yourself into.
528a853
@malexmave malexmave Edited Readme and Manpage to reflect new --index switch 1be82ed
@malexmave malexmave Typo 8e0fd72
@malexmave malexmave commented on an outdated diff May 10, 2012
z.sh
@@ -29,6 +29,11 @@ _z() {
# bail out if we don't own ~/.z (we're another user but our ENV is still set)
[ -f "$datafile" -a ! -O "$datafile" ] && return
+ # Index subdirectories
+ if [ "$1" = "--index"]; then
+ find `pwd` -type d | while read line
@malexmave
malexmave May 10, 2012

There is probably a more effective way to go about this if you know the exact implementation of the --add-algorithm, I just had no time to look into that. As always, feel free to tweak it with your knowledge ;-)

@orefalo
orefalo commented May 10, 2012

+1

@malexmave

[FIXED] Ow. Please don't (blindly) merge this yet, as it still has bugs:

bash: /bin/z: line 36: syntax error near unexpected token `fi'
bash: /bin/z: line 36: ` fi'

(on startup of the console).
Sorry for lack of testing, did not think of that. You can merge and fix if you can see the error, else I will try to debug and commit corrected version tomorrow or on the weekend.

@Enlik
Contributor
Enlik commented May 12, 2012

I haven't tested it, but if it's going to be used, I suggest to put double quotes around pwd: "pwd" or, perhaps better, use "$PWD".

@malexmave

What difference would that make? Honest question, I am pretty new to Shell Code, (perhaps 2 weeks on Linux) as you might see from my code ;-)

@Enlik
Contributor
Enlik commented May 12, 2012
Congratulations on finding z besides so short time spent. :)
Hm, I took a look once again and that $line should be double quoted too.
On most UNIX-like file systems file name can contain any character besides '\0' (null) and '/'. Even some "special" characters, like a space, * and a newline are allowed, which means your snipped would break if:
a) directory structure contains a character used by the shell to split parameters, for example a space: if pwd is "/abc/x y", then `pwd` or $PWD would be two args: "/tmp/x" and "y", which is bad; "`pwd`" or "$PWD" correctly one: "/abc/x y";
b) if it's named, for example, *: * is a glob meaning "all items (excl. those with start with a dot)". Try yourself:
mkdir '/tmp/*'; cd '/tmp/*'; ls -l `pwd` (compare with ls -l "`pwd`");
c) your while read line snippet reads find output line by line which means it would not behave correctly if a directory contains a newline :) - find … -exec … should fix it (entries with newlines seem to be pruned from z's datafile anyway).

PS Sorry for stupid formatting but I'm not keen on fighting with this shiny markup to preserve what I actually wrote.

@malexmave malexmave Added Quotes to pwd, as suggested by Enlik, and used the chance to al…
…so rewrite the script to use the -exec option of find, which is probably better than my previous implementation.
cb4170d
@rupa
Owner
rupa commented Jun 23, 2012

Although this is a small change, and I appreciate the pull request, I'm not inclined to merge it. It doesn't seem like something enough people would need often enough to justify an option (and I begrudge options). In addition, indexing a directory would set each subdir to a rank of 1 - see #53 about how that would probably lead to almost all of the indexed directories being dropped from the datafile rather quickly, en masse.

I'm also don't think it works

find '`pwd`'

throws an error cause of the single quotes (but you don't need to start a subshell anyway - see below).

I think this is a pretty cool way to deal with a potential use case, but I think it's more suited as something to throw in the wiki, possibly in a for loop, like:

# add all subdirs of current directory to index, with rank 5
for x in {1..5}; do 
    find "." -type d -exec z --add \'\{\}\' \; 
done
@rupa rupa closed this Jun 23, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment