ttable 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3f5aa789decb21c44678b0b5865b3b7d0f17ae10
4
+ data.tar.gz: cac976ca917be93b636c1150ce3d96f0055d7229
5
+ SHA512:
6
+ metadata.gz: 1dd66ff78816eee695535e8197bd24eeb8fac2b265cfa911cdf21084f00b74f12232e1394b75aaec6da5ad73780859ccf5e81040da66472c6b21a3497944c1e7
7
+ data.tar.gz: 354d25c974938f4b63f3bc8ea3ccf3dd329f219a830a1dd9a8f604a16c3616b33f80a9c3ed17cbeda6ce570aa0eb63e89d05636f8472273401f4854c001029db
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'http://ruby.taobao.org'
2
+
3
+ gemspec
4
+
5
+ group :development, :test do
6
+ gem 'rspec', '~> 2.12.0'
7
+ gem 'guard'
8
+ gem 'guard-rspec'
9
+ end
10
+
@@ -0,0 +1,8 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec' do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Forrest Ye
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.
@@ -0,0 +1,33 @@
1
+ # ttable
2
+
3
+ See it in action:
4
+
5
+ ![screenshot.png](screenshot.png)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'ttable'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install ttable
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Contributing
28
+
29
+ 1. Fork it ( https://github.com/[my-github-username]/ttable/fork )
30
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
31
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
32
+ 4. Push to the branch (`git push origin my-new-feature`)
33
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "httparty"
4
+ require "ttable"
5
+
6
+ API_ENDPOINT="http://api.ohsame.com/v2.1.0"
7
+
8
+ def table_of_channel_list(response, name)
9
+ Terminal::Table.new do |table|
10
+ table.rows = response[name].map { |i| [i['channel_id'], i['cate'], i['title'] || i['name']] }
11
+ end
12
+ end
13
+
14
+ def get(path)
15
+ HTTParty.get(API_ENDPOINT + path)
16
+ end
17
+
18
+ puts table_of_channel_list(get('/discovery'), 'kv')
@@ -0,0 +1,84 @@
1
+ # coding: utf-8
2
+
3
+ require "gemoji"
4
+
5
+ class String
6
+ def twidth
7
+ # ❤️ is 2 characters
8
+ result = -3 * scan('❤️').size
9
+
10
+ chars.inject(result) do |result, c|
11
+ if c.ord <= 126
12
+ result += 1
13
+ elsif %w{ • é · ♪ … ω ˊ ˋ }.include?(c)
14
+ result += 1
15
+ elsif Emoji.find_by_unicode(c)
16
+ result += 1
17
+ else
18
+ result += 2
19
+ end
20
+ end
21
+ end
22
+
23
+ def tljust(width)
24
+ if width > twidth
25
+ self + ' ' * (width - twidth)
26
+ else
27
+ self
28
+ end
29
+ end
30
+ end
31
+
32
+ module Terminal
33
+ class Table
34
+ attr_accessor :rows
35
+ attr_accessor :headings
36
+ attr_accessor :column_widths
37
+
38
+ def initialize
39
+ @rows = []
40
+ @headings = []
41
+ @column_widths = []
42
+ yield self if block_given?
43
+ recalculate_column_widths!
44
+ end
45
+
46
+ def recalculate_column_widths!
47
+ if @rows.count > 0
48
+ (0...@rows.first.size).each do |col|
49
+ @column_widths[col] = @rows.map { |row| row[col].to_s.twidth }.max
50
+ end
51
+ end
52
+
53
+ if @headings.count > 0
54
+ (0...@headings.size).each do |col|
55
+ @column_widths[col] = [@column_widths[col], @headings[col].twidth].max
56
+ end
57
+ end
58
+ end
59
+
60
+ def to_s
61
+ result = ''
62
+
63
+ header_and_footer = '+' + @column_widths.map { |w| '-' * (w + 2) }.join('+') + '+' + "\n"
64
+
65
+ if @headings.count > 0
66
+ result += header_and_footer
67
+
68
+ content = @headings.each_with_index.map { |grid, i| grid.to_s.tljust(@column_widths[i]) }
69
+
70
+ result += '| ' + content.join(' | ') + " |\n"
71
+ end
72
+
73
+ result += header_and_footer
74
+
75
+ @rows.each do |row|
76
+ content = row.each_with_index.map { |grid, i| grid.to_s.tljust(@column_widths[i]) }
77
+
78
+ result += '| ' + content.join(' | ') + " |\n"
79
+ end
80
+
81
+ result + header_and_footer
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,2 @@
1
+ require "ttable/version"
2
+ require "terminal/table"
@@ -0,0 +1,3 @@
1
+ module Ttable
2
+ VERSION = "0.0.1"
3
+ end
Binary file
@@ -0,0 +1 @@
1
+ require "ttable"
@@ -0,0 +1,7 @@
1
+ require "spec_helper"
2
+
3
+ module Terminal
4
+ describe Table do
5
+ it { should respond_to :to_s }
6
+ end
7
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ttable/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ttable"
8
+ spec.version = Ttable::VERSION
9
+ spec.authors = ["Forrest Ye"]
10
+ spec.email = ["afu@forresty.com"]
11
+ spec.summary = %q{Terminal Table with @2x width character support}
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.6"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+
23
+ spec.add_runtime_dependency 'gemoji'
24
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ttable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Forrest Ye
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-23 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: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: gemoji
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - afu@forresty.com
58
+ executables:
59
+ - ttable
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - .gitignore
64
+ - Gemfile
65
+ - Guardfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/ttable
70
+ - lib/terminal/table.rb
71
+ - lib/ttable.rb
72
+ - lib/ttable/version.rb
73
+ - screenshot.png
74
+ - spec/spec_helper.rb
75
+ - spec/terminal/table_spec.rb
76
+ - ttable.gemspec
77
+ homepage: ''
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.0.2
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: Terminal Table with @2x width character support
101
+ test_files:
102
+ - spec/spec_helper.rb
103
+ - spec/terminal/table_spec.rb