sq_ruby_grep 0.0.3 → 0.0.4
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/README.md +4 -8
- data/bin/sq_ruby_grep +2 -1
- data/example.rb +1 -8
- data/lib/sq_ruby_grep/parser.rb +4 -7
- data/lib/sq_ruby_grep/version.rb +1 -1
- data/lib/sq_ruby_grep.rb +39 -49
- data/spec/sq_ruby_grep_spec.rb +48 -110
- data/sq_ruby_grep.gemspec +9 -8
- data/test/fixtures/text.txt +5 -5
- metadata +16 -4
- data/test/fixtures/lines.txt +0 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2688d28246d3cb3f85bb521c7681bc7eba93e37c
|
4
|
+
data.tar.gz: 93a5b5a2a4922e6f85f478493fe16d21afb0ec8a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 538b57f60e8f781d162d2f446ca27ebced40ce97be61d5b861ca00d80de16e8d45b37c37a4c6669680e9e3110588e3d81870c62186fc3dd3d0c142714f7f76c6
|
7
|
+
data.tar.gz: f0a4c4307c86f0de281d367867ff68a93fff2d97a4eb89a1cc50d95465eac0c46208c753f62341df8f80a980d94ed1f4f814cc4e1c3d24428c372e34b723e87d
|
data/README.md
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
# SqRubyGrep
|
2
2
|
|
3
|
-
It just the training project for SQ interview. Forget it! :)
|
4
|
-
|
5
3
|
## Installation
|
6
4
|
|
7
5
|
Add this line to your application's Gemfile:
|
@@ -20,15 +18,13 @@ Or install it yourself as:
|
|
20
18
|
|
21
19
|
## Usage
|
22
20
|
|
23
|
-
$ sq_ruby_grep
|
21
|
+
$ sq_ruby_grep <pattern> <file> [options]
|
24
22
|
|
25
|
-
-e, --regex=PATTERN Use PATTERN as the pattern.
|
26
|
-
|
27
23
|
-A, --after-context=NUM Print NUM lines of trailing context after matching lines.
|
28
24
|
|
29
25
|
-B, --before-context=NUM Print NUM line of leading context before matching lines.
|
30
|
-
|
31
|
-
-
|
32
|
-
|
26
|
+
|
27
|
+
--not-colorize Without colorize
|
28
|
+
|
33
29
|
-h, --help Prints this help
|
34
30
|
|
data/bin/sq_ruby_grep
CHANGED
data/example.rb
CHANGED
@@ -2,11 +2,4 @@ lib = File.expand_path('../lib', __FILE__)
|
|
2
2
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
3
|
require 'sq_ruby_grep'
|
4
4
|
|
5
|
-
SqRubyGrep.
|
6
|
-
p '------------'
|
7
|
-
puts result.before_context
|
8
|
-
puts '---' + result.match_line
|
9
|
-
puts result.after_context
|
10
|
-
p '------------'
|
11
|
-
end
|
12
|
-
|
5
|
+
puts SqRubyGrep.run(file_path:'test/fixtures/text.txt', pattern: /needle/, after_lines: 1, before_lines: 2)
|
data/lib/sq_ruby_grep/parser.rb
CHANGED
@@ -3,16 +3,12 @@ require 'optparse'
|
|
3
3
|
module SqRubyGrep
|
4
4
|
class Parser
|
5
5
|
def self.parse(options)
|
6
|
-
args =
|
6
|
+
args = {colorize: true}
|
7
7
|
|
8
8
|
opt_parser = OptionParser.new do |opts|
|
9
9
|
opts.banner = 'RubyGrep searches the named input FILE for lines containing a match to the given PATTERN.'
|
10
10
|
opts.banner = 'Usage: ruby_grep [FILE] [options]'
|
11
11
|
|
12
|
-
opts.on('-e PATTERN', '--regex=PATTERN', String, 'Use PATTERN as the pattern.') do |pattern|
|
13
|
-
args[:pattern] = pattern
|
14
|
-
end
|
15
|
-
|
16
12
|
opts.on('-A NUM', '--after-context=NUM', Integer, 'Print NUM lines of trailing context after matching lines.') do |n|
|
17
13
|
args[:after_lines] = n
|
18
14
|
end
|
@@ -21,8 +17,8 @@ module SqRubyGrep
|
|
21
17
|
args[:before_lines] = n
|
22
18
|
end
|
23
19
|
|
24
|
-
opts.on('-
|
25
|
-
args[:colorize] =
|
20
|
+
opts.on('--not-colorize', 'Without colorize.') do
|
21
|
+
args[:colorize] = false
|
26
22
|
end
|
27
23
|
|
28
24
|
opts.on('-h', '--help', 'Prints this help') do
|
@@ -34,6 +30,7 @@ module SqRubyGrep
|
|
34
30
|
|
35
31
|
opt_parser.parse!(options)
|
36
32
|
|
33
|
+
args[:pattern] = ARGV.shift
|
37
34
|
args[:file_path] = ARGV.shift
|
38
35
|
|
39
36
|
if args[:file_path].to_s.empty? || args[:pattern].to_s.empty?
|
data/lib/sq_ruby_grep/version.rb
CHANGED
data/lib/sq_ruby_grep.rb
CHANGED
@@ -4,67 +4,57 @@ require 'sq_ruby_grep/parser'
|
|
4
4
|
|
5
5
|
module SqRubyGrep
|
6
6
|
|
7
|
-
Result = Struct.new(:match_line, :before_context, :after_context)
|
8
7
|
|
9
|
-
def self.run
|
10
|
-
|
11
|
-
|
12
|
-
resuts.each do |result|
|
13
|
-
puts '--'
|
14
|
-
puts result.before_context
|
15
|
-
puts result.match_line
|
16
|
-
puts result.after_context
|
17
|
-
end
|
18
|
-
|
19
|
-
puts '----'
|
20
|
-
puts "Line count: #{resuts.count}"
|
21
|
-
end
|
22
|
-
|
23
|
-
def self.grep(file_path:, pattern:, after_lines: 0, before_lines: 0, colorize: false)
|
24
|
-
results = []
|
25
|
-
|
26
|
-
buffer = RingBuffer.new(after_lines + before_lines + 1)
|
27
|
-
current_position = 0
|
8
|
+
def self.run(pattern:, file_path:, after_lines: 0, before_lines: 0, colorize: true)
|
9
|
+
result = []
|
28
10
|
|
29
11
|
File.open(file_path, 'r') do |file|
|
12
|
+
current_position, last_match_position = 0, 0
|
13
|
+
buffer = RingBuffer.new after_lines + before_lines
|
14
|
+
f = false
|
15
|
+
was_matches = false
|
30
16
|
file.each_line do |line|
|
31
|
-
buffer << line
|
32
17
|
current_position += 1
|
33
18
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
19
|
+
if after_lines + before_lines > 0
|
20
|
+
if line.match(pattern)
|
21
|
+
if was_matches
|
22
|
+
if (current_position - last_match_position) > after_lines + before_lines + 1
|
23
|
+
result << (colorize ? "\e[0;36m--\033[0m" : "--")
|
24
|
+
result.concat buffer.last(before_lines)
|
25
|
+
else
|
26
|
+
result.concat buffer
|
27
|
+
end
|
28
|
+
else
|
29
|
+
result.concat buffer.last(before_lines)
|
30
|
+
end
|
31
|
+
|
32
|
+
result << (colorize ? line.gsub(/(#{pattern})/, "\e[01;31m\\1\033[0m") : line)
|
33
|
+
f = false
|
34
|
+
was_matches = true
|
35
|
+
buffer.clear
|
36
|
+
last_match_position = current_position
|
37
|
+
else
|
38
|
+
buffer << line
|
39
|
+
end
|
40
|
+
|
41
|
+
if (current_position - last_match_position) == after_lines + before_lines && was_matches
|
42
|
+
result.concat buffer.shift(after_lines)
|
43
|
+
# result.concat buffer.last(before_lines)
|
44
|
+
f = true
|
45
|
+
end
|
46
|
+
else
|
47
|
+
if line.match(pattern)
|
48
|
+
result << (colorize ? line.gsub(/(#{pattern})/, "\e[01;31m\\1\033[0m") : line)
|
49
|
+
end
|
58
50
|
end
|
59
51
|
end
|
60
52
|
|
53
|
+
result.concat buffer.first(after_lines) unless f
|
61
54
|
end
|
62
55
|
|
63
|
-
|
56
|
+
result
|
64
57
|
end
|
65
58
|
|
66
|
-
def self.colorize(string, pattern)
|
67
|
-
string.gsub(/(#{pattern})/, "\e[0;33m\\1\033[0m")
|
68
|
-
end
|
69
59
|
|
70
60
|
end
|
data/spec/sq_ruby_grep_spec.rb
CHANGED
@@ -2,136 +2,74 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe SqRubyGrep do
|
4
4
|
context 'when grep file' do
|
5
|
-
let(:
|
6
|
-
let(:
|
7
|
-
let(:
|
8
|
-
let(:
|
9
|
-
let(:
|
10
|
-
let(:
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
it 'before_context is valid' do
|
17
|
-
expect(result.before_context).to eql lines[target_line - before_lines - 1 , before_lines]
|
18
|
-
end
|
19
|
-
|
20
|
-
it 'match_line is valid' do
|
21
|
-
expect(result.match_line).to eql lines[target_line - 1]
|
5
|
+
let(:path) { 'test/fixtures/text.txt' }
|
6
|
+
let(:bin_path) { File.expand_path '../../bin/sq_ruby_grep', __FILE__ }
|
7
|
+
let(:result) { Grep.new(pattern: pattern, file_path: path, before_lines: before_lines, after_lines: after_lines).run.first }
|
8
|
+
let(:pattern) { 'needle' }
|
9
|
+
let(:original_grep) { "grep #{pattern} #{path} -A #{after_lines} -B #{before_lines}" }
|
10
|
+
let(:sq_ruby_grep) { "#{bin_path} #{pattern} #{path} -A #{after_lines} -B #{before_lines} --not-colorize" }
|
11
|
+
|
12
|
+
shared_examples 'origin grep' do
|
13
|
+
it 'sq_ruby_grep vs original_grep' do
|
14
|
+
expect(`#{sq_ruby_grep}`).to eq `#{original_grep}`
|
22
15
|
end
|
23
|
-
|
24
|
-
it 'after_context is valid' do
|
25
|
-
expect(result.after_context).to eql lines[target_line, after_lines]
|
26
|
-
end
|
27
|
-
|
28
|
-
context 'when after_lines is out scope of file' do
|
29
|
-
let(:after_lines) { 30 }
|
30
|
-
|
31
|
-
it 'after_context is valid' do
|
32
|
-
expect(result.after_context).to eql lines[target_line, after_lines]
|
33
|
-
end
|
34
|
-
|
35
|
-
it 'before_context is valid' do
|
36
|
-
expect(result.before_context).to eql lines[target_line - before_lines - 1 , before_lines]
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
context 'when before_lines is out scope of file' do
|
41
|
-
let(:before_lines) { 30 }
|
42
|
-
|
43
|
-
it 'after_context is valid' do
|
44
|
-
expect(result.after_context).to eql lines[target_line, after_lines]
|
45
|
-
end
|
46
|
-
|
47
|
-
it 'before_context is valid' do
|
48
|
-
expect(result.before_context).to eql lines[0 , target_line - 1]
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
16
|
end
|
53
17
|
|
54
|
-
context 'when
|
55
|
-
let(:
|
56
|
-
|
57
|
-
it 'before_context is valid' do
|
58
|
-
expect(result.before_context).to eql lines[target_line - before_lines - 1 , before_lines]
|
59
|
-
end
|
60
|
-
|
61
|
-
it 'match_line is valid' do
|
62
|
-
expect(result.match_line).to eql lines[target_line - 1]
|
63
|
-
end
|
64
|
-
|
65
|
-
it 'after_context is valid' do
|
66
|
-
expect(result.after_context).to eql lines[target_line, after_lines]
|
67
|
-
end
|
18
|
+
context 'when has no context' do
|
19
|
+
let(:after_lines) { 0 }
|
20
|
+
let(:before_lines) { 0 }
|
68
21
|
|
69
|
-
|
70
|
-
|
22
|
+
it_behaves_like 'origin grep'
|
23
|
+
end
|
71
24
|
|
72
|
-
|
73
|
-
|
74
|
-
|
25
|
+
context 'when has no group separator' do
|
26
|
+
let(:after_lines) { 2 }
|
27
|
+
let(:before_lines) { 2 }
|
75
28
|
|
76
|
-
|
77
|
-
|
78
|
-
end
|
79
|
-
end
|
29
|
+
it_behaves_like 'origin grep'
|
30
|
+
end
|
80
31
|
|
81
|
-
|
82
|
-
|
32
|
+
context 'when before context intersects prev after context' do
|
33
|
+
let(:after_lines) { 2 }
|
34
|
+
let(:before_lines) { 3 }
|
83
35
|
|
84
|
-
|
85
|
-
|
86
|
-
end
|
36
|
+
it_behaves_like 'origin grep'
|
37
|
+
end
|
87
38
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
end
|
39
|
+
context 'when before context touch prev after context' do
|
40
|
+
let(:after_lines) { 2 }
|
41
|
+
let(:before_lines) { 3 }
|
92
42
|
|
43
|
+
it_behaves_like 'origin grep'
|
93
44
|
end
|
94
45
|
|
95
|
-
context 'when
|
96
|
-
let(:
|
97
|
-
|
98
|
-
it 'before_context is valid' do
|
99
|
-
expect(result.before_context).to eql lines[0, 1]
|
100
|
-
end
|
46
|
+
context 'when after context is out range of file' do
|
47
|
+
let(:after_lines) { 20 }
|
48
|
+
let(:before_lines) { 2 }
|
101
49
|
|
102
|
-
|
103
|
-
|
104
|
-
end
|
50
|
+
it_behaves_like 'origin grep'
|
51
|
+
end
|
105
52
|
|
106
|
-
|
107
|
-
|
108
|
-
|
53
|
+
context 'when before context is out range of file' do
|
54
|
+
let(:after_lines) { 2 }
|
55
|
+
let(:before_lines) { 20 }
|
109
56
|
|
110
|
-
|
111
|
-
|
57
|
+
it_behaves_like 'origin grep'
|
58
|
+
end
|
112
59
|
|
113
|
-
|
114
|
-
|
115
|
-
|
60
|
+
context 'when has no after context' do
|
61
|
+
let(:after_lines) { 0 }
|
62
|
+
let(:before_lines) { 1 }
|
116
63
|
|
117
|
-
|
118
|
-
|
119
|
-
end
|
120
|
-
end
|
64
|
+
it_behaves_like 'origin grep'
|
65
|
+
end
|
121
66
|
|
122
|
-
context 'when before_lines is out scope of file' do
|
123
|
-
let(:before_lines) { 30 }
|
124
67
|
|
125
|
-
|
126
|
-
|
127
|
-
|
68
|
+
context 'when has no before context' do
|
69
|
+
let(:after_lines) { 1 }
|
70
|
+
let(:before_lines) { 0 }
|
128
71
|
|
129
|
-
|
130
|
-
expect(result.before_context).to eql lines[0 , target_line - 1]
|
131
|
-
end
|
132
|
-
end
|
72
|
+
it_behaves_like 'origin grep'
|
133
73
|
end
|
134
|
-
|
135
74
|
end
|
136
|
-
|
137
75
|
end
|
data/sq_ruby_grep.gemspec
CHANGED
@@ -4,22 +4,23 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
require 'sq_ruby_grep/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
7
|
+
spec.name = 'sq_ruby_grep'
|
8
8
|
spec.version = SqRubyGrep::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
9
|
+
spec.authors = ['coolelvis']
|
10
|
+
spec.email = ['elvisplus2@gmail.com']
|
11
11
|
spec.summary = %q{It just the training project for SQ interview.}
|
12
12
|
spec.description = %q{It just the training project for SQ interview.}
|
13
|
-
spec.homepage =
|
14
|
-
spec.license =
|
13
|
+
spec.homepage = 'https://github.com/CoolElvis/ruby_grep.git'
|
14
|
+
spec.license = 'MIT'
|
15
15
|
|
16
16
|
spec.files = `git ls-files -z`.split("\x0")
|
17
17
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
-
spec.require_paths = [
|
19
|
+
spec.require_paths = ['lib']
|
20
20
|
|
21
21
|
spec.required_ruby_version = '~> 2.1.3'
|
22
22
|
|
23
|
-
spec.add_development_dependency
|
24
|
-
spec.add_development_dependency
|
23
|
+
spec.add_development_dependency 'bundler', '~> 1.7'
|
24
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
25
|
+
spec.add_development_dependency 'rspec', '~> 3.2.2'
|
25
26
|
end
|
data/test/fixtures/text.txt
CHANGED
@@ -3,11 +3,11 @@ line 2 Vivamus id tortor non metus ornare rutrum.
|
|
3
3
|
line 3 Curabitur vel ex id tellus faucibus lobortis.
|
4
4
|
line 4 Cras aliquet orci at accumsan rhoncus.
|
5
5
|
line 5 Cras at justo ut risus rutrum posuere.
|
6
|
-
line 6 Mauris sit amet eros sed risus accumsan vulputate.
|
7
|
-
line 7 Aenean id nulla tempus, blandit nunc lacinia, tincidunt odio.
|
8
|
-
line 8 Vestibulum non est eu tortor ullamcorper scelerisque.
|
6
|
+
needle line 6 Mauris sit amet eros sed risus accumsan vulputate. non
|
7
|
+
needle line 7 Aenean id nulla tempus, blandit nunc lacinia, tincidunt odio.
|
8
|
+
line 8 Vestibulum non est eu non tortor ullamcorper scelerisque.
|
9
9
|
line 9 Duis pharetra nisl at nibh feugiat varius.
|
10
|
-
line 10 Praesent vitae magna feugiat lectus aliquet hendrerit ac ut urna.
|
11
|
-
line 11 Vivamus sed arcu sit amet orci auctor maximus.
|
10
|
+
line 10 Praesent vitae magna feugiat lectus aliquet hendrerit ac ut urna .
|
11
|
+
needle line 11 Vivamus sed arcu sit amet orci auctor maximus.
|
12
12
|
line 12 Pellentesque vitae purus sagittis, ultrices urna at, porttitor eros.
|
13
13
|
line 13 Curabitur id metus egestas, bibendum nisi id, aliquet lacus.
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sq_ruby_grep
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- coolelvis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-04-
|
11
|
+
date: 2015-04-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 3.2.2
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.2.2
|
41
55
|
description: It just the training project for SQ interview.
|
42
56
|
email:
|
43
57
|
- elvisplus2@gmail.com
|
@@ -61,7 +75,6 @@ files:
|
|
61
75
|
- spec/spec_helper.rb
|
62
76
|
- spec/sq_ruby_grep_spec.rb
|
63
77
|
- sq_ruby_grep.gemspec
|
64
|
-
- test/fixtures/lines.txt
|
65
78
|
- test/fixtures/text.txt
|
66
79
|
homepage: https://github.com/CoolElvis/ruby_grep.git
|
67
80
|
licenses:
|
@@ -90,5 +103,4 @@ summary: It just the training project for SQ interview.
|
|
90
103
|
test_files:
|
91
104
|
- spec/spec_helper.rb
|
92
105
|
- spec/sq_ruby_grep_spec.rb
|
93
|
-
- test/fixtures/lines.txt
|
94
106
|
- test/fixtures/text.txt
|