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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7ecbae1d7f5407a9164e0945f0b9a4c2597abb97
4
- data.tar.gz: 43c21842405cf2548d04417a3481d2b2ddba8b57
3
+ metadata.gz: 2688d28246d3cb3f85bb521c7681bc7eba93e37c
4
+ data.tar.gz: 93a5b5a2a4922e6f85f478493fe16d21afb0ec8a
5
5
  SHA512:
6
- metadata.gz: 9822f2472d575e7caa1ccb8daa64fe5f3abed5ef394598aef434016452006ecac6cd63bbada25acbd8cbbe25a6cb851d74a8dd672b7962c78a777dc372886e2f
7
- data.tar.gz: f49524ba86648e4fb8797e5d8be4572b6cd154ae71cf60cf9284c597c385300667ff7fef89f68c3e7eb8b40374e16b0e9bc16cd15f8c4976018543d6753a6f5c
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 [FILE] [options]
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
- -c, --colorize Colorize matches
32
-
26
+
27
+ --not-colorize Without colorize
28
+
33
29
  -h, --help Prints this help
34
30
 
data/bin/sq_ruby_grep CHANGED
@@ -3,5 +3,6 @@
3
3
  lib = File.expand_path('../../lib', __FILE__)
4
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
5
  require 'sq_ruby_grep'
6
+ require 'parser'
6
7
 
7
- SqRubyGrep.run
8
+ puts SqRubyGrep.run SqRubyGrep::Parser.parse(ARGV)
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.grep(file_path:'test/fixtures/text.txt', pattern: /non/, after_lines: 2, before_lines: 2, colorize: true).each do |result|
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)
@@ -3,16 +3,12 @@ require 'optparse'
3
3
  module SqRubyGrep
4
4
  class Parser
5
5
  def self.parse(options)
6
- args = Hash.new
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('-c', '--colorize', 'Colorize matches') do
25
- args[:colorize] = true
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?
@@ -1,3 +1,3 @@
1
1
  module SqRubyGrep
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
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
- resuts = grep(Parser.parse(ARGV))
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
- offset = buffer.size - after_lines - 1
35
-
36
- if current_position > after_lines && buffer[offset].match(pattern)
37
- match_line = buffer[offset]
38
- match_line = self.colorize(match_line, pattern) if colorize
39
-
40
- result = Result.new(match_line, buffer[0, offset], buffer[offset + 1, after_lines])
41
-
42
- results << result
43
- end
44
- end
45
-
46
- offset = (buffer.size - after_lines) > 0 ? (buffer.size - after_lines) : 0
47
-
48
- buffer[offset, after_lines].each_with_index do |line, i|
49
- if line.match(pattern)
50
- before_offset = (offset + i) > before_lines ? (offset + i - before_lines) : 0
51
- _before_lines = (offset + i) > before_lines ? before_lines : (offset + i)
52
-
53
- line = colorize(line, pattern) if colorize
54
-
55
- result = Result.new(line, buffer[before_offset, _before_lines], buffer[i + offset + 1, after_lines])
56
-
57
- results << result
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
- results
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
@@ -2,136 +2,74 @@ require 'spec_helper'
2
2
 
3
3
  describe SqRubyGrep do
4
4
  context 'when grep file' do
5
- let(:lines) { lines = File.readlines path }
6
- let(:path) { 'test/fixtures/lines.txt' }
7
- let(:after_lines) { 3 }
8
- let(:before_lines) { 3 }
9
- let(:result) { SqRubyGrep.grep(file_path:path, pattern: pattern, before_lines: before_lines, after_lines: after_lines).first }
10
- let(:pattern) { /#{target_line} line/ }
11
-
12
- context 'when the target line at the middle' do
13
-
14
- let(:target_line) { 6 }
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 the target line at the end' do
55
- let(:target_line) { 12 }
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
- context 'when after_lines is out scope of file' do
70
- let(:after_lines) { 30 }
22
+ it_behaves_like 'origin grep'
23
+ end
71
24
 
72
- it 'after_context is valid' do
73
- expect(result.after_context).to eql lines[target_line, after_lines]
74
- end
25
+ context 'when has no group separator' do
26
+ let(:after_lines) { 2 }
27
+ let(:before_lines) { 2 }
75
28
 
76
- it 'before_context is valid' do
77
- expect(result.before_context).to eql lines[target_line - before_lines - 1 , before_lines]
78
- end
79
- end
29
+ it_behaves_like 'origin grep'
30
+ end
80
31
 
81
- context 'when before_lines is out scope of file' do
82
- let(:before_lines) { 30 }
32
+ context 'when before context intersects prev after context' do
33
+ let(:after_lines) { 2 }
34
+ let(:before_lines) { 3 }
83
35
 
84
- it 'after_context is valid' do
85
- expect(result.after_context).to eql lines[target_line, after_lines]
86
- end
36
+ it_behaves_like 'origin grep'
37
+ end
87
38
 
88
- it 'before_context is valid' do
89
- expect(result.before_context).to eql lines[0 , target_line - 1]
90
- end
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 the target line at the beginning ' do
96
- let(:target_line) { 2 }
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
- it 'match_line is valid' do
103
- expect(result.match_line).to eql lines[target_line - 1]
104
- end
50
+ it_behaves_like 'origin grep'
51
+ end
105
52
 
106
- it 'after_context is valid' do
107
- expect(result.after_context).to eql lines[target_line, after_lines]
108
- end
53
+ context 'when before context is out range of file' do
54
+ let(:after_lines) { 2 }
55
+ let(:before_lines) { 20 }
109
56
 
110
- context 'when after_lines is out scope of file' do
111
- let(:after_lines) { 30 }
57
+ it_behaves_like 'origin grep'
58
+ end
112
59
 
113
- it 'after_context is valid' do
114
- expect(result.after_context).to eql lines[target_line, after_lines]
115
- end
60
+ context 'when has no after context' do
61
+ let(:after_lines) { 0 }
62
+ let(:before_lines) { 1 }
116
63
 
117
- it 'before_context is valid' do
118
- expect(result.before_context).to eql lines[0, target_line - 1]
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
- it 'after_context is valid' do
126
- expect(result.after_context).to eql lines[target_line, after_lines]
127
- end
68
+ context 'when has no before context' do
69
+ let(:after_lines) { 1 }
70
+ let(:before_lines) { 0 }
128
71
 
129
- it 'before_context is valid' do
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 = "sq_ruby_grep"
7
+ spec.name = 'sq_ruby_grep'
8
8
  spec.version = SqRubyGrep::VERSION
9
- spec.authors = ["coolelvis"]
10
- spec.email = ["elvisplus2@gmail.com"]
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 = "https://github.com/CoolElvis/ruby_grep.git"
14
- spec.license = "MIT"
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 = ["lib"]
19
+ spec.require_paths = ['lib']
20
20
 
21
21
  spec.required_ruby_version = '~> 2.1.3'
22
22
 
23
- spec.add_development_dependency "bundler", "~> 1.7"
24
- spec.add_development_dependency "rake", "~> 10.0"
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
@@ -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.3
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-02 00:00:00.000000000 Z
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
@@ -1,13 +0,0 @@
1
- 1 line
2
- 2 line
3
- 3 line
4
- 4 line
5
- 5 line
6
- 6 line
7
- 7 line
8
- 8 line
9
- 9 line
10
- 10 line
11
- 11 line
12
- 12 line
13
- 13 line