grepfruit 1.1.2 → 2.0.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d6d72bac5375937fd7214c4d0f13de9f619695de3614202af1d877aaffdaf1f4
4
- data.tar.gz: 7d1abd0f67111b48ccf0e988c8bfcab18fd90cf0510d02571292d29f2b4642b1
3
+ metadata.gz: 96acccb252509097d33a932f2d463358358d1ee20aea951561e94c7aed2e995d
4
+ data.tar.gz: 1dd758ab6f2fef47cea4890cf8fe09ed4f31cd5477d04e0be9891614ed605457
5
5
  SHA512:
6
- metadata.gz: 18e978abd3bd6beeb29e14b88e8847f1410e3adfe8e1ad4b82910075cae099e25e5139c4ca8c4077b3666e7b2ad63911eb9a5cb637c8b3306ac42ad908f3caf1
7
- data.tar.gz: 7d48ec2fd3f5f04baa4c69b9187675e0664c46d728907702aaa9a82f62b4689f999fc923bf5cc6035feb298d80350d9cd4f2906e006276b79b6e1a9768a6bc5b
6
+ metadata.gz: '02887d74be8c339a488626d15d53fc8609b5ec8b6ec68396d988b30d5bd147e1f643d6dc07bfdb75a71db40f4c18e5d40e556ea464814664ff8dcfa676fb5beb'
7
+ data.tar.gz: 17d41be73711460f8da710759850b53f2d622d5a03442e00521123c2c0c964e01effdb93e97a768e5d75db33d4087ec03a64a42d4466f462d4d5a65e4def5b2c
data/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ ## v2.0.1
2
+
3
+ - Enhanced output to include the number of files with matches
4
+
5
+ ## v2.0.0
6
+
7
+ - Added support for Ruby 3.4
8
+ - Dropped support for Ruby 3.0
9
+
1
10
  ## v1.1.2
2
11
 
3
12
  - Refactored code significantly for improved search efficiency and easier maintenance
data/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Grepfruit is a Ruby gem for searching files within a directory for a specified regular expression pattern, with options to exclude certain files or directories from the search and colorized output for better readability.
7
7
 
8
- <img width="416" alt="Screenshot 2024-07-28 at 03 52 37" src="https://github.com/user-attachments/assets/95b26b81-dcc1-430b-ac44-641251cb84b6">
8
+ <img width="440" alt="Screenshot 2024-08-16 at 21 17 25" src="https://github.com/user-attachments/assets/3caaa1f4-5636-4ca1-ae45-6fc56f8945e4">
9
9
 
10
10
  Grepfruit was originally created to be used in CI/CD pipelines to search for `TODO` comments in Ruby on Rails applications and provide more user-friendly output than the standard `grep` command, but it is flexible enough to be used for other similar purposes as well.
11
11
 
@@ -23,14 +23,22 @@ And then execute:
23
23
  bundle install
24
24
  ```
25
25
 
26
+ Or install it yourself as:
27
+
28
+ ```shell
29
+ gem install grepfruit
30
+ ```
31
+
26
32
  ## Usage
27
33
 
28
34
  You can use Grepfruit from the command line to search for a regex pattern within files in a specified directory.
29
35
 
30
36
  ```shell
31
- bundle exec grepfruit [options] PATH
37
+ grepfruit [options] PATH
32
38
  ```
33
39
 
40
+ If no matches are found, Grepfruit returns exit status 0; otherwise, it returns exit status 1.
41
+
34
42
  ### Options
35
43
 
36
44
  - `-r, --regex REGEX`: Regex pattern to search for (required).
@@ -43,31 +51,31 @@ bundle exec grepfruit [options] PATH
43
51
  Search for the pattern `/TODO/` in the current directory, excluding `log`, `tmp`, `vendor`, `node_modules`, and `assets` directories:
44
52
 
45
53
  ```shell
46
- bundle exec grepfruit -r 'TODO' -e 'log,tmp,vendor,node_modules,assets'
54
+ grepfruit -r 'TODO' -e 'log,tmp,vendor,node_modules,assets'
47
55
  ```
48
56
 
49
57
  Search for the pattern `/FIXME|TODO/` in `dev/grepfruit` directory, excluding `bin`, `tmp/log`, and `Gemfile.lock` files and directories:
50
58
 
51
59
  ```shell
52
- bundle exec grepfruit -r 'FIXME|TODO' -e 'bin,tmp/log,Gemfile.lock' dev/grepfruit
60
+ grepfruit -r 'FIXME|TODO' -e 'bin,tmp/log,Gemfile.lock' dev/grepfruit
53
61
  ```
54
62
 
55
63
  Search for the pattern `/FIXME|TODO/` in the current directory, excluding line 18 of `README.md`:
56
64
 
57
65
  ```shell
