ltsview 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,7 +1,12 @@
1
- # Ltsview
1
+ Ltsview
2
+ =================================================================
2
3
 
3
4
  Ltsview - Labeled Tab Separated Value manipulator Viewer
4
5
 
6
+ [![Gem Version](https://badge.fury.io/rb/ltsview.png)](https://badge.fury.io/rb/ltsview)
7
+ [![Code CLimate](https://codeclimate.com/github/naoto/ltsview.png)](https://codeclimate.com/github/naoto/ltsview)
8
+
9
+
5
10
  ## Requirements
6
11
 
7
12
  * OSX or Ubuntu
data/lib/ltsview/parse.rb CHANGED
@@ -2,32 +2,39 @@ module Ltsview
2
2
  class Parse
3
3
 
4
4
  def initialize(options)
5
- @color = true
5
+ @options = {
6
+ mode: :yaml,
7
+ color: true,
8
+ file: nil,
9
+ keys: nil,
10
+ ignore_key: nil,
11
+ regex: nil
12
+ }
6
13
  option_parse options
7
14
  end
8
15
 
9
16
  def print
10
17
  file_or_stdin do |ltsv|
11
- filter(ltsv) do |key, val|
12
- puts color key, val
13
- end
18
+ puts "#{tag}#{formatter(filter(ltsv))}"
14
19
  end
15
20
  end
16
21
 
17
22
  private
18
23
  def option_parse(options)
19
24
  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.on('-f', '--file VAL'){ |v| @options[:file] = v }
26
+ option.on('-k', '--keys VAL'){ |v| @options[:keys] = v.split(',') }
27
+ option.on('-i', '--ignore-key VAL'){ |v| @options[:ignore_key] = v.split(',') }
28
+ option.on('-r', '--regexp key:VAL', /\A([^:]+):(.*)/){ |_, k,v| @options[:regex] = {key: k.to_sym, value: v} }
29
+ option.on('-j', '--json') { |v| @options[:mode] = :json }
30
+ option.on('-t', '--tag VAL'){ |v| @options[:tag] = v }
31
+ option.on('--[no-]colors'){ |v| @options[:color] = v }
25
32
  option.permute!(options)
26
33
  end
27
34
 
28
35
  def file_or_stdin(&block)
29
- if !@file.nil?
30
- file_load(@file, &block)
36
+ if !@options[:file].nil?
37
+ file_load(@options[:file], &block)
31
38
  else
32
39
  stdin_load(&block)
33
40
  end
@@ -35,7 +42,7 @@ module Ltsview
35
42
 
36
43
  def stdin_load
37
44
  $stdin.each_line do |line|
38
- yield LTSV.parse(line.chomp)
45
+ yield LTSV.parse(line.chomp).first
39
46
  end
40
47
  end
41
48
 
@@ -46,29 +53,37 @@ module Ltsview
46
53
  end
47
54
  end
48
55
 
56
+ def formatter(ltsv)
57
+ color ltsv.send("to_#{@options[:mode]}".to_sym), @options[:mode]
58
+ end
59
+
60
+ def tag
61
+ "@[#{@options[:tag]}] " if @options[:tag]
62
+ end
63
+
49
64
  def filter(ltsv)
50
- matcher(ltsv).each do |key, val|
51
- yield key, val if keys?(key) && !ignore?(key)
65
+ matcher(ltsv).delete_if do |key, val|
66
+ !keys?(key) || ignore?(key)
52
67
  end
53
68
  end
54
69
 
55
70
  def matcher(ltsv)
56
- if !@regex.nil? && ltsv[@regex[:key]] !~ /(#{@regex[:value]})/
71
+ if !@options[:regex].nil? && ltsv[@options[:regex][:key]] !~ /(#{@options[:regex][:value]})/
57
72
  ltsv = {}
58
73
  end
59
74
  ltsv
60
75
  end
61
76
 
62
77
  def keys?(key)
63
- @keys.nil? || @keys.include?(key.to_s)
78
+ @options[:keys].nil? || @options[:keys].include?(key.to_s)
64
79
  end
65
80
 
66
81
  def ignore?(key)
67
- !@ignore_key.nil? && @ignore_key.include?(key.to_s)
82
+ !@options[:ignore_key].nil? && @options[:ignore_key].include?(key.to_s)
68
83
  end
69
84
 
70
- def color(key, val)
71
- @color ? "#{key.to_s.magenta}: #{val.cyan}" : "#{key}: #{val}"
85
+ def color(ltsv, mode)
86
+ @options[:color] ? CodeRay.scan(ltsv, mode).term : ltsv
72
87
  end
73
88
 
74
89
  end
@@ -1,3 +1,3 @@
1
1
  module Ltsview
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/ltsview.rb CHANGED
@@ -4,10 +4,10 @@ module Ltsview
4
4
  # Your code goes here...
5
5
 
6
6
  require 'ltsv'
7
- require 'colored'
7
+ require 'coderay'
8
8
  require 'optparse'
9
9
  require 'yaml'
10
- require 'pp'
10
+ require 'json'
11
11
 
12
12
  require 'ltsview/parse'
13
13
  end
data/ltsview.gemspec CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |gem|
18
18
  gem.require_paths = ["lib"]
19
19
 
20
20
  gem.add_runtime_dependency 'ltsv'
21
- gem.add_runtime_dependency 'colored'
21
+ gem.add_runtime_dependency 'coderay'
22
22
 
23
23
  gem.add_development_dependency 'rake'
24
24
  gem.add_development_dependency 'rspec'
data/spec/parse_spec.rb CHANGED
@@ -11,7 +11,29 @@ describe Ltsview::Parse do
11
11
  $stdin << "hoge:fuga hago\tfoo:barbaz\n"
12
12
  $stdin.rewind
13
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")
14
+ }.must_equal("\e[45m---\e[0m\n\e[1;32m:hoge\e[0m: \e[1;33m\e[41mfuga hago\e[0m\n\e[1;32m:foo\e[0m: \e[1;33m\e[41mbarbaz\e[0m\n")
15
+ end
16
+
17
+ it 'shoild get json' do
18
+ parse = Ltsview::Parse.new(['-j'])
19
+ capture(:stdout) {
20
+ $stdin = StringIO.new
21
+ $stdin << "hoge:fuga hago\tfoo:barbaz\n"
22
+ $stdin.rewind
23
+ parse.print
24
+ }.must_equal("{\e[35m\"hoge\"\e[0m:\e[32m\e[1;32m\"\e[0m\e[32mfuga hago\e[1;32m\"\e[0m\e[32m\e[0m,\e[35m\"foo\"\e[0m:\e[32m\e[1;32m\"\e[0m\e[32mbarbaz\e[1;32m\"\e[0m\e[32m\e[0m}\n")
25
+ end
26
+ end
27
+
28
+ describe 'when appended tag' do
29
+ it 'should apeended tag' do
30
+ parse = Ltsview::Parse.new(['-t', 'test.tag'])
31
+ capture(:stdout) {
32
+ $stdin = StringIO.new
33
+ $stdin << "hoge:fuga hago\tfoo:barbaz\n"
34
+ $stdin.rewind
35
+ parse.print
36
+ }.must_equal("@[test.tag] \e[45m---\e[0m\n\e[1;32m:hoge\e[0m: \e[1;33m\e[41mfuga hago\e[0m\n\e[1;32m:foo\e[0m: \e[1;33m\e[41mbarbaz\e[0m\n")
15
37
  end
16
38
  end
17
39
 
@@ -20,7 +42,7 @@ describe Ltsview::Parse do
20
42
  parse = Ltsview::Parse.new(['-f','spec/test.ltsv'])
21
43
  capture(:stdout){
22
44
  parse.print
23
- }.must_equal("\e[35mhoge\e[0m: \e[36mfuga hago\e[0m\n\e[35mfoo\e[0m: \e[36mbarbaz\e[0m\n")
45
+ }.must_equal("\e[45m---\e[0m\n\e[1;32m:hoge\e[0m: \e[1;33m\e[41mfuga hago\e[0m\n\e[1;32m:foo\e[0m: \e[1;33m\e[41mbarbaz\e[0m\n\e[45m---\e[0m {}\n")
24
46
  end
25
47
  end
26
48
 
@@ -33,7 +55,7 @@ describe Ltsview::Parse do
33
55
  $stdin << "hoge:fuga hago\tfoo:barbaz\n"
34
56
  $stdin.rewind
35
57
  parse.print
36
- }.must_equal("\e[35mfoo\e[0m: \e[36mbarbaz\e[0m\n")
58
+ }.must_equal("\e[45m---\e[0m\n\e[1;32m:foo\e[0m: \e[1;33m\e[41mbarbaz\e[0m\n")
37
59
  end
38
60
  end
39
61
 
@@ -45,7 +67,7 @@ describe Ltsview::Parse do
45
67
  $stdin << "hoge:fuga hago\tfoo:barbaz\n"
46
68
  $stdin.rewind
47
69
  parse.print
48
- }.must_equal("\e[35mhoge\e[0m: \e[36mfuga hago\e[0m\n")
70
+ }.must_equal("\e[45m---\e[0m\n\e[1;32m:hoge\e[0m: \e[1;33m\e[41mfuga hago\e[0m\n")
49
71
  end
