texttable 1.0.6 → 1.1.4
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 +50 -20
- 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: 5c4dbf7e3ed3836d1f3e61665700ca6dc7aaf94c95a1a026b3a84cd613c89ee7
|
4
|
+
data.tar.gz: fc17d9a066c413f3c887889ad488ce703381470f6c53992e10f3ff54d47c7b51
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 11639822bf41842b1ee7e8817e53f724ce22cc7fa128391675700a8aa4048e975fe00679bf73d534c276991dcfab242205043d5d20f749b3de0439ab262de300
|
7
|
+
data.tar.gz: 42519909f0df28de90b93744fb985e99ae3662395cdc2815767d4a0510108154a632e791b295537ee12efd862a5010f3b2af5165ce351b264e512caacd8c528d
|
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,11 +154,20 @@ class TextTable
|
|
134
154
|
self
|
135
155
|
end
|
136
156
|
|
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
|
+
|
137
165
|
def csv(sep=',', encoding: nil)
|
138
166
|
require 'csv'
|
139
167
|
csv = {}
|
140
|
-
csv[:encoding] = encoding + ":UTF-8" if encoding
|
141
|
-
csv[:col_sep
|
168
|
+
csv[:encoding ] = encoding + ":UTF-8" if encoding
|
169
|
+
csv[:col_sep ] = sep
|
170
|
+
csv[:quote_empty] = false #!# TODO: make this an option
|
142
171
|
csv = CSV.new($stdout, csv)
|
143
172
|
csv << @cols.keys
|
144
173
|
@rows.each {|vals| csv << vals}
|
@@ -147,7 +176,7 @@ class TextTable
|
|
147
176
|
def tsv; csv("\t"); end
|
148
177
|
def psv; csv("|" ); end
|
149
178
|
|
150
|
-
def sql(table='table', quote: false)
|
179
|
+
def sql(table='table', quote: false, timestamps: false, verb: 'insert')
|
151
180
|
q = quote ? '`' : ''
|
152
181
|
flip = @cols.invert
|
153
182
|
@rows.each do |vals|
|
@@ -156,7 +185,8 @@ class TextTable
|
|
156
185
|
list << "#{q}#{flip[i]}#{q}='#{item.gsub("'","''")}'" if item =~ /\S/
|
157
186
|
list
|
158
187
|
end
|
159
|
-
|
188
|
+
list.push('created_at=now(), updated_at=now()') if timestamps
|
189
|
+
puts "#{verb} into #{q}#{table}#{q} set #{list * ', '};" if !list.empty?
|
160
190
|
end
|
161
191
|
nil
|
162
192
|
end
|
@@ -166,4 +196,4 @@ class ActiveRecord::Result
|
|
166
196
|
def +@
|
167
197
|
TextTable.new(columns, rows)
|
168
198
|
end
|
169
|
-
end
|
199
|
+
end if defined?(ActiveRecord)
|
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.4"
|
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.4
|
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:
|
11
|
+
date: 2020-10-06 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: []
|