grepfruit 1.0.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 +5 -0
- data/README.md +15 -4
- data/exe/grepfruit +8 -3
- data/grepfruit.gemspec +1 -1
- data/lib/grepfruit/version.rb +1 -1
- data/lib/grepfruit.rb +15 -8
- metadata +4 -4
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
data/README.md
CHANGED
@@ -33,8 +33,9 @@ bundle exec grepfruit [options] PATH
|
|
33
33
|
|
34
34
|
### Options
|
35
35
|
|
36
|
-
- `-r, --regex REGEX`: Regex pattern to search for (required).
|
37
|
-
- `-e, --exclude x,y,z`: Comma-separated list of files
|
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.
|
38
39
|
- `--search-hidden`: Search hidden files and directories.
|
39
40
|
|
40
41
|
### Examples
|
@@ -43,8 +44,6 @@ Search for the pattern `/TODO/` in the current directory, excluding `log`, `tmp`
|
|
43
44
|
|
44
45
|
```shell
|
45
46
|
bundle exec grepfruit -r 'TODO' -e 'log,tmp,vendor,node_modules,assets'
|
46
|
-
# or
|
47
|
-
bundle exec grepfruit --regex 'TODO' --exclude 'log,tmp,vendor,node_modules,assets'
|
48
47
|
```
|
49
48
|
|
50
49
|
Search for the pattern `/FIXME|TODO/` in `dev/grepfruit` directory, excluding `bin`, `tmp/log`, and `Gemfile.lock` files and directories:
|
@@ -53,6 +52,18 @@ Search for the pattern `/FIXME|TODO/` in `dev/grepfruit` directory, excluding `b
|
|
53
52
|
bundle exec grepfruit -r 'FIXME|TODO' -e 'bin,tmp/log,Gemfile.lock' dev/grepfruit
|
54
53
|
```
|
55
54
|
|
55
|
+
Search for the pattern `/FIXME|TODO/` in the current directory, excluding line 18 of `README.md`:
|
56
|
+
|
57
|
+
```shell
|
58
|
+
bundle exec grepfruit -r 'FIXME|TODO' -e 'README.md:18'
|
59
|
+
```
|
60
|
+
|
61
|
+
Search for the pattern `/FIXME|TODO/` in the current directory, truncating the output of the search results to 50 characters:
|
62
|
+
|
63
|
+
```shell
|
64
|
+
bundle exec grepfruit -r 'FIXME|TODO' -t 50
|
65
|
+
```
|
66
|
+
|
56
67
|
Search for the pattern `/FIXME|TODO/` in the current directory, including hidden files and directories:
|
57
68
|
|
58
69
|
```shell
|
data/exe/grepfruit
CHANGED
@@ -6,9 +6,10 @@ require "optparse"
|
|
6
6
|
require "grepfruit"
|
7
7
|
|
8
8
|
options = {
|
9
|
-
|
9
|
+
dir: Dir.pwd,
|
10
10
|
regex: nil,
|
11
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|
|
@@ -33,6 +38,6 @@ if options[:regex].nil?
|
|
33
38
|
exit 1
|
34
39
|
end
|
35
40
|
|
36
|
-
options[:
|
41
|
+
options[:dir] = ARGV[0] if ARGV[0]
|
37
42
|
|
38
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
|
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.
|
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:
|
@@ -47,8 +47,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
47
47
|
- !ruby/object:Gem::Version
|
48
48
|
version: '0'
|
49
49
|
requirements: []
|
50
|
-
rubygems_version: 3.
|
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: []
|