50
72
  end
51
73
 
@@ -58,7 +80,7 @@ describe Ltsview::Parse do
58
80
  $stdin << "hago:fuga2 hago\tfoo:fugabarbaz\n"
59
81
  $stdin.rewind
60
82
  parse.print
61
- }.must_equal("\e[35mhago\e[0m: \e[36mfuga2 hago\e[0m\n\e[35mfoo\e[0m: \e[36mfugabarbaz\e[0m\n")
83
+ }.must_equal("\e[45m---\e[0m {}\n\e[45m---\e[0m\n\e[1;32m:hago\e[0m: \e[1;33m\e[41mfuga2 hago\e[0m\n\e[1;32m:foo\e[0m: \e[1;33m\e[41mfugabarbaz\e[0m\n")
62
84
  end
63
85
  end
64
86
 
@@ -70,7 +92,7 @@ describe Ltsview::Parse do
70
92
  $stdin << "hoge:fuga hago\tfoo:barbaz\n"
71
93
  $stdin.rewind
72
94
  parse.print
73
- }.must_equal("\e[35mhoge\e[0m: \e[36mfuga hago\e[0m\n\e[35mfoo\e[0m: \e[36mbarbaz\e[0m\n")
95
+ }.must_equal("\e[45m---\e[0m\n\e[1;32m:hoge\e[0m: \e[1;33m\e[41mfuga hago\e[0m\n\e[1;32m:foo\e[0m: \e[1;33m\e[41mbarbaz\e[0m\n")
74
96
  end
75
97
 
76
98
  it 'should by color mode off' do
@@ -80,7 +102,7 @@ describe Ltsview::Parse do
80
102
  $stdin << "hoge:fuga hago\tfoo:barbaz\n"
81
103
  $stdin.rewind
82
104
  parse.print
83
- }.must_equal("hoge: fuga hago\nfoo: barbaz\n")
105
+ }.must_equal("---\n:hoge: fuga hago\n:foo: barbaz\n")
84
106
  end
85
107
 
86
108
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ltsview
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-15 00:00:00.000000000 Z
12
+ date: 2013-02-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ltsv
@@ -28,7 +28,7 @@ dependencies:
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
30
  - !ruby/object:Gem::Dependency
31
- name: colored
31
+ name: coderay
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  none: false
34
34
  requirements: