ltsview 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ltsview.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Naoto SHINGAKI
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,55 @@
1
+ # Ltsview
2
+
3
+ Ltsview - Labeled Tab Separated Value manipulator Viewer
4
+
5
+ ## Requirements
6
+
7
+ * OSX or Ubuntu
8
+ * Ruby 1.9.3 or later
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'ltsview'
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install ltsview
23
+
24
+ ## Usage
25
+
26
+ $ cat logfile.ltsv | ltsview
27
+
28
+ key select
29
+
30
+ $ cat logfile.ltsv | ltsview -k firstkey,therdkey
31
+
32
+ ignore key select
33
+
34
+ $ cat logfile.ltsv | ltsview -i firstkey,secondkey
35
+
36
+ regex key select
37
+
38
+ $ cat logfile.ltsv | ltsview -r key:regex
39
+
40
+ load file
41
+
42
+ $ ltsview -f logfile.ltsv
43
+
44
+ ### Option
45
+
46
+ * `-k , --keys VAL` to display keys select
47
+ * `-i, --ignore-key VAL` to ignore keys select display
48
+
49
+ ## Contributing
50
+
51
+ 1. Fork it
52
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
53
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
54
+ 4. Push to the branch (`git push origin my-new-feature`)
55
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env rake
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.pattern = 'spec/**/*_spec.rb'
7
+ t.libs.push 'spec'
8
+ t.libs.push 'lib'
9
+ end
10
+
data/bin/ltsview ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'ltsview'
4
+
5
+ Ltsview::Parse.new(ARGV).print
@@ -0,0 +1,75 @@
1
+ module Ltsview
2
+ class Parse
3
+
4
+ def initialize(options)
5
+ @color = true
6
+ option_parse options
7
+ end
8
+
9
+ def print
10
+ file_or_stdin do |ltsv|
11
+ filter(ltsv) do |key, val|
12
+ puts color key, val
13
+ end
14
+ end
15
+ end
16
+
17
+ private
18
+ def option_parse(options)
19
+ option = OptionParser.new(options)
20
+ option.on('-f', '--file VAL'){ |v| @file = v }
21
+ option.on('-k', '--keys VAL'){ |v| @keys = v.split(',') }
22
+ option.on('-i', '--ignore-key VAL'){ |v| @ignore_key = v.split(',') }
23
+ option.on('-r', '--regexp key:VAL', /\A([^:]+):(.*)/){ |_, k,v| @regex = {key: k.to_sym, value: v} }
24
+ option.on('--[no-]colors'){ |v| @color = v }
25
+ option.permute!(options)
26
+ end
27
+
28
+ def file_or_stdin(&block)
29
+ if !@file.nil?
30
+ file_load(@file, &block)
31
+ else
32
+ stdin_load(&block)
33
+ end
34
+ end
35
+
36
+ def stdin_load
37
+ $stdin.each_line do |line|
38
+ yield LTSV.parse(line.chomp)
39
+ end
40
+ end
41
+
42
+ def file_load(file_path)
43
+ stream = File.open(file_path, 'r')
44
+ LTSV.parse(stream).each do |line|
45
+ yield line
46
+ end
47
+ end
48
+
49
+ def filter(ltsv)
50
+ matcher(ltsv).each do |key, val|
51
+ yield key, val if keys?(key) && !ignore?(key)
52
+ end
53
+ end
54
+
55
+ def matcher(ltsv)
56
+ if !@regex.nil? && ltsv[@regex[:key]] !~ /(#{@regex[:value]})/
57
+ ltsv = {}
58
+ end
59
+ ltsv
60
+ end
61
+
62
+ def keys?(key)
63
+ @keys.nil? || @keys.include?(key.to_s)
64
+ end
65
+
66
+ def ignore?(key)
67
+ !@ignore_key.nil? && @ignore_key.include?(key.to_s)
68
+ end
69
+
70
+ def color(key, val)
71
+ @color ? "#{key.to_s.magenta}: #{val.cyan}" : "#{key}: #{val}"
72
+ end
73
+
74
+ end
75
+ end
@@ -0,0 +1,3 @@
1
+ module Ltsview
2
+ VERSION = "0.0.1"
3
+ end
data/lib/ltsview.rb ADDED
@@ -0,0 +1,13 @@
1
+ require "ltsview/version"
2
+
3
+ module Ltsview
4
+ # Your code goes here...
5
+
6
+ require 'ltsv'
7
+ require 'colored'
8
+ require 'optparse'
9
+ require 'yaml'
10
+ require 'pp'
11
+
12
+ require 'ltsview/parse'
13
+ end
data/ltsview.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ltsview/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "ltsview"
8
+ gem.version = Ltsview::VERSION
9
+ gem.authors = ["Naoto SHINGAKI"]
10
+ gem.email = ["n.shingaki@gmail.com"]
11
+ gem.description = %q{Ltsview - Labeled Tab Separated Value manipulator Viewer}
12
+ gem.summary = %q{Ltsview - Labeled Tab Separated Value manipulator Viewer}
13
+ gem.homepage = "https://github.com/naoto/ltsview"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_runtime_dependency 'ltsv'
21
+ gem.add_runtime_dependency 'colored'
22
+
23
+ gem.add_development_dependency 'rake'
24
+ gem.add_development_dependency 'rspec'
25
+
26
+ end
@@ -0,0 +1,89 @@
1
+ require 'spec_helper'
2
+ require 'ltsview/parse'
3
+
4
+ describe Ltsview::Parse do
5
+
6
+ describe 'when encode stdin text line' do
7
+ it 'should get yaml' do
8
+ parse = Ltsview::Parse.new(ARGV)
9
+ capture(:stdout) {
10
+ $stdin = StringIO.new
11
+ $stdin << "hoge:fuga hago\tfoo:barbaz\n"
12
+ $stdin.rewind
13
+ parse.print
14
+ }.must_equal("\e[35mhoge\e[0m: \e[36mfuga hago\e[0m\n\e[35mfoo\e[0m: \e[36mbarbaz\e[0m\n")
15
+ end
16
+ end
17
+
18
+ describe 'when encode ltsv format file' do
19
+ it 'should load ltsv' do
20
+ parse = Ltsview::Parse.new(['-f','spec/test.ltsv'])
21
+ capture(:stdout){
22
+ parse.print
23
+ }.must_equal("\e[35mhoge\e[0m: \e[36mfuga hago\e[0m\n\e[35mfoo\e[0m: \e[36mbarbaz\e[0m\n")
24
+ end
25
+ end
26
+
27
+
28
+ describe 'when key select' do
29
+ it 'should by key select' do
30
+ parse = Ltsview::Parse.new(['-k','foo'])
31
+ capture(:stdout){
32
+ $stdin = StringIO.new
33
+ $stdin << "hoge:fuga hago\tfoo:barbaz\n"
34
+ $stdin.rewind
35
+ parse.print
36
+ }.must_equal("\e[35mfoo\e[0m: \e[36mbarbaz\e[0m\n")
37
+ end
38
+ end
39
+
40
+ describe 'when ignore key select' do
41
+ it 'should by igonore key select' do
42
+ parse = Ltsview::Parse.new(['-i','foo'])
43
+ capture(:stdout){
44
+ $stdin = StringIO.new
45
+ $stdin << "hoge:fuga hago\tfoo:barbaz\n"
46
+ $stdin.rewind
47
+ parse.print
48
+ }.must_equal("\e[35mhoge\e[0m: \e[36mfuga hago\e[0m\n")
49
+ end
50
+ end
51
+
52
+ describe 'when regex matche' do
53
+ it 'should by regex matcher' do
54
+ parse = Ltsview::Parse.new(['-r', 'foo:^fuga'])
55
+ capture(:stdout){
56
+ $stdin = StringIO.new
57
+ $stdin << "hago:fuga hago\tfoo:barbaz\n"
58
+ $stdin << "hago:fuga2 hago\tfoo:fugabarbaz\n"
59
+ $stdin.rewind
60
+ parse.print
61
+ }.must_equal("\e[35mhago\e[0m: \e[36mfuga2 hago\e[0m\n\e[35mfoo\e[0m: \e[36mfugabarbaz\e[0m\n")
62
+ end
63
+ end
64
+
65
+ describe 'when color mode' do
66
+ it 'should by default color mode on' do
67
+ parse = Ltsview::Parse.new(ARGV)
68
+ capture(:stdout){
69
+ $stdin = StringIO.new
70
+ $stdin << "hoge:fuga hago\tfoo:barbaz\n"
71
+ $stdin.rewind
72
+ parse.print
73
+ }.must_equal("\e[35mhoge\e[0m: \e[36mfuga hago\e[0m\n\e[35mfoo\e[0m: \e[36mbarbaz\e[0m\n")
74
+ end
75
+
76
+ it 'should by color mode off' do
77
+ parse = Ltsview::Parse.new(['--no-colors'])
78
+ capture(:stdout){
79
+ $stdin = StringIO.new
80
+ $stdin << "hoge:fuga hago\tfoo:barbaz\n"
81
+ $stdin.rewind
82
+ parse.print
83
+ }.must_equal("hoge: fuga hago\nfoo: barbaz\n")
84
+ end
85
+
86
+ end
87
+
88
+ end
89
+
@@ -0,0 +1,21 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'minitest/unit'
5
+ require 'minitest/autorun'
6
+
7
+ require 'ltsview'
8
+
9
+ def capture(stream)
10
+ begin
11
+ stream = stream.to_s
12
+ eval "$#{stream} = StringIO.new"
13
+ yield
14
+ result = eval("$#{stream}").string
15
+ rescue => e
16
+ puts "Error : #{e}"
17
+ ensure
18
+ eval "$#{stream} = #{stream.upcase}"
19
+ end
20
+ result
21
+ end
data/spec/test.ltsv ADDED
@@ -0,0 +1,2 @@
1
+ hoge:fuga hago foo:barbaz
2
+
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ltsview
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Naoto SHINGAKI
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: ltsv
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: colored
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Ltsview - Labeled Tab Separated Value manipulator Viewer
79
+ email:
80
+ - n.shingaki@gmail.com
81
+ executables:
82
+ - ltsview
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - .gitignore
87
+ - Gemfile
88
+ - LICENSE.txt
89
+ - README.md
90
+ - Rakefile
91
+ - bin/ltsview
92
+ - lib/ltsview.rb
93
+ - lib/ltsview/parse.rb
94
+ - lib/ltsview/version.rb
95
+ - ltsview.gemspec
96
+ - spec/parse_spec.rb
97
+ - spec/spec_helper.rb
98
+ - spec/test.ltsv
99
+ homepage: https://github.com/naoto/ltsview
100
+ licenses: []
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ! '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 1.8.23
120
+ signing_key:
121
+ specification_version: 3
122
+ summary: Ltsview - Labeled Tab Separated Value manipulator Viewer
123
+ test_files:
124
+ - spec/parse_spec.rb
125
+ - spec/spec_helper.rb
126
+ - spec/test.ltsv