grepfruit 0.2.0 → 1.1.0
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 +4 -4
- data/CHANGELOG.md +11 -0
- data/README.md +23 -8
- data/exe/grepfruit +15 -5
- data/grepfruit.gemspec +1 -1
- data/lib/grepfruit/version.rb +1 -1
- data/lib/grepfruit.rb +16 -9
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4375b6c22cf2b2323abd76e66e35c098a4f681fe24fd63a40df0a4fa90640ea3
|
4
|
+
data.tar.gz: 743a2141c660fa7aa90687f56de16e5d3c9b5172abe4b9d52f7aa7ad322a773c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d2a124c7e449b5a098c229f07e2c392ee89c06391f1c75ae5666bb485ac28d10dec33e0f556eda2bd5bbcaf659b052ea013ae4bb2617efcb2a6a2ed7c3a89fa6
|
7
|
+
data.tar.gz: 1e4640be220f8a72b1592e229de4d8bf70bf06e1d6d461f2fd85d663b5bd67bc7d27ea0e513867a32e6a129ab07ad13dbe4b905ef023633c89c7c61fda81cdd8
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,14 @@
|
|
1
|
+
## v1.1.0
|
2
|
+
|
3
|
+
- Added `--truncate` option to truncate the output of the search results
|
4
|
+
- Added the ability to exclude lines from the search results
|
5
|
+
|
6
|
+
## v1.0.0
|
7
|
+
|
8
|
+
- Removed default values for `--exclude` and `--regex` options
|
9
|
+
- Made `--regex` option required
|
10
|
+
- Fixed an error that was raised when a symbolic link was encountered during the search
|
11
|
+
|
1
12
|
## v0.2.0
|
2
13
|
|
3
14
|
- Added `--search-hidden` option to search hidden files and directories
|
data/README.md
CHANGED
@@ -7,6 +7,8 @@ Grepfruit is a Ruby gem for searching files within a directory for a specified r
|
|
7
7
|
|
8
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">
|
9
9
|
|
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
|
+
|
10
12
|
## Installation
|
11
13
|
|
12
14
|
Add this line to your application's Gemfile:
|
@@ -31,25 +33,38 @@ bundle exec grepfruit [options] PATH
|
|
31
33
|
|
32
34
|
### Options
|
33
35
|
|
34
|
-
- `-r, --regex REGEX`: Regex pattern to search for.
|
35
|
-
- `-e, --exclude x,y,z`: Comma-separated list of files
|
36
|
-
-
|
36
|
+
- `-r, --regex REGEX`: Regex pattern to search for (required).
|
37
|
+
- `-e, --exclude x,y,z`: Comma-separated list of files, directories, or lines to exclude from the search.
|
38
|
+
- `-t, --truncate N`: Truncate the output of the search results to N characters.
|
39
|
+
- `--search-hidden`: Search hidden files and directories.
|
37
40
|
|
38
41
|
### Examples
|
39
42
|
|
40
|
-
Search for the pattern
|
43
|
+
Search for the pattern `/TODO/` in the current directory, excluding `log`, `tmp`, `vendor`, `node_modules`, and `assets` directories:
|
44
|
+
|
45
|
+
```shell
|
46
|
+
bundle exec grepfruit -r 'TODO' -e 'log,tmp,vendor,node_modules,assets'
|
47
|
+
```
|
48
|
+
|
49
|
+
Search for the pattern `/FIXME|TODO/` in `dev/grepfruit` directory, excluding `bin`, `tmp/log`, and `Gemfile.lock` files and directories:
|
50
|
+
|
51
|
+
```shell
|
52
|
+
bundle exec grepfruit -r 'FIXME|TODO' -e 'bin,tmp/log,Gemfile.lock' dev/grepfruit
|
53
|
+
```
|
54
|
+
|
55
|
+
Search for the pattern `/FIXME|TODO/` in the current directory, excluding line 18 of `README.md`:
|
41
56
|
|
42
57
|
```shell
|
43
|
-
bundle exec grepfruit
|
58
|
+
bundle exec grepfruit -r 'FIXME|TODO' -e 'README.md:18'
|
44
59
|
```
|
45
60
|
|
46
|
-
Search for
|
61
|
+
Search for the pattern `/FIXME|TODO/` in the current directory, truncating the output of the search results to 50 characters:
|
47
62
|
|
48
63
|
```shell
|
49
|
-
bundle exec grepfruit -r 'FIXME|TODO' -
|
64
|
+
bundle exec grepfruit -r 'FIXME|TODO' -t 50
|
50
65
|
```
|
51
66
|
|
52
|
-
Search for
|
67
|
+
Search for the pattern `/FIXME|TODO/` in the current directory, including hidden files and directories:
|
53
68
|
|
54
69
|
```shell
|
55
70
|
bundle exec grepfruit -r 'FIXME|TODO' --search-hidden
|
data/exe/grepfruit
CHANGED
@@ -6,9 +6,10 @@ require "optparse"
|
|
6
6
|
require "grepfruit"
|
7
7
|
|
8
8
|
options = {
|
9
|
-
|
10
|
-
regex:
|
11
|
-
exclude: [
|
9
|
+
dir: Dir.pwd,
|
10
|
+
regex: nil,
|
11
|
+
exclude: [],
|
12
|
+
truncate: nil,
|
12
13
|
search_hidden: false
|
13
14
|
}
|
14
15
|
|
@@ -20,7 +21,11 @@ OptionParser.new do |opts|
|
|
20
21
|
end
|
21
22
|
|
22
23
|
opts.on("-e", "--exclude x,y,z", Array, "Comma-separated list of files and directories to exclude") do |exclude|
|
23
|
-
options[:exclude] = exclude.map
|
24
|
+
options[:exclude] = exclude.map { |path| path.split("/") }
|
25
|
+
end
|
26
|
+
|
27
|
+
opts.on("-t", "--truncate N", Integer, "Truncate output to N characters") do |truncate|
|
28
|
+
options[:truncate] = truncate
|
24
29
|
end
|
25
30
|
|
26
31
|
opts.on("--search-hidden", TrueClass, "Search hidden files and directories") do |search_hidden|
|
@@ -28,6 +33,11 @@ OptionParser.new do |opts|
|
|
28
33
|
end
|
29
34
|
end.parse!
|
30
35
|
|
31
|
-
options[:
|
36
|
+
if options[:regex].nil?
|
37
|
+
puts "Error: You must specify a regex pattern using the -r or --regex option."
|
38
|
+
exit 1
|
39
|
+
end
|
40
|
+
|
41
|
+
options[:dir] = ARGV[0] if ARGV[0]
|
32
42
|
|
33
43
|
Grepfruit.run(**options)
|
data/grepfruit.gemspec
CHANGED
@@ -5,7 +5,7 @@ Gem::Specification.new do |spec|
|
|
5
5
|
spec.version = Grepfruit::VERSION
|
6
6
|
spec.authors = ["enjaku4"]
|
7
7
|
spec.metadata["rubygems_mfa_required"] = "true"
|
8
|
-
spec.summary = "A
|
8
|
+
spec.summary = "A Ruby gem for searching text patterns in files with colorized output."
|
9
9
|
spec.homepage = "https://github.com/enjaku4/grepfruit"
|
10
10
|
spec.license = "MIT"
|
11
11
|
spec.required_ruby_version = ">= 3.0.0", "< 3.4"
|
data/lib/grepfruit/version.rb
CHANGED
data/lib/grepfruit.rb
CHANGED
@@ -3,27 +3,34 @@ require "pathname"
|
|
3
3
|
require "find"
|
4
4
|
|
5
5
|
module Grepfruit
|
6
|
-
def self.run(
|
6
|
+
def self.run(dir:, regex:, exclude:, truncate:, search_hidden:)
|
7
7
|
lines = []
|
8
8
|
files = 0
|
9
|
-
|
9
|
+
excluded_lines = exclude.select { |e| e.any? { |s| s.include?(":") } }
|
10
|
+
excluded_paths = exclude - excluded_lines
|
10
11
|
|
11
|
-
puts "Searching for #{regex.inspect}...\n\n"
|
12
|
+
puts "Searching for #{regex.inspect} in #{dir.inspect}...\n\n"
|
12
13
|
|
13
|
-
Find.find(dir) do |
|
14
|
-
Find.prune if
|
14
|
+
Find.find(dir) do |path|
|
15
|
+
Find.prune if excluded_paths.any? { |e| path.split("/").last(e.length) == e } || !search_hidden && File.basename(path).start_with?(".")
|
15
16
|
|
16
|
-
next if File.directory?(
|
17
|
+
next if File.directory?(path) || File.symlink?(path)
|
17
18
|
|
18
19
|
files += 1
|
19
20
|
|
20
21
|
match = false
|
21
22
|
|
22
|
-
File.foreach(
|
23
|
+
File.foreach(path).with_index do |line, line_num|
|
23
24
|
next unless line.valid_encoding?
|
24
25
|
|
25
26
|
if line.match?(regex)
|
26
|
-
|
27
|
+
path_with_line = "#{Pathname.new(path).relative_path_from(Pathname.new(dir))}:#{line_num + 1}"
|
28
|
+
|
29
|
+
next if excluded_lines.any? { |e| path_with_line.split("/").last(e.length) == e }
|
30
|
+
|
31
|
+
processed_line = line.strip
|
32
|
+
processed_line = "#{processed_line[0..truncate - 1]}..." if truncate && processed_line.length > truncate
|
33
|
+
lines << "\e[36m#{path_with_line}\e[0m: #{processed_line}"
|
27
34
|
match = true
|
28
35
|
end
|
29
36
|
end
|
@@ -34,7 +41,7 @@ module Grepfruit
|
|
34
41
|
puts "\n\n"
|
35
42
|
|
36
43
|
if lines.empty?
|
37
|
-
puts "#{files} files checked, \e[32mno matches found\e[0m"
|
44
|
+
puts "#{files} file#{'s' if files > 1} checked, \e[32mno matches found\e[0m"
|
38
45
|
exit(0)
|
39
46
|
else
|
40
47
|
puts "Matches:\n\n"
|
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:
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- enjaku4
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-07-
|
11
|
+
date: 2024-07-31 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|
@@ -50,5 +50,5 @@ requirements: []
|
|
50
50
|
rubygems_version: 3.2.33
|
51
51
|
signing_key:
|
52
52
|
specification_version: 4
|
53
|
-
summary: A
|
53
|
+
summary: A Ruby gem for searching text patterns in files with colorized output.
|
54
54
|
test_files: []
|