grepity 0.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 +7 -0
- data/.gitignore +22 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +22 -0
- data/README.md +41 -0
- data/Rakefile +6 -0
- data/bin/grepity +4 -0
- data/example.jpg +0 -0
- data/grepity.gemspec +23 -0
- data/lib/grepity/line.rb +27 -0
- data/lib/grepity/option.rb +31 -0
- data/lib/grepity/target_file.rb +12 -0
- data/lib/grepity/target_text.rb +18 -0
- data/lib/grepity/version.rb +3 -0
- data/lib/grepity.rb +14 -0
- data/spec/fixtures/test_file.txt +7 -0
- data/spec/grepity/line_spec.rb +16 -0
- data/spec/grepity/option_spec.rb +25 -0
- data/spec/grepity/target_file_spec.rb +11 -0
- data/spec/grepity/target_text_spec.rb +26 -0
- data/spec/grepity_spec.rb +64 -0
- data/spec/support/spec_helper.rb +12 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0d8d9720e0bca498ba35626106330682bed7b446
|
4
|
+
data.tar.gz: 646a195fc36ec8d8390328c844ad8c9bb1365842
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e4e3b8536562635b669b6f3d754164ba29816076372c5abd9dc398b83589ad34cbe9b74482154025ab1ee9ab939bbe599d3076bcfa8eb8eb003baa03afadd70e
|
7
|
+
data.tar.gz: 3c7422947acba1f799bf3352cf112840a2090c7b6c20bec96db443d25d9b3af2ba9d60ac2a7b13453456bc8fd37da0f779b1a28db21a7e8fbecca3b5a9c7296c
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Kirill Isakov
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# Grepity
|
2
|
+
|
3
|
+
Small analog for grep utility
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
Install it from RubyGems.org:
|
7
|
+
|
8
|
+
$ gem install grepity
|
9
|
+
|
10
|
+
Or install it from GitHub.com:
|
11
|
+
|
12
|
+
$ git clone git@github.com:kisakov/grepity.git
|
13
|
+
$ rake install
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
Run `grepity` for grep text from file
|
18
|
+
|
19
|
+
Usage: grepity text file [OPTIONS]
|
20
|
+
Example: grepity localhost /etc/hosts
|
21
|
+
|
22
|
+
Common options:
|
23
|
+
-i, --ignorecase ignore case sensivity
|
24
|
+
-w, --word match only full wordmatch
|
25
|
+
-l, --line show line numbers
|
26
|
+
-h, --help Help screen
|
27
|
+
|
28
|
+

