console_table 0.0.2
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 +26 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/console_table.gemspec +24 -0
- data/lib/console_table.rb +228 -0
- metadata +95 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6d6584806bd70be8c861b3f31cab09d40dd7aaea
|
4
|
+
data.tar.gz: b14fa23bff85071edfb100410e7058935837aec5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4b325157fe823b192c90a2f55966e79f3540a446bc903776221fea12a201d10abe2f72ced458be2f4ec92f1ba7f82cb61afa2f073b9db9ee1c1e77322b797b8d
|
7
|
+
data.tar.gz: 533d3190d9c667094541be8a7dd3bf1092253932491da7c382ed0b02c86452bf08d4a2bd7be68e256a2c5fb87730b68219dcdf5903003313fb9e9fc53980dd3a
|
data/.gitignore
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
.idea
|
2
|
+
*.gem
|
3
|
+
*.rbc
|
4
|
+
.bundle
|
5
|
+
.config
|
6
|
+
.yardoc
|
7
|
+
Gemfile.lock
|
8
|
+
InstalledFiles
|
9
|
+
_yardoc
|
10
|
+
coverage
|
11
|
+
doc/
|
12
|
+
lib/bundler/man
|
13
|
+
pkg
|
14
|
+
rdoc
|
15
|
+
spec/reports
|
16
|
+
test/tmp
|
17
|
+
test/version_tmp
|
18
|
+
tmp
|
19
|
+
|
20
|
+
# for a library or gem, you might want to ignore these files since the code is
|
21
|
+
# intended to run in multiple environments; otherwise, check them in:
|
22
|
+
Gemfile.lock
|
23
|
+
.ruby-version
|
24
|
+
.ruby-gemset
|
25
|
+
|
26
|
+
.rvmrc
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Rod Hilton
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# ConsoleTable
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'console_table'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install console_table
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it ( http://github.com/<my-github-username>/console_table/fork )
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -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 'console_table/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "console_table"
|
8
|
+
spec.version = "0.0.2"
|
9
|
+
spec.authors = ["Rod Hilton"]
|
10
|
+
spec.email = ["consoletable@rodhilton.com"]
|
11
|
+
spec.summary = %q{Simplifies printing tables of information to commandline consoles}
|
12
|
+
spec.description = %q{Allows developers to define tables with specifically-sized columns, which can then have entries printed to them that are automatically formatted, truncated, and padded to fit in the console window.}
|
13
|
+
spec.homepage = "https://github.com/rodhilton/console_table"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
21
|
+
spec.add_development_dependency 'rake', '~> 0'
|
22
|
+
|
23
|
+
spec.add_runtime_dependency 'colorize', '~> 0.7'
|
24
|
+
end
|
@@ -0,0 +1,228 @@
|
|
1
|
+
class ConsoleTable
|
2
|
+
|
3
|
+
def self.define(layout, options, &block)
|
4
|
+
table = ConsoleTable.new(layout, options)
|
5
|
+
table.print_header()
|
6
|
+
block.call(table)
|
7
|
+
table.print_footer()
|
8
|
+
end
|
9
|
+
|
10
|
+
def initialize(column_layout, options={})
|
11
|
+
@original_column_layout = column_layout
|
12
|
+
@left_margin = options[:left_margin] || 0
|
13
|
+
@right_margin = options[:right_margin] || 0
|
14
|
+
|
15
|
+
@count = 0
|
16
|
+
|
17
|
+
@title = options[:title]
|
18
|
+
|
19
|
+
@footer_lines = []
|
20
|
+
|
21
|
+
calc_column_widths()
|
22
|
+
@headings_printed = false
|
23
|
+
|
24
|
+
Signal.trap('SIGWINCH', proc { calc_column_widths() })
|
25
|
+
end
|
26
|
+
|
27
|
+
def print_header()
|
28
|
+
$stdout.print " " * @left_margin
|
29
|
+
$stdout.print "=" * @working_width
|
30
|
+
$stdout.print "\n"
|
31
|
+
|
32
|
+
if not @title.nil? and @title.length <= @working_width
|
33
|
+
$stdout.print " "*@left_margin
|
34
|
+
left_side = (@working_width - @title.uncolorize.length)/2
|
35
|
+
right_side = (@working_width - @title.uncolorize.length) - left_side
|
36
|
+
$stdout.print " "*left_side
|
37
|
+
$stdout.print @title
|
38
|
+
$stdout.print " "*right_side
|
39
|
+
$stdout.print "\n"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def print_headings()
|
44
|
+
@headings_printed = true
|
45
|
+
$stdout.print " "*@left_margin
|
46
|
+
|
47
|
+
@column_widths.each_with_index do |column, i|
|
48
|
+
justify = column[:justify] || :left
|
49
|
+
title = column[:title].strip
|
50
|
+
$stdout.print format(column[:size], title, false, justify).bold
|
51
|
+
$stdout.print " " if i < @column_widths.size-1
|
52
|
+
end
|
53
|
+
$stdout.print "\n"
|
54
|
+
|
55
|
+
$stdout.print " " * @left_margin
|
56
|
+
$stdout.print "-" * @working_width
|
57
|
+
$stdout.print "\n"
|
58
|
+
end
|
59
|
+
|
60
|
+
def add_footer(line)
|
61
|
+
lines = line.split("\n")
|
62
|
+
lines.each do |l|
|
63
|
+
@footer_lines << l.strip unless l.nil? or l.uncolorize.strip.blank?
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def print_footer()
|
68
|
+
should_print_footer = @footer_lines.length > 0 && @footer_lines.any?{|l| l.uncolorize.length <= @working_width}
|
69
|
+
|
70
|
+
if(should_print_footer)
|
71
|
+
$stdout.print " " * @left_margin
|
72
|
+
$stdout.print "-" * @working_width
|
73
|
+
$stdout.print "\n"
|
74
|
+
end
|
75
|
+
|
76
|
+
@footer_lines.each do |line|
|
77
|
+
if line.uncolorize.length <= @working_width
|
78
|
+
$stdout.print " " * @left_margin
|
79
|
+
$stdout.print " " * (@working_width - line.uncolorize.length)
|
80
|
+
$stdout.print line
|
81
|
+
$stdout.print "\n"
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
$stdout.print " " * @left_margin
|
86
|
+
$stdout.print "=" * @working_width
|
87
|
+
$stdout.print "\n"
|
88
|
+
end
|
89
|
+
|
90
|
+
def print_plain(to_print)
|
91
|
+
$stdout.print " "*@left_margin
|
92
|
+
|
93
|
+
if to_print.is_a? String
|
94
|
+
$stdout.print format(@working_width, normalize(to_print))
|
95
|
+
elsif to_print.is_a? Hash
|
96
|
+
color = to_print[:color] || :default
|
97
|
+
background = to_print[:background] || :default
|
98
|
+
text = normalize(to_print[:text]) || ""
|
99
|
+
ellipsize = to_print[:ellipsize] || false
|
100
|
+
justify = to_print[:justify] || :left
|
101
|
+
mode = to_print[:mode] || :default
|
102
|
+
|
103
|
+
formatted=format(@working_width, text, ellipsize, justify).colorize(:color=>color, :background=>background, :mode=>mode)
|
104
|
+
$stdout.print formatted
|
105
|
+
end
|
106
|
+
|
107
|
+
$stdout.print "\n"
|
108
|
+
end
|
109
|
+
|
110
|
+
def print(options)
|
111
|
+
print_headings unless @headings_printed
|
112
|
+
|
113
|
+
$stdout.print " "*@left_margin
|
114
|
+
#column order is set, so go through each column and look up values in the incoming options
|
115
|
+
@column_widths.each_with_index do |column, i|
|
116
|
+
to_print = options[column[:key]] || ""
|
117
|
+
justify = column[:justify] || :left
|
118
|
+
if to_print.is_a? String
|
119
|
+
$stdout.print format(column[:size], normalize(to_print), false, justify)
|
120
|
+
elsif to_print.is_a? Hash
|
121
|
+
color = to_print[:color] || :default
|
122
|
+
background = to_print[:background] || :default
|
123
|
+
text = normalize(to_print[:text]) || ""
|
124
|
+
ellipsize = to_print[:ellipsize] || false
|
125
|
+
highlight = to_print[:highlight]
|
126
|
+
justify = to_print[:justify] || justify #can override
|
127
|
+
mode = to_print[:mode] || :default
|
128
|
+
|
129
|
+
formatted=format(column[:size], text, ellipsize, justify).colorize(:color=>color, :background=>background, :mode=>mode)
|
130
|
+
|
131
|
+
unless(to_print[:highlight].nil?)
|
132
|
+
highlight_regex = to_print[:highlight][:regex] || /wontbefoundbecauseit'sgobbledygookblahblahblahbah/
|
133
|
+
highlight_color = to_print[:highlight][:color] || :blue
|
134
|
+
highlight_background = to_print[:highlight][:background] || :default
|
135
|
+
|
136
|
+
formatted = formatted.gsub(highlight_regex, '\0'.colorize(:color=>highlight_color, :background=>highlight_background))
|
137
|
+
end
|
138
|
+
|
139
|
+
$stdout.print formatted
|
140
|
+
else
|
141
|
+
$stdout.print format(column[:size], normalize(to_print.to_s))
|
142
|
+
end
|
143
|
+
|
144
|
+
$stdout.print " " if i < @column_widths.size-1
|
145
|
+
end
|
146
|
+
$stdout.print "\n"
|
147
|
+
|
148
|
+
@count = @count + 1
|
149
|
+
end
|
150
|
+
|
151
|
+
private
|
152
|
+
def normalize(string)
|
153
|
+
if (string.nil?)
|
154
|
+
nil
|
155
|
+
else
|
156
|
+
string.to_s.gsub(/\s+/, " ").strip #Primarily to remove any tabs or newlines
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
def calc_column_widths()
|
161
|
+
@column_widths = []
|
162
|
+
|
163
|
+
begin
|
164
|
+
total_width = TermInfo.screen_columns
|
165
|
+
rescue => ex
|
166
|
+
total_width = ENV["COLUMNS"].to_i || 150
|
167
|
+
end
|
168
|
+
|
169
|
+
num_spacers = @original_column_layout.length - 1
|
170
|
+
set_sizes = @original_column_layout.collect{|x| x[:size]}.find_all{|x| x.is_a? Integer}
|
171
|
+
used_up = set_sizes.inject(:+) || 0
|
172
|
+
available = total_width - used_up - @left_margin - @right_margin - num_spacers
|
173
|
+
|
174
|
+
if(available <= 0)
|
175
|
+
$stderr.puts "ConsoleTable configuration invalid, current window is too small to display required sizes"
|
176
|
+
Kernel.exit(-1)
|
177
|
+
end
|
178
|
+
|
179
|
+
percentages = @original_column_layout.collect{|x| x[:size]}.find_all{|x| x.is_a? Float}
|
180
|
+
percent_used = percentages.inject(:+) || 0
|
181
|
+
|
182
|
+
if(percent_used > 1.0)
|
183
|
+
$stderr.puts "ConsoleTable configuration invalid, percentages total value greater than 100%"
|
184
|
+
Kernel.exit(-1)
|
185
|
+
end
|
186
|
+
|
187
|
+
percent_available = 1 - percent_used
|
188
|
+
stars = @original_column_layout.collect{|x| x[:size]}.find_all{|x| x.is_a? String}
|
189
|
+
num_stars = stars.length || 1
|
190
|
+
percent_for_stars = percent_available / num_stars
|
191
|
+
|
192
|
+
@original_column_layout.each do |column_config|
|
193
|
+
if column_config[:size].is_a? Integer
|
194
|
+
@column_widths << column_config #As-is when integer
|
195
|
+
elsif column_config[:size].is_a? Float
|
196
|
+
@column_widths << column_config.merge({:size=> (column_config[:size]*available).floor })
|
197
|
+
elsif column_config[:size].is_a?(String) && column_config[:size] == "*"
|
198
|
+
@column_widths << column_config.merge({:size=> (percent_for_stars*available).floor })
|
199
|
+
else
|
200
|
+
$stderr.puts "ConsoleTable configuration invalid, '#{column_config[:size]}' is not a valid size"
|
201
|
+
Kernel.exit(-1)
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
@working_width = (@column_widths.inject(0) {|res, c| res+c[:size]}) + @column_widths.length - 1
|
206
|
+
end
|
207
|
+
|
208
|
+
def format(length, text, ellipsize=false, justify=:left)
|
209
|
+
if text.length > length
|
210
|
+
if(ellipsize)
|
211
|
+
text[0, length-3] + '...'
|
212
|
+
else
|
213
|
+
text[0, length]
|
214
|
+
end
|
215
|
+
else
|
216
|
+
if justify == :right
|
217
|
+
(" "*(length-text.length)) + text
|
218
|
+
elsif justify == :center
|
219
|
+
space = length-text.length
|
220
|
+
left_side = space/2
|
221
|
+
right_side = space - left_side
|
222
|
+
(" " * left_side) + text + (" "*right_side)
|
223
|
+
else #assume left
|
224
|
+
text + (" "*(length-text.length))
|
225
|
+
end
|
226
|
+
end
|
227
|
+
end
|
228
|
+
end
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: console_table
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rod Hilton
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-02-12 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.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
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
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: colorize
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.7'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.7'
|
55
|
+
description: Allows developers to define tables with specifically-sized columns, which
|
56
|
+
can then have entries printed to them that are automatically formatted, truncated,
|
57
|
+
and padded to fit in the console window.
|
58
|
+
email:
|
59
|
+
- consoletable@rodhilton.com
|
60
|
+
executables: []
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files: []
|
63
|
+
files:
|
64
|
+
- ".gitignore"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- console_table.gemspec
|
70
|
+
- lib/console_table.rb
|
71
|
+
homepage: https://github.com/rodhilton/console_table
|
72
|
+
licenses:
|
73
|
+
- MIT
|
74
|
+
metadata: {}
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 2.2.2
|
92
|
+
signing_key:
|
93
|
+
specification_version: 4
|
94
|
+
summary: Simplifies printing tables of information to commandline consoles
|
95
|
+
test_files: []
|