Created
February 14, 2012 11:23
-
-
Save kschiess/1825995 to your computer and use it in GitHub Desktop.
A textmate like folder search using picky. This is just a prototype.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'picky' | |
| require 'facets' | |
| require 'facets/array/combination.rb' | |
| Picky.logger = Picky::Loggers::Silent.new | |
| input = ARGV | |
| Folder = Struct.new(:id, :name) | |
| folders = Dir[ENV['HOME'] + "/**"]. | |
| each_with_index.map { |name, idx| Folder.new(idx, name) } | |
| puts folders | |
| include Picky # So we don't have to type Picky:: everywhere. | |
| class Textmate < Partial::Strategy | |
| def each_partial token, &block | |
| token.split('/').each do |word| | |
| chars = word.chars.to_a | |
| 1.upto(word.size) do |n| | |
| chars.combination(n).each { |comb| | |
| yield comb.join } | |
| end | |
| end | |
| end | |
| end | |
| folder_index = Index.new(:folders) do | |
| source folders | |
| category :name, partial: Textmate.new | |
| end | |
| folder_index.index | |
| puts | |
| folder_search = Search.new folder_index | |
| require 'pp' | |
| result = folder_search.search input.join(' ') | |
| puts result | |
| pp result.allocations |
Additional notes:
- Uses the built-in splitter.
- Uses
Partial::Infix.new(min: 1)to index down to each character. - Joins the given input with "* " so that one can search for: a b c and have Picky actually search for partials as if one had typed "a* b* c"
Hope this helps!
Another nice idiom is:
folder_index.load rescue folder_index.indexThis will load the indexed folders, if already indexed and indexes if not.
Are you planning to run a Picky server in the background, being updated in changes (via … guard? Other watchdog?) and then running a script which searches the indexes?
P.S: Just noticed that the TextMate search searches in folders from left to right, of course. Not exactly what my script does. Anyway, just some ideas … I have more, depending on what you actually want ;)
Author
Thanks for the rewrite, that saves it for me. My approach was all backward and you showed me how. Looks like I can use picky after all ;) And I can live with the left to right thing as it is for now, just need a quick simple search.
I'm glad :)
One thing you can assume with Picky is that it's relatively fast - without
wanting to sound glib. When overriding internals be sure not to allocate
too many resources, especially in code that's called many times. But you
know all that already :)
Have fun! It's a great idea and I'd love to see it go somewhere!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Love it! I rewrote it a little using the built-in Picky facilities (It seems to take a lot of time to index).