|
29
|
+
|
30
|
+
## Tests
|
31
|
+
|
32
|
+
Use `rake test` to run tests
|
33
|
+
|
34
|
+
## Contributing
|
35
|
+
|
36
|
+
1. Fork it ( https://github.com/kisakov/grepity/fork )
|
37
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
38
|
+
3. Ensure all tests passed (`rake test`)
|
39
|
+
4. Commit your changes (`git commit -am 'Add some feature'`)
|
40
|
+
5. Push to the branch (`git push origin my-new-feature`)
|
41
|
+
6. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/grepity
ADDED
data/example.jpg
ADDED
Binary file
|
data/grepity.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'grepity/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "grepity"
|
8
|
+
spec.version = Grepity::VERSION
|
9
|
+
spec.authors = ["Kirill Isakov"]
|
10
|
+
spec.email = ["isakov90@gmail.com"]
|
11
|
+
spec.summary = %q{Grep analog}
|
12
|
+
spec.description = %q{Small analog for grep utility}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
end
|
data/lib/grepity/line.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'colorize'
|
2
|
+
|
3
|
+
module Grepity
|
4
|
+
class Line
|
5
|
+
attr_reader :contents, :number
|
6
|
+
|
7
|
+
def initialize(contents, number)
|
8
|
+
@contents, @number = contents, number + 1
|
9
|
+
end
|
10
|
+
|
11
|
+
def result(text, options = {})
|
12
|
+
return colorized_line_with_text(number, text) if options[:line]
|
13
|
+
colorized_text(text)
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def colorized_text(text)
|
19
|
+
matched_text = @contents.match(text)[0].yellow
|
20
|
+
@contents.gsub(text, matched_text)
|
21
|
+
end
|
22
|
+
|
23
|
+
def colorized_line_with_text(number, text)
|
24
|
+
"#{number.to_s.green}: #{colorized_text(text)}"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
|
3
|
+
module Grepity
|
4
|
+
class Option
|
5
|
+
def self.parse
|
6
|
+
options = {}
|
7
|
+
OptionParser.new do |opts|
|
8
|
+
opts.banner = "Usage: #{$0} text file [OPTIONS]"
|
9
|
+
|
10
|
+
opts.on('-i', '--ignorecase', 'ignore case sensivity') do
|
11
|
+
options[:ignorecase] = Regexp::IGNORECASE
|
12
|
+
end
|
13
|
+
|
14
|
+
opts.on('-w', '--word', 'match only full word') do
|
15
|
+
options[:word] = true
|
16
|
+
end
|
17
|
+
|
18
|
+
opts.on('-l', '--line', 'show line numbers') do
|
19
|
+
options[:line] = true
|
20
|
+
end
|
21
|
+
|
22
|
+
opts.on_tail('-h', '--help', 'Help screen') do
|
23
|
+
puts opts
|
24
|
+
exit
|
25
|
+
end
|
26
|
+
end.parse!
|
27
|
+
|
28
|
+
options
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module Grepity
|
2
|
+
class TargetFile
|
3
|
+
def initialize(file_name)
|
4
|
+
@lines = File.read(file_name).split("\n")
|
5
|
+
@lines.map! { |line| Line.new(line, @lines.index(line)) }
|
6
|
+
end
|
7
|
+
|
8
|
+
def match(text)
|
9
|
+
@lines.select { |line| line.contents.match(text) }
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Grepity
|
2
|
+
class TargetText
|
3
|
+
attr_reader :value
|
4
|
+
|
5
|
+
def initialize(text, options = {})
|
6
|
+
@options = options
|
7
|
+
@value = create_regexp(text)
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def create_regexp(text)
|
13
|
+
text = "\\b#{text}\\b" if @options[:word]
|
14
|
+
Regexp.new(text, @options[:ignorecase])
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
data/lib/grepity.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'grepity/option'
|
2
|
+
require 'grepity/target_text'
|
3
|
+
require 'grepity/target_file'
|
4
|
+
require 'grepity/line'
|
5
|
+
|
6
|
+
module Grepity
|
7
|
+
def self.run!
|
8
|
+
options = Option.parse
|
9
|
+
text = TargetText.new(ARGV[0], options)
|
10
|
+
TargetFile.new(ARGV[1]).match(text.value).each do |line|
|
11
|
+
puts line.result(text.value, options)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require_relative '../support/spec_helper'
|
2
|
+
require 'colorize'
|
3
|
+
|
4
|
+
describe Grepity::Line do
|
5
|
+
subject { Grepity::Line.new('Social quantum', 12) }
|
6
|
+
|
7
|
+
it 'prints result string without options' do
|
8
|
+
subject.result('quantum')
|
9
|
+
.must_equal "Social #{colorized_word('quantum').rstrip}"
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'prints result string with numbers' do
|
13
|
+
subject.result('Social quantum', line: true)
|
14
|
+
.must_equal "#{colorized_line_with_word(13, 'Social quantum').rstrip}"
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require_relative '../support/spec_helper'
|
2
|
+
|
3
|
+
def option(*options)
|
4
|
+
ARGV.clear
|
5
|
+
options.each { |option| ARGV << option }
|
6
|
+
Grepity::Option.parse
|
7
|
+
end
|
8
|
+
|
9
|
+
describe Grepity::Option do
|
10
|
+
it 'must be instance of Hash' do
|
11
|
+
option.must_be_instance_of Hash
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'ignorecase must be Regexp::IGNORECASE with -i' do
|
15
|
+
option('-i').must_equal ignorecase: Regexp::IGNORECASE
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'word must be true with -w' do
|
19
|
+
option('-w').must_equal word: true
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'line must be true with -l' do
|
23
|
+
option('-l').must_equal line: true
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require_relative '../support/spec_helper'
|
2
|
+
|
3
|
+
describe Grepity::TargetFile do
|
4
|
+
subject { Grepity::TargetFile.new('spec/fixtures/test_file.txt') }
|
5
|
+
|
6
|
+
it 'must be instance of Array with Grepity::Line' do
|
7
|
+
subj = subject.match('Quantum')
|
8
|
+
subj.must_be_instance_of Array
|
9
|
+
subj.first.must_be_instance_of Grepity::Line
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require_relative '../support/spec_helper'
|
2
|
+
|
3
|
+
def text(text, options = {})
|
4
|
+
Grepity::TargetText.new(text, options)
|
5
|
+
end
|
6
|
+
|
7
|
+
describe Grepity::TargetText do
|
8
|
+
let(:word) { 'rails' }
|
9
|
+
|
10
|
+
it 'must be instance of Regexp' do
|
11
|
+
text(word).value.must_be_instance_of Regexp
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'must wright Regexp without options' do
|
15
|
+
text(word).value.must_equal Regexp.new(word)
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'must wright Regexp without option -i' do
|
19
|
+
text(word, ignorecase: Regexp::IGNORECASE).value
|
20
|
+
.must_equal Regexp.new(word, Regexp::IGNORECASE)
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'must wright Regexp without option -w' do
|
24
|
+
text(word, word: true).value.must_equal Regexp.new("\\b#{word}\\b")
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require_relative './support/spec_helper'
|
2
|
+
require 'fileutils'
|
3
|
+
require 'colorize'
|
4
|
+
|
5
|
+
def flag(set)
|
6
|
+
ARGV.clear
|
7
|
+
@exit = catch(:system_exit) { command_line(set) }
|
8
|
+
end
|
9
|
+
|
10
|
+
def command_line(options)
|
11
|
+
options.each { |opt| ARGV << opt }
|
12
|
+
def subject.exit(*args)
|
13
|
+
throw(:system_exit, :exit)
|
14
|
+
end
|
15
|
+
subject
|
16
|
+
end
|
17
|
+
|
18
|
+
def capture_io
|
19
|
+
require 'stringio'
|
20
|
+
|
21
|
+
orig_stdout, orig_stderr = $stdout, $stderr
|
22
|
+
captured_stdout, captured_stderr = StringIO.new, StringIO.new
|
23
|
+
$stdout, $stderr = captured_stdout, captured_stderr
|
24
|
+
|
25
|
+
yield
|
26
|
+
|
27
|
+
return captured_stdout.string, captured_stderr.string
|
28
|
+
ensure
|
29
|
+
$stdout = orig_stdout
|
30
|
+
$stderr = orig_stderr
|
31
|
+
end
|
32
|
+
|
33
|
+
def out(*args)
|
34
|
+
capture_io { flag args }[0]
|
35
|
+
end
|
36
|
+
|
37
|
+
describe Grepity do
|
38
|
+
subject { Grepity::run! }
|
39
|
+
let(:file) { 'spec/fixtures/test_file.txt' }
|
40
|
+
|
41
|
+
it 'prints empty string without options' do
|
42
|
+
out('quantum', file).must_be_empty
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'prints result with -i' do
|
46
|
+
out('quantum', file, '-i').must_equal "Social #{colorized_word('Quantum')}"
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'prints one line without options' do
|
50
|
+
out('burg', file).must_equal "Saint Peters#{colorized_word('burg')}"
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'prints two lines with -i' do
|
54
|
+
out('burg', file, '-i').must_equal "Saint Peters#{colorized_word('burg')}#{colorized_word('Burg')}"
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'prints one line with -i -w' do
|
58
|
+
out('burg', file, '-i', '-w').must_equal colorized_word('Burg')
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'prints one numbered line with -i -w -l' do
|
62
|
+
out('burg', file, '-i', '-w', '-l').must_equal colorized_line_with_word(6, 'Burg')
|
63
|
+
end
|
64
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: grepity
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kirill Isakov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-07-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: Small analog for grep utility
|
42
|
+
email:
|
43
|
+
- isakov90@gmail.com
|
44
|
+
executables:
|
45
|
+
- grepity
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- .gitignore
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- bin/grepity
|
55
|
+
- example.jpg
|
56
|
+
- grepity.gemspec
|
57
|
+
- lib/grepity.rb
|
58
|
+
- lib/grepity/line.rb
|
59
|
+
- lib/grepity/option.rb
|
60
|
+
- lib/grepity/target_file.rb
|
61
|
+
- lib/grepity/target_text.rb
|
62
|
+
- lib/grepity/version.rb
|
63
|
+
- spec/fixtures/test_file.txt
|
64
|
+
- spec/grepity/line_spec.rb
|
65
|
+
- spec/grepity/option_spec.rb
|
66
|
+
- spec/grepity/target_file_spec.rb
|
67
|
+
- spec/grepity/target_text_spec.rb
|
68
|
+
- spec/grepity_spec.rb
|
69
|
+
- spec/support/spec_helper.rb
|
70
|
+
homepage: ''
|
71
|
+
licenses:
|
72
|
+
- MIT
|
73
|
+
metadata: {}
|
74
|
+
post_install_message:
|
75
|
+
rdoc_options: []
|
76
|
+
require_paths:
|
77
|
+
- lib
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
requirements: []
|
89
|
+
rubyforge_project:
|
90
|
+
rubygems_version: 2.2.2
|
91
|
+
signing_key:
|
92
|
+
specification_version: 4
|
93
|
+
summary: Grep analog
|
94
|
+
test_files:
|
95
|
+
- spec/fixtures/test_file.txt
|
96
|
+
- spec/grepity/line_spec.rb
|
97
|
+
- spec/grepity/option_spec.rb
|
98
|
+
- spec/grepity/target_file_spec.rb
|
99
|
+
- spec/grepity/target_text_spec.rb
|
100
|
+
- spec/grepity_spec.rb
|
101
|
+
- spec/support/spec_helper.rb
|