Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--index switch #48

Closed
wants to merge 5 commits into from
Closed

--index switch #48

wants to merge 5 commits into from

Conversation

malexmave
Copy link

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 3 commits May 10, 2012 18:02
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.
@@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link

orefalo commented May 10, 2012

+1

@malexmave
Copy link
Author

[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
Copy link
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
Copy link
Author

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
Copy link
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.

…so rewrite the script to use the -exec option of find, which is probably better than my previous implementation.
@rupa
Copy link
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants