grepfruit 2.0.0 → 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: 8e88618b796ae6c0d9f54ec4bc64033e44ecee9fef5a3a889a0585dff11741e4
4
- data.tar.gz: 1d6acc4c77b5175b9d13b3c4eb23b8ed069caf6f8e031037a1ec0244d945fa5c
3
+ metadata.gz: 96acccb252509097d33a932f2d463358358d1ee20aea951561e94c7aed2e995d
4
+ data.tar.gz: 1dd758ab6f2fef47cea4890cf8fe09ed4f31cd5477d04e0be9891614ed605457
5
5
  SHA512:
6
- metadata.gz: 1690b2abec8f59847eace9a70cbaa9d8b08c54d64e35b76ef5d8f2ce6c707478a044e40fad514932f2bbcd9ec2cd289129e05b47497b102ab83fa6ed78dd631b
7
- data.tar.gz: 322537a62dbb5d81731af9d973f8b5e69dc557e0721cfabbd8add2da69712b88a231371d5842b2e710fa882b7d03c9e7fd3ed9dfc4a3a5422c893ce0b9f21050
6
+ metadata.gz: '02887d74be8c339a488626d15d53fc8609b5ec8b6ec68396d988b30d5bd147e1f643d6dc07bfdb75a71db40f4c18e5d40e556ea464814664ff8dcfa676fb5beb'
7
+ data.tar.gz: 17d41be73711460f8da710759850b53f2d622d5a03442e00521123c2c0c964e01effdb93e97a768e5d75db33d4087ec03a64a42d4466f462d4d5a65e4def5b2c
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## v2.0.1
2
+
3
+ - Enhanced output to include the number of files with matches
4
+
1
5
  ## v2.0.0
2
6
 
3
7
  - Added support for Ruby 3.4
@@ -1,3 +1,3 @@
1
1
  module Grepfruit
2
- VERSION = "2.0.0"
2
+ VERSION = "2.0.1"
3
3
  end
data/lib/search.rb CHANGED
@@ -3,11 +3,6 @@ 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:)
@@ -19,48 +14,65 @@ module Grepfruit
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)
28
+
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
32
36
 
33
- match = false
37
+ display_results(lines, files, files_with_matches)
38
+ end
34
39
 
35
- File.foreach(path).with_index do |line, line_num|
36
- next unless line.valid_encoding?
40
+ COLORS = { cyan: "\e[36m", red: "\e[31m", green: "\e[32m", reset: "\e[0m" }
41
+ private_constant :COLORS
37
42
 
38
- if line.match?(regex) && !excluded_line?(path, line_num)
39
- lines << "#{CYAN}#{relative_path_with_line_num(path, line_num)}#{RESET}: #{processed_line(line)}"
40
- match = true
41
- end
42
- end
43
+ private
43
44
 
44
- print match ? "#{RED}M#{RESET}" : "#{GREEN}.#{RESET}"
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
51
+
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)}"
45
56
  end
46
57
 
58
+ lines.size > lines_size
59
+ end
60
+
61
+ def display_results(lines, files, files_with_matches)
47
62
  puts "\n\n" if files.positive?
48
63
 
49
64
  if lines.empty?
50
- 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]}"
51
66
  exit(0)
52
67
  else
53
- puts "Matches:\n\n"
54
- puts "#{lines.join("\n")}\n\n"
55
- 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]}"
56
70
  exit(1)
57
71
  end
58
72
  end
59
73
 
60
- private
61
-
62
74
  def excluded_path?(path)
63
- excluded?(excluded_paths, relative_path(path))
75
+ excluded?(excluded_paths, relative_path(path)) || !search_hidden && hidden?(path)
64
76
  end
65
77
 
66
78
  def excluded_line?(path, line_num)
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: 2.0.0
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-12-25 00:00:00.000000000 Z
11
+ date: 2024-12-26 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: