Skip to content

Instantly share code, notes, and snippets.

@robhurring
Created October 8, 2014 23:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robhurring/661c188dc30cb069f2c7 to your computer and use it in GitHub Desktop.
Save robhurring/661c188dc30cb069f2c7 to your computer and use it in GitHub Desktop.
# set the finddir lookup PATH
export DEFAULT_SEARCH_PATHS="~/Sites/apps:~/Sites/gems:~/Projects"
# use `finddir` to fuzzy lookup directories
function cds {
local search=$1;
local found=$(noglob finddir $search)
if [ $? -eq 0 ]; then
cd $found
fi
}
#!/usr/bin/env ruby
require 'pathname'
# Search the `search_paths` for any directory that matches of your search term
# If a single match is found it is printed out, otherwise it will exit(1)
#
# Usage: finddir PATTERN [PATH, ...]
# Set this up in your env to specify where to search
# export DEFAULT_SEARCH_PATHS="~/Sites:~/Projects[:OTHER_PATHS]"
DEFAULT_SEARCH_PATHS = \
if paths = ENV['DEFAULT_SEARCH_PATHS']
paths.split(':')
else
[]
end
if ARGV.count < 1
puts "Usage: #{File.basename $0} PATTERN [PATH, ...]"
exit 1
end
matches = []
search = ARGV[0]
if ARGV.size > 1
search_paths = ARGV[1..-1]
else
search_paths = DEFAULT_SEARCH_PATHS
end
matcher = Regexp.new(search)
search_paths.each do |root|
path = File.expand_path(root)
Pathname.new(path).children.each do |child|
name = child.basename.to_s
if matcher.match(name)
matches << child
end
end
end
case matches.size
when 0
STDERR.puts "No matches found for '#{ARGV[0]}'"
exit 1
when 1
STDOUT.print matches.first
exit 0
else
STDERR.puts "Found #{matches.size} matches:"
matches.each do |match|
STDERR.puts match.to_s
end
exit 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment