From 0281d9a63375463187b2ab9b72adc33878f5e345 Mon Sep 17 00:00:00 2001 From: Adam Sanderson Date: Fri, 19 Nov 2010 15:01:28 -0800 Subject: [PATCH] Pattern matching for library names --- TODO | 5 +++-- bin/qw | 9 +++++---- lib/qwandry/flat_repository.rb | 4 ++-- lib/qwandry/launcher.rb | 7 +++++-- lib/qwandry/library_repository.rb | 4 ++-- 5 files changed, 17 insertions(+), 12 deletions(-) diff --git a/TODO b/TODO index 6a241f1..88232e1 100644 --- a/TODO +++ b/TODO @@ -1,6 +1,7 @@ +TODO, or perhaps not... + - Differentiate multiple similar matches (show path or repository label) - Allow better customization of repositories - - Filter by pattern, ie: `accept '*.rb'` or `reject '*.o'` @done ... but code be better - Customize collection phase (see LibraryRepository) - Customize naming phase -- Match multiword args, ie: activerec 3. => activerec*3.* => activerecord-3.0.3 +- Match multiword args, ie: 'activerec 3.' => 'activerec*3.*' => 'activerecord-3.0.3' @done \ No newline at end of file diff --git a/bin/qw b/bin/qw index d341e60..99c961a 100755 --- a/bin/qw +++ b/bin/qw @@ -55,7 +55,7 @@ opts = OptionParser.new do |opts| end opts.parse! ARGV -if ARGV.length != 1 +if ARGV.length == 0 puts opts exit(1) end @@ -63,8 +63,9 @@ end # Configure default values @qwandry.editor = @editor if @editor -name = ARGV.pop -packages = @qwandry.find(name) +name = ARGV.join(' ') +packages = @qwandry.find(*ARGV) +ARGV.clear # for the gets below package = nil case packages.length @@ -79,7 +80,7 @@ else end print ">> " - index = gets.to_i-1 + index = gets.to_i - 1 package = packages[index] end diff --git a/lib/qwandry/flat_repository.rb b/lib/qwandry/flat_repository.rb index 1371ba6..4039a05 100644 --- a/lib/qwandry/flat_repository.rb +++ b/lib/qwandry/flat_repository.rb @@ -3,10 +3,10 @@ module Qwandry # ./lib-0.1 # ./lib-0.2 class FlatRepository < Qwandry::Repository - def scan(name) + def scan(pattern) results = [] all_paths.select do |path| - if File.basename(path).start_with?(name) + if File.fnmatch?(pattern, File.basename(path)) results << package(File.basename(path), [path]) end end diff --git a/lib/qwandry/launcher.rb b/lib/qwandry/launcher.rb index f69bf63..a06ef9f 100644 --- a/lib/qwandry/launcher.rb +++ b/lib/qwandry/launcher.rb @@ -46,11 +46,14 @@ module Qwandry end # Searches all of the loaded repositories for `name` - def find(name) + def find(*pattern) + pattern = pattern.join('*') + pattern << '*' unless pattern =~ /\*$/ + packages = [] @repositories.select{|label,_| @active.include? label }.each do |label, repos| repos.each do |repo| - packages.concat(repo.scan(name)) + packages.concat(repo.scan(pattern)) end end packages diff --git a/lib/qwandry/library_repository.rb b/lib/qwandry/library_repository.rb index 87b17ed..a736502 100644 --- a/lib/qwandry/library_repository.rb +++ b/lib/qwandry/library_repository.rb @@ -5,11 +5,11 @@ module Qwandry # lib2.py # lib2/... class LibraryRepository < Qwandry::Repository - def scan(name) + def scan(pattern) results = Hash.new{|h,k| h[k] = package(k)} all_paths.select do |path| basename = File.basename(path) - if basename.start_with?(name) + if File.fnmatch?(pattern, basename) # strip any file extension basename.sub! /\.\w+$/,'' unless File.directory?(path) results[basename].paths << path