texttable 1.0.7 → 1.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +33 -2
- data/lib/texttable.rb +55 -24
- data/texttable.gemspec +2 -2
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3f9b989789bcfe00d1c8ead3a7079c9156ac5bc7227324a34b79f82baa915bd3
|
4
|
+
data.tar.gz: 4de414dc3664abba7a8cb089fa93bf5e059c779dcb9665314feb8b4c0588bc9b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 28c2cb92bbe3354f26776666c55a56ee177ff2969eea1fcf7b0fd0ee42bfe16dd1732315ee7328f8792f3e0e893f71af03f8fad5211091b176c23dee441f4f93
|
7
|
+
data.tar.gz: 6869f42df077bc4d038d679114ec0a647b92c51ebc5e3623247c82cf8eedfbcd60e36bdd8f9144a61c6f722bb233a12e6f09c90c7b9531d8e8bebe01c9cde0ba
|
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# texttable
|
2
2
|
|
3
|
-
`texttable` is a Ruby gem that provides an easy way to
|
3
|
+
`texttable` is a Ruby gem that provides an easy way to ingest, manage, and output rows and columns as simple tables.
|
4
4
|
|
5
|
-
##
|
5
|
+
## Simple Example
|
6
6
|
|
7
7
|
This code:
|
8
8
|
|
@@ -30,3 +30,34 @@ Will produce:
|
|
30
30
|
+-------+-----+--------------+
|
31
31
|
4 rows displayed
|
32
32
|
```
|
33
|
+
|
34
|
+
## Loading Example
|
35
|
+
|
36
|
+
This code:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
require "texttable"
|
40
|
+
|
41
|
+
info = TextTable.csv("data.csv")
|
42
|
+
info.show!
|
43
|
+
```
|
44
|
+
|
45
|
+
With the file `data.csv`:
|
46
|
+
|
47
|
+
```csv
|
48
|
+
id,first_name,last_name,email,cell,dept,photo,status
|
49
|
+
28,Mark,Jones,mark@bigcompany.com,800-555-1000,Finance,mark-jones.jpg,2
|
50
|
+
29,Sally,Miller,sally@bigcompany.com,800-555-2000,Accounting,sally-miller.jpg,1
|
51
|
+
```
|
52
|
+
|
53
|
+
Will produce:
|
54
|
+
|
55
|
+
```text
|
56
|
+
+----+------------+-----------+----------------------+--------------+------------+------------------+--------+
|
57
|
+
| id | first_name | last_name | email | cell | dept | photo | status |
|
58
|
+
+----+------------+-----------+----------------------+--------------+------------+------------------+--------+
|
59
|
+
| 28 | Mark | Jones | mark@bigcompany.com | 800-555-1000 | Finance | mark-jones.jpg | 2 |
|
60
|
+
| 29 | Sally | Miller | sally@bigcompany.com | 800-555-2000 | Accounting | sally-miller.jpg | 1 |
|
61
|
+
+----+------------+-----------+----------------------+--------------+------------+------------------+--------+
|
62
|
+
2 rows displayed
|
63
|
+
```
|
data/lib/texttable.rb
CHANGED
@@ -1,19 +1,34 @@
|
|
1
1
|
class TextTable
|
2
2
|
attr_accessor :values, :rows
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
4
|
+
class << self
|
5
|
+
def csv(src, sep=',', encoding: nil, **kw)
|
6
|
+
require 'csv'
|
7
|
+
new CSV.read(src || ARGF, {
|
8
|
+
col_sep: sep,
|
9
|
+
encoding: encoding ? encoding + ":UTF-8" : nil,
|
10
|
+
**kw
|
11
|
+
}.compact)
|
12
|
+
end
|
13
|
+
def tsv(*args, **kw); csv(args.shift, "\t", *args, **kw); end
|
14
|
+
def psv(*args, **kw); csv(args.shift, "|" , *args, **kw); end
|
15
|
+
|
16
|
+
def add(*args)
|
17
|
+
new.add(*args)
|
11
18
|
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def initialize(*args)
|
22
|
+
cols = args
|
23
|
+
cols = cols[0] if cols[0].is_a?(Array) && cols[0][0].is_a?(Array)
|
24
|
+
cols, *rows = cols if cols[0].is_a?(Array)
|
25
|
+
rows = *rows[0] if rows && rows[0].is_a?(Array) && rows[0][0].is_a?(Array)
|
26
|
+
rows = [] if !rows || !rows[0].is_a?(Array) || rows[0].empty?
|
12
27
|
@cols = Hash.new {|h,k| h[k] = h.size}
|
13
28
|
@rows = rows
|
14
29
|
@values = nil
|
15
30
|
@row = 0
|
16
|
-
cols.
|
31
|
+
cols.each_with_index {|col, i| index!(col || i) }
|
17
32
|
end
|
18
33
|
|
19
34
|
def index(field, auto=false)
|
@@ -104,7 +119,7 @@ class TextTable
|
|
104
119
|
obj = [obj, *args] if args.size > 0
|
105
120
|
@values = @rows[@row = @rows.size] = []
|
106
121
|
case obj
|
107
|
-
when Hash then obj.each {|k, v| @values[
|
122
|
+
when Hash then obj.each {|k, v| @values[index(k.to_s, true)] = v }
|
108
123
|
when Array then @values.replace(obj)
|
109
124
|
else raise "unable to add #{obj.class} objects"
|
110
125
|
end
|
@@ -118,15 +133,20 @@ class TextTable
|
|
118
133
|
def show!(list=nil)
|
119
134
|
meth = list.is_a?(Array) ? list.method(:push) : method(:puts)
|
120
135
|
join = " | "
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
136
|
+
size = @cols.size
|
137
|
+
full = [@cols.keys] + rows
|
138
|
+
full.each_with_index do |vals, i| # only when asymmetric
|
139
|
+
miss = size - vals.size
|
140
|
+
full[i] += [nil] * miss if miss > 0
|
141
|
+
full[i] = vals[0...size] if miss < 0
|
142
|
+
end
|
143
|
+
lens = full.map {|r| r.map {|c| c.to_s.size}}.transpose.map(&:max)
|
144
|
+
pict = lens.map {|len| "%-#{len}.#{len}s" }.join(join)
|
125
145
|
pict = [join, pict, join].join.strip
|
126
|
-
line = (pict % ([""] *
|
146
|
+
line = (pict % ([""] * size)).tr("| ", "+-")
|
127
147
|
seen = -1
|
128
148
|
meth["", line]
|
129
|
-
|
149
|
+
full.each do |vals|
|
130
150
|
meth[pict % vals]
|
131
151
|
meth[line] if (seen += 1) == 0
|
132
152
|
end
|
@@ -134,20 +154,30 @@ class TextTable
|
|
134
154
|
self
|
135
155
|
end
|
136
156
|
|
137
|
-
def
|
157
|
+
def lookup!(field)
|
158
|
+
@rows or raise "no rows defined"
|
159
|
+
index = index(field)
|
160
|
+
lookup = {}
|
161
|
+
@rows.each_with_index {|cols, i| lookup[cols[index]] = i}
|
162
|
+
lookup
|
163
|
+
end
|
164
|
+
|
165
|
+
def csv(sep=',', encoding: nil, **kw)
|
138
166
|
require 'csv'
|
139
|
-
csv = {
|
140
|
-
|
141
|
-
|
142
|
-
|
167
|
+
csv = CSV.new($stdout, {
|
168
|
+
col_sep: sep,
|
169
|
+
encoding: encoding ? encoding + ":UTF-8" : nil,
|
170
|
+
quote_empty: false, #!# TODO: make this an option
|
171
|
+
**kw
|
172
|
+
})
|
143
173
|
csv << @cols.keys
|
144
174
|
@rows.each {|vals| csv << vals}
|
145
175
|
nil
|
146
176
|
end
|
147
|
-
def tsv; csv("\t"); end
|
148
|
-
def psv; csv("|" ); end
|
177
|
+
def tsv(**kw); csv("\t", **kw); end
|
178
|
+
def psv(**kw); csv("|" , **kw); end
|
149
179
|
|
150
|
-
def sql(table='table', quote: false)
|
180
|
+
def sql(table='table', quote: false, timestamps: false, verb: 'insert')
|
151
181
|
q = quote ? '`' : ''
|
152
182
|
flip = @cols.invert
|
153
183
|
@rows.each do |vals|
|
@@ -156,7 +186,8 @@ class TextTable
|
|
156
186
|
list << "#{q}#{flip[i]}#{q}='#{item.gsub("'","''")}'" if item =~ /\S/
|
157
187
|
list
|
158
188
|
end
|
159
|
-
|
189
|
+
list.push('created_at=now(), updated_at=now()') if timestamps
|
190
|
+
puts "#{verb} into #{q}#{table}#{q} set #{list * ', '};" if !list.empty?
|
160
191
|
end
|
161
192
|
nil
|
162
193
|
end
|
data/texttable.gemspec
CHANGED
@@ -2,10 +2,10 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = "texttable"
|
5
|
-
s.version = "1.
|
5
|
+
s.version = "1.1.5"
|
6
6
|
s.author = "Steve Shreeve"
|
7
7
|
s.email = "steve.shreeve@gmail.com"
|
8
|
-
s.summary = "An easy way to
|
8
|
+
s.summary = "An easy way to work with rows and columns as simple tables"
|
9
9
|
s.description = "This gem will auto-size based on column widths."
|
10
10
|
s.homepage = "https://github.com/shreeve/texttable"
|
11
11
|
s.license = "MIT"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: texttable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Steve Shreeve
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-10-29 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: This gem will auto-size based on column widths.
|
14
14
|
email: steve.shreeve@gmail.com
|
@@ -26,7 +26,7 @@ homepage: https://github.com/shreeve/texttable
|
|
26
26
|
licenses:
|
27
27
|
- MIT
|
28
28
|
metadata: {}
|
29
|
-
post_install_message:
|
29
|
+
post_install_message:
|
30
30
|
rdoc_options: []
|
31
31
|
require_paths:
|
32
32
|
- lib
|
@@ -41,8 +41,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
41
41
|
- !ruby/object:Gem::Version
|
42
42
|
version: '0'
|
43
43
|
requirements: []
|
44
|
-
rubygems_version: 3.
|
45
|
-
signing_key:
|
44
|
+
rubygems_version: 3.1.4
|
45
|
+
signing_key:
|
46
46
|
specification_version: 4
|
47
|
-
summary: An easy way to
|
47
|
+
summary: An easy way to work with rows and columns as simple tables
|
48
48
|
test_files: []
|