-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from renderedtext/rexich/findutil-refactoring
Massive changes to FindUtil
- Loading branch information
Showing
6 changed files
with
130 additions
and
211 deletions.
There are no files selected for viewing
Binary file not shown.
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,36 @@ | ||
#!/usr/bin/env ruby | ||
|
||
# File search methods using shell globbing or regular expressions | ||
# File search methods using shell globbing, regular expressions | ||
|
||
module FindUtility | ||
class Finder | ||
|
||
def initialize(opts) | ||
@opts = opts | ||
class FindRegexp | ||
def initialize(path, pattern, case_sensitive, recursive) | ||
@path, @pattern = path, pattern | ||
@flags = ! case_sensitive ? | ||
(Regexp::IGNORECASE | File::FNM_DOTMATCH) : File::FNM_DOTMATCH | ||
@glob = recursive ? '**/*' : '' | ||
end | ||
|
||
def find(path, pattern) | ||
@opts[:regex] == true ? find_regex(path, pattern) : find_name(path, pattern) | ||
def find | ||
Dir.glob(File.join(@path, @glob), @flags) | ||
.grep(Regexp.new(@pattern)) | ||
.sort | ||
.each { |x| puts x } | ||
end | ||
end | ||
|
||
def find_regex(path, pattern) | ||
# If recursive, glob directories using **/* to get all files | ||
# and then perform grep on the array | ||
asterisks = '' | ||
asterisks = '**/*' if @opts[:recursive] == true | ||
|
||
if @opts[:case_sensitive] == true | ||
files = Dir.glob(File.join(path, asterisks)).grep(Regexp.new(pattern)).sort.each { |x| puts x } | ||
else | ||
files = Dir.glob(File.join(path, asterisks)).grep(Regexp.new(pattern, Regexp::IGNORECASE)).sort.each { |x| puts x } | ||
end | ||
class FindName | ||
def initialize(path, name, case_sensitive, recursive) | ||
@path, @name = path, name | ||
@flags = ! case_sensitive ? | ||
(Regexp::IGNORECASE | File::FNM_DOTMATCH) : File::FNM_DOTMATCH | ||
@glob = recursive ? '**' : '' | ||
end | ||
|
||
def find_name(path, pattern) | ||
# If recursive, glob directories using ** | ||
asterisks = '' | ||
asterisks = '**' if @opts[:recursive] == true | ||
|
||
if @opts[:case_sensitive] == true | ||
files = Dir.glob(File.join(path, asterisks, pattern)).each { |x| puts x } | ||
else | ||
files = Dir.glob(File.join(path, asterisks, pattern), File::FNM_CASEFOLD).each { |x| puts x } | ||
end | ||
def find | ||
Dir.glob(File.join(@path, @glob, @name), @flags) | ||
.sort | ||
.each { |x| puts x } | ||
end | ||
|
||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#!/usr/bin/env ruby | ||
|
||
# Parsing of command line switches | ||
|
||
module FindUtility | ||
class Options | ||
|
||
def initialize(args) | ||
@args = args | ||
end | ||
|
||
def no_options? | ||
@args.length == 0 | ||
end | ||
|
||
def show_usage? | ||
has_options?(["--help", "-h"]) | ||
end | ||
|
||
def path_defined? | ||
File.directory?(@args[0]) | ||
end | ||
|
||
def path | ||
@args[0] | ||
end | ||
|
||
def search_pattern | ||
@args[1] | ||
end | ||
|
||
def recursive? | ||
has_options?(["-r", "-R", "--recursive"]) | ||
end | ||
|
||
def case_sensitive? | ||
has_options?(["-c", "--case"]) | ||
end | ||
|
||
def regex_search? | ||
has_options?(["--regex"]) | ||
end | ||
|
||
private | ||
|
||
def has_options?(options) | ||
options.count { |option| @args.include?(option) } > 0 | ||
end | ||
|
||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,89 @@ | ||
#!/usr/bin/env ruby | ||
|
||
# Command line swtiches parser | ||
# Main part of the program | ||
|
||
require_relative '../lib/finder.rb' | ||
require_relative '../lib/errormsg.rb' | ||
require_relative '../lib/options.rb' | ||
|
||
|
||
module FindUtility | ||
class Runner | ||
Version = 0.1 | ||
|
||
Version = 0.2 | ||
|
||
def initialize(args) | ||
@args = args | ||
|
||
@prog_name = File.basename($0, File.extname($0)) | ||
end | ||
|
||
def run | ||
options = FindUtility::Options.new(@args) | ||
opts = FindUtility::Options.new(@args) | ||
|
||
if options.no_options? | ||
FindUtility::Finder.new(configuration).find(Dir.pwd, "**/*") | ||
# If no command line arguments provided, | ||
# show all files in current directory and exit | ||
if opts.no_options? | ||
FindUtility::FindName.new(Dir.pwd, "**", false, false).find | ||
return | ||
end | ||
|
||
if options.show_version? | ||
show_version | ||
if opts.show_usage? | ||
show_usage | ||
return | ||
end | ||
|
||
if options.show_tux? | ||
show_tux | ||
return | ||
if opts.path_defined? | ||
path = opts.path | ||
pattern = opts.search_pattern | ||
else | ||
path = Dir.pwd | ||
pattern = opts.path | ||
end | ||
|
||
if options.show_help? | ||
show_usage | ||
return | ||
if opts.regex_search? | ||
FindUtility::FindRegexp | ||
.new(path, pattern, opts.case_sensitive?, opts.recursive?) | ||
.find | ||
else | ||
FindUtility::FindName | ||
.new(path, pattern, opts.case_sensitive?, opts.recursive?) | ||
.find | ||
end | ||
end | ||
|
||
path = options.path_defined? ? options.path : Dir.pwd | ||
def show_usage | ||
puts <<-HELP | ||
File finder utility, version #{Version} | ||
Copyright (c) 2016 Filip Dimovski. Licensed under GPLv3+. | ||
if options.regex_search? | ||
FindUtility::RegexFinder.new(options.case_insensitive?, options.recursive?, path, options.search_pattern) | ||
return | ||
end | ||
Usage: #{@prog_name} [path] [name] [parameter(s)]... | ||
if options.name_search? | ||
FindUtility::NameFinder.new(options.case_insensitive?, path, options.search_pattern) | ||
return | ||
end | ||
Parameters: | ||
--help, -h | ||
Show this help message | ||
--recursive, -r, -R | ||
Search continues inside subdirectories as well | ||
--case, -c | ||
Search will be case-sensitive, don't ignore case | ||
--regex | ||
Search for a file name using a regular expression | ||
Hints: | ||
Search is not recursive by default, it doesn't search in subdirectories. | ||
Searches are not case sensitive by default, so 'A' is same as 'a'. | ||
If no parameters are provided, it will show files in current directory. | ||
HELP | ||
end | ||
|
||
def show_not_exist(file) | ||
puts "Directory does not exist: #{file}" | ||
end | ||
|
||
def show_no_name | ||
puts "No name or regular expression provided." | ||
end | ||
|
||
end | ||
end |