58
- bundle exec grepfruit -r 'FIXME|TODO' -e 'README.md:18'
66
+ grepfruit -r 'FIXME|TODO' -e 'README.md:18'
59
67
  ```
60
68
 
61
69
  Search for the pattern `/FIXME|TODO/` in the current directory, truncating the output of the search results to 50 characters:
62
70
 
63
71
  ```shell
64
- bundle exec grepfruit -r 'FIXME|TODO' -t 50
72
+ grepfruit -r 'FIXME|TODO' -t 50
65
73
  ```
66
74
 
67
75
  Search for the pattern `/FIXME|TODO/` in the current directory, including hidden files and directories:
68
76
 
69
77
  ```shell
70
- bundle exec grepfruit -r 'FIXME|TODO' --search-hidden
78
+ grepfruit -r 'FIXME|TODO' --search-hidden
71
79
  ```
72
80
 
73
81
  ## Problems?
data/exe/grepfruit CHANGED
@@ -15,22 +15,10 @@ options = {
15
15
 
16
16
  OptionParser.new do |opts|
17
17
  opts.banner = "Usage: grepfruit [options] PATH"
18
-
19
- opts.on("-r", "--regex REGEX", Regexp, "Regex pattern to search for") do |regex|
20
- options[:regex] = regex
21
- end
22
-
23
- opts.on("-e", "--exclude x,y,z", Array, "Comma-separated list of files and directories to exclude") do |exclude|
24
- options[:exclude] = exclude
25
- end
26
-
27
- opts.on("-t", "--truncate N", Integer, "Truncate output to N characters") do |truncate|
28
- options[:truncate] = truncate
29
- end
30
-
31
- opts.on("--search-hidden", TrueClass, "Search hidden files and directories") do |search_hidden|
32
- options[:search_hidden] = search_hidden
33
- end
18
+ opts.on("-r", "--regex REGEX", Regexp, "Regex pattern to search for") { options[:regex] = _1 }
19
+ opts.on("-e", "--exclude x,y,z", Array, "Comma-separated list of files and directories to exclude") { options[:exclude] = _1 }
20
+ opts.on("-t", "--truncate N", Integer, "Truncate output to N characters") { options[:truncate] = _1 }
21
+ opts.on("--search-hidden", TrueClass, "Search hidden files and directories") { options[:search_hidden] = _1 }
34
22
  end.parse!
35
23
 
36
24
  if options[:regex].nil?
data/grepfruit.gemspec CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
11
11
  spec.metadata["rubygems_mfa_required"] = "true"
12
12
  spec.summary = "A Ruby gem for searching text patterns in files with colorized output"
13
13
  spec.license = "MIT"
14
- spec.required_ruby_version = ">= 3.0", "< 3.4"
14
+ spec.required_ruby_version = ">= 3.1", "< 3.5"
15
15
 
16
16
  spec.files = [
17
17
  "grepfruit.gemspec", "README.md", "CHANGELOG.md", "LICENSE.txt"
@@ -1,3 +1,3 @@
1
1
  module Grepfruit
2
- VERSION = "1.1.2"
2
+ VERSION = "2.0.1"
3
3
  end
data/lib/search.rb CHANGED
@@ -3,66 +3,76 @@ require "find"
3
3
 
4
4
  module Grepfruit
5
5
  class Search
6
- CYAN = "\e[36m"
7
- RED = "\e[31m"
8
- GREEN = "\e[32m"
9
- RESET = "\e[0m"
10
-
11
6
  attr_reader :dir, :regex, :excluded_paths, :excluded_lines, :truncate, :search_hidden
12
7
 
13
8
  def initialize(dir:, regex:, exclude:, truncate:, search_hidden:)
14
9
  @dir = dir
15
10
  @regex = regex
16
- @excluded_lines, @excluded_paths = exclude.map { |e| e.split("/") }.partition { |e| e.last.include?(":") }
11
+ @excluded_lines, @excluded_paths = exclude.map { _1.split("/") }.partition { _1.last.include?(":") }
17
12
  @truncate = truncate
18
13
  @search_hidden = search_hidden
19
14
  end
20
15
 
21
16
  def run
22
- lines, files = [], 0
17
+ lines, files, files_with_matches = [], 0, 0
23
18
 
24
19
  puts "Searching for #{regex.inspect} in #{dir.inspect}...\n\n"
25
20
 
26
21
  Find.find(dir) do |path|
27
- Find.prune if excluded_path?(path) || !search_hidden && hidden?(path)
22
+ Find.prune if excluded_path?(path)
28
23
 
29
- next if File.directory?(path) || File.symlink?(path)
24
+ next if not_searchable?(path)
30
25
 
31
26
  files += 1
27
+ match = process_file(path, lines)
32
28
 
33
- match = false
29
+ if match
30
+ files_with_matches += 1
31
+ print "#{COLORS[:red]}M#{COLORS[:reset]}"
32
+ else
33
+ print "#{COLORS[:green]}.#{COLORS[:reset]}"
34
+ end
35
+ end
34
36
 
35
- File.foreach(path).with_index do |line, line_num|
36
- next unless line.valid_encoding?
37
+ display_results(lines, files, files_with_matches)
38
+ end
37
39
 
38
- if line.match?(regex)
39
- next if excluded_line?(path, line_num)
40
+ COLORS = { cyan: "\e[36m", red: "\e[31m", green: "\e[32m", reset: "\e[0m" }
41
+ private_constant :COLORS
40
42
 
41
- lines << "#{CYAN}#{relative_path_with_line_num(path, line_num)}#{RESET}: #{processed_line(line)}"
42
- match = true
43
- end
44
- end
43
+ private
44
+
45
+ def not_searchable?(path)
46
+ File.directory?(path) || File.symlink?(path)
47
+ end
48
+
49
+ def process_file(path, lines)
50
+ lines_size = lines.size
45
51
 
46
- print match ? "#{RED}M#{RESET}" : "#{GREEN}.#{RESET}"
52
+ File.foreach(path).with_index do |line, line_num|
53
+ next if !line.valid_encoding? || !line.match?(regex) || excluded_line?(path, line_num)
54
+
55
+ lines << "#{COLORS[:cyan]}#{relative_path_with_line_num(path, line_num)}#{COLORS[:reset]}: #{processed_line(line)}"
47
56
  end
48
57
 
58
+ lines.size > lines_size
59
+ end
60
+
61
+ def display_results(lines, files, files_with_matches)
49
62
  puts "\n\n" if files.positive?
50
63
 
51
64
  if lines.empty?
52
- puts "#{number_of_files(files)} checked, #{GREEN}no matches found#{RESET}"
65
+ puts "#{number_of_files(files)} checked, #{COLORS[:green]}no matches found#{COLORS[:reset]}"
53
66
  exit(0)
54
67
  else
55
- puts "Matches:\n\n"
56
- puts "#{lines.join("\n")}\n\n"
57
- puts "#{number_of_files(files)} checked, #{RED}#{number_of_matches(lines.size)} found#{RESET}"
68
+ puts "Matches:\n\n#{lines.join("\n")}\n\n"
69
+ puts "#{number_of_files(files)} checked, #{COLORS[:red]}#{number_of_matches(lines.size)} found in #{number_of_files(files_with_matches)}#{COLORS[:reset]}"
58
70
  exit(1)
59
71
  end
60
72
  end
61
73
 
62
- private
63
-
64
74
  def excluded_path?(path)
65
- excluded?(excluded_paths, relative_path(path))
75
+ excluded?(excluded_paths, relative_path(path)) || !search_hidden && hidden?(path)
66
76
  end
67
77
 
68
78
  def excluded_line?(path, line_num)
@@ -70,7 +80,7 @@ module Grepfruit
70
80
  end
71
81
 
72
82
  def excluded?(list, path)
73
- list.any? { |e| path.split("/").last(e.length) == e }
83
+ list.any? { path.split("/").last(_1.length) == _1 }
74
84
  end
75
85
 
76
86
  def relative_path(path)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grepfruit
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - enjaku4
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-08-15 00:00:00.000000000 Z
11
+ date: 2024-12-26 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -41,17 +41,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
41
41
  requirements:
42
42
  - - ">="
43
43
  - !ruby/object:Gem::Version
44
- version: '3.0'
44
+ version: '3.1'
45
45
  - - "<"
46
46
  - !ruby/object:Gem::Version
47
- version: '3.4'
47
+ version: '3.5'
48
48
  required_rubygems_version: !ruby/object:Gem::Requirement
49
49
  requirements:
50
50
  - - ">="
51
51
  - !ruby/object:Gem::Version
52
52
  version: '0'
53
53
  requirements: []
54
- rubygems_version: 3.5.11
54
+ rubygems_version: 3.3.27
55
55
  signing_key:
56
56
  specification_version: 4
57
57
  summary: A Ruby gem for searching text patterns in files with colorized output