fif 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (6) hide show
  1. checksums.yaml +7 -0
  2. data/bin/fif +75 -0
  3. data/lib/fif/utils.rb +39 -0
  4. data/lib/fif.rb +45 -0
  5. data/test/test_fif.rb +84 -0
  6. metadata +47 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e7d694888b4597bef7ebc8cfa539cc5274605759
4
+ data.tar.gz: 0e65ed7934e9bcf587e7fa2de805c6e73aaaa494
5
+ SHA512:
6
+ metadata.gz: 3b5125557862b66e2e05adfdeb2ed70b3b7c96cce6a5bd07cc9735f44c12d6b426fc3464c202ff35555297c50c68418da9b8a0af68be6c6bcd9300cbec5ac6f9
7
+ data.tar.gz: 13cb3fb53233501cfd23ff8ec22fed212e87d3bba92b794b794f1eb130e467823676228fd5c349f87ca237913891e9aa330ca476f4c4d8d0057832acd264dda7
data/bin/fif ADDED
@@ -0,0 +1,75 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+
5
+ require 'fif'
6
+
7
+ options = {
8
+ :colour => true,
9
+ :dumb => false,
10
+ :regex_opts => 0,
11
+ :fregex_opts => 0,
12
+ }
13
+
14
+ banner = <<-eos
15
+ Usage: fif [options] search_string [filename_string]
16
+
17
+ Find lines matching search_string in files recursively in a directory. Files can
18
+ optionally be limited to those matching the pattern filename_string.
19
+
20
+ Options:
21
+ eos
22
+
23
+ begin
24
+ OptionParser.new do |o|
25
+ o.banner = banner
26
+
27
+ o.on('-c', '--colour', 'Disable coloured output.') do |c|
28
+ options[:colour] = false
29
+ end
30
+
31
+ o.on('-d', '--dumb', 'Don\'t use regular expressions') do |d|
32
+ options[:dumb] = d
33
+ end
34
+
35
+ o.on('-I', '--file-ignorecase', 'Case insensitive matching on filenames.') do |i|
36
+ raise OptionCombinationError if options[:dumb]
37
+ options[:fregex_opts] |= Regexp::IGNORECASE
38
+ end
39
+ o.on('-X', '--file-extended', 'Ignore whitespace in filename regex.') do |x|
40
+ raise OptionCombinationError if options[:dumb]
41
+ options[:fregex_opts] |= Regexp::EXTENDED
42
+ end
43
+
44
+ o.on('-i', '--ignorecase', 'Case insensitive matching in files.') do |i|
45
+ raise OptionCombinationError if options[:dumb]
46
+ options[:regex_opts] |= Regexp::IGNORECASE
47
+ end
48
+ o.on('-m', '--multiline', 'Make dot match newlines.') do |m|
49
+ raise OptionCombinationError if options[:dumb]
50
+ options[:regex_opts] |= Regexp::MULTILINE
51
+ end
52
+ o.on('-x', '--extended', 'Ignore whitespace in regex.') do |x|
53
+ raise OptionCombinationError if options[:dumb]
54
+ options[:regex_opts] |= Regexp::EXTENDED
55
+ end
56
+
57
+ o.on_tail('-h', '--help', 'Output this help and exit.') { puts o; exit }
58
+ o.parse!
59
+ end
60
+ rescue OptionCombinationError
61
+ puts "Cannot mix regex options with dumb search, see `fif --help`."
62
+ exit
63
+ rescue OptionParser::InvalidOption
64
+ puts "Invalid argument(s), see `fif --help`."
65
+ exit
66
+ end
67
+
68
+ if ARGV.empty?
69
+ puts "Missing argument(s), see `fif --help`."
70
+ elsif options[:dumb]
71
+ Fif.dumb_search(ARGV, options) { |result| puts result }
72
+ else
73
+ p options
74
+ Fif.search(ARGV, options) { |result| puts result }
75
+ end
data/lib/fif/utils.rb ADDED
@@ -0,0 +1,39 @@
1
+ class Dir
2
+ def self.recurse(path, &block)
3
+ Dir.foreach(path) do |entry|
4
+ next if (entry == '.' || entry == '..')
5
+
6
+ full_path = File.join(path, entry)
7
+
8
+ if File.directory?(full_path)
9
+ recurse(full_path, &block)
10
+ else
11
+ yield full_path
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ class String
18
+ # colorization
19
+ def colorize(color_code)
20
+ "\e[#{color_code}m#{self}\e[0m"
21
+ end
22
+
23
+ def red
24
+ colorize(31)
25
+ end
26
+
27
+ def green
28
+ colorize(32)
29
+ end
30
+ end
31
+
32
+ class File
33
+ def self.binary?(path)
34
+ IO.popen(
35
+ ["file", "-b", "--mime-encoding", path],
36
+ in: :close, err: :close
37
+ ).read.chomp.include? 'binary'
38
+ end
39
+ end
data/lib/fif.rb ADDED
@@ -0,0 +1,45 @@
1
+ require 'fif/utils'
2
+
3
+ require 'pathname'
4
+
5
+ class Fif
6
+ def self.search(arguments, options)
7
+ regex = Regexp.new("(#{arguments[0]})", options[:regex_opts])
8
+ fregex = Regexp.new("#{arguments[1]}", options[:fregex_opts])
9
+ Dir.recurse('.') do |file|
10
+ if arguments[1] == nil || fregex.match(file)
11
+ if not File.binary?(file)
12
+ f = File.new(file)
13
+ f.each do |line|
14
+ begin
15
+ if regex.match(line)
16
+ line.gsub!(regex, '\1'.green) if options[:colour]
17
+ yield "#{file}:#{f.lineno}:\t#{line}"
18
+ end
19
+ rescue ArgumentError
20
+ puts "Ignoring unreadable file: #{file}"
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ def self.dumb_search(arguments, options)
29
+ Dir.recurse('.') do |file|
30
+ if arguments[1] == nil || file[arguments[1]]
31
+ if not File.binary?(file)
32
+ f = File.new(file)
33
+ f.each do |line|
34
+ if line[arguments[0]]
35
+ line[arguments[0]] = arguments[0].green if options[:colour]
36
+ yield "#{file}:#{f.lineno}:\t#{line}"
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ class OptionCombinationError < Exception; end
data/test/test_fif.rb ADDED
@@ -0,0 +1,84 @@
1
+ require 'test/unit'
2
+ require 'fif'
3
+
4
+ class FifTest < Test::Unit::TestCase
5
+ def test_search
6
+ results_a, results_b = 0, 0
7
+
8
+ options = {
9
+ :colour => false,
10
+ :regex_opts => 0,
11
+ :fregex_opts => 0,
12
+ }
13
+ arguments = [
14
+ "f[aeiou]f"
15
+ ]
16
+ regex = Regexp.new(arguments[0])
17
+
18
+ Fif.search(arguments, options) do |result|
19
+ results_a += 1
20
+ assert regex.match result
21
+ end
22
+
23
+
24
+ arguments[1] = ".rb"
25
+
26
+ Fif.search(arguments, options) do |result|
27
+ results_b += 1
28
+ assert regex.match result
29
+ end
30
+
31
+
32
+ assert_not_equal results_a, results_b
33
+
34
+
35
+ arguments[0] = "F[aeiou]F"
36
+ arguments[1] = nil
37
+ options[:regex_opts] |= Regexp::IGNORECASE
38
+ regex = Regexp.new(arguments[0], Regexp::IGNORECASE)
39
+ results_a, results_b = 0, 0
40
+
41
+ Fif.search(arguments, options) do |result|
42
+ results_a += 1
43
+ assert regex.match result
44
+ end
45
+
46
+ arguments[1] = ".rB"
47
+ options[:fregex_opts] |= Regexp::IGNORECASE
48
+
49
+ Fif.search(arguments, options) do |result|
50
+ results_b += 1
51
+ assert regex.match result
52
+ end
53
+
54
+
55
+ assert_not_equal results_a, results_b
56
+ end
57
+
58
+ def test_dumb
59
+ results_a, results_b = 0, 0
60
+
61
+ options = {
62
+ :colour => false
63
+ }
64
+ arguments = [
65
+ "fif"
66
+ ]
67
+
68
+ Fif.dumb_search(arguments, options) do |result|
69
+ results_a += 1
70
+ assert result.include? arguments[0]
71
+ end
72
+
73
+
74
+ arguments[1] = ".rb"
75
+
76
+ Fif.dumb_search(arguments, options) do |result|
77
+ results_b += 1
78
+ assert result.include? arguments[0]
79
+ end
80
+
81
+
82
+ assert_not_equal results_a, results_b
83
+ end
84
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fif
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.2
5
+ platform: ruby
6
+ authors:
7
+ - Elliott Hillary
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-04-03 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple hello world gem
14
+ email: elliotthillary@googlemail.com
15
+ executables:
16
+ - fif
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/fif.rb
21
+ - lib/fif/utils.rb
22
+ - test/test_fif.rb
23
+ - bin/fif
24
+ homepage: http://github.com/ElliottH/fif
25
+ licenses: []
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 2.0.0.rc.2
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: Find in files
47
+ test_files: []