texttable 1.0.5 → 1.1.3
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 +4 -4
- data/README.md +33 -2
- data/lib/texttable.rb +42 -21
- data/texttable.gemspec +2 -2
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: daf8fb1088ce9570617aea39c1cbbfa3b4b5819af7013e26c2e4680d1ec2ceb1
|
4
|
+
data.tar.gz: 343e38b5ff4ed2cb20076af22bcfaedb00fd70702dcc8654b4169736b87a4768
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dfbd834c7999c0c666c39043f2529b31fa4077292aec331541744cb6fc22a841380914435b75c3462647896969d4b8eb6846e72ebdf6cb3878c761f0ac8005a1
|
7
|
+
data.tar.gz: 8b401aab5b7df8d12aa883a7c0932cf5e4713889d5a3f63aa59416cfb3e3c15eb9029257819e9e46aa62e7f23a547239cd2e21e52a35ab91990240c950bdd279
|
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,14 +1,24 @@
|
|
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)
|
6
|
+
require 'csv'
|
7
|
+
new CSV.read(src || ARGF, {
|
8
|
+
col_sep: sep,
|
9
|
+
encoding: encoding ? encoding + ":UTF-8" : nil,
|
10
|
+
}.compact)
|
11
11
|
end
|
12
|
+
def tsv(*args); csv(args.shift, "\t", *args); end
|
13
|
+
def psv(*args); csv(args.shift, "|" , *args); end
|
14
|
+
end
|
15
|
+
|
16
|
+
def initialize(*args)
|
17
|
+
cols = args
|
18
|
+
cols = cols[0] if cols[0].is_a?(Array) && cols[0][0].is_a?(Array)
|
19
|
+
cols, *rows = cols if cols[0].is_a?(Array)
|
20
|
+
rows = *rows[0] if rows && rows[0].is_a?(Array) && rows[0][0].is_a?(Array)
|
21
|
+
rows = [] if !rows || !rows[0].is_a?(Array) || rows[0].empty?
|
12
22
|
@cols = Hash.new {|h,k| h[k] = h.size}
|
13
23
|
@rows = rows
|
14
24
|
@values = nil
|
@@ -111,21 +121,31 @@ class TextTable
|
|
111
121
|
self
|
112
122
|
end
|
113
123
|
|
114
|
-
def show
|
124
|
+
def show(*)
|
125
|
+
self
|
126
|
+
end
|
127
|
+
|
128
|
+
def show!(list=nil)
|
129
|
+
meth = list.is_a?(Array) ? list.method(:push) : method(:puts)
|
115
130
|
join = " | "
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
131
|
+
size = @cols.size
|
132
|
+
full = [@cols.keys] + rows
|
133
|
+
full.each_with_index do |vals, i| # only when asymmetric
|
134
|
+
miss = size - vals.size
|
135
|
+
full[i] += [nil] * miss if miss > 0
|
136
|
+
full[i] = vals[0...size] if miss < 0
|
137
|
+
end
|
138
|
+
lens = full.map {|r| r.map {|c| c.to_s.size}}.transpose.map(&:max)
|
139
|
+
pict = lens.map {|len| "%-#{len}.#{len}s" }.join(join)
|
120
140
|
pict = [join, pict, join].join.strip
|
121
|
-
line = (pict % ([""] *
|
141
|
+
line = (pict % ([""] * size)).tr("| ", "+-")
|
122
142
|
seen = -1
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
143
|
+
meth["", line]
|
144
|
+
full.each do |vals|
|
145
|
+
meth[pict % vals]
|
146
|
+
meth[line] if (seen += 1) == 0
|
127
147
|
end
|
128
|
-
|
148
|
+
meth[line, "#{seen} rows displayed", ""]
|
129
149
|
self
|
130
150
|
end
|
131
151
|
|
@@ -142,7 +162,7 @@ class TextTable
|
|
142
162
|
def tsv; csv("\t"); end
|
143
163
|
def psv; csv("|" ); end
|
144
164
|
|
145
|
-
def sql(table='table', quote: false)
|
165
|
+
def sql(table='table', quote: false, timestamps: false, verb: 'insert')
|
146
166
|
q = quote ? '`' : ''
|
147
167
|
flip = @cols.invert
|
148
168
|
@rows.each do |vals|
|
@@ -151,7 +171,8 @@ class TextTable
|
|
151
171
|
list << "#{q}#{flip[i]}#{q}='#{item.gsub("'","''")}'" if item =~ /\S/
|
152
172
|
list
|
153
173
|
end
|
154
|
-
|
174
|
+
list.push('created_at=now(), updated_at=now()') if timestamps
|
175
|
+
puts "#{verb} into #{q}#{table}#{q} set #{list * ', '};" if !list.empty?
|
155
176
|
end
|
156
177
|
nil
|
157
178
|
end
|
@@ -161,4 +182,4 @@ class ActiveRecord::Result
|
|
161
182
|
def +@
|
162
183
|
TextTable.new(columns, rows)
|
163
184
|
end
|
164
|
-
end
|
185
|
+
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.3"
|
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.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Steve Shreeve
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-06-10 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
|
@@ -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.
|
44
|
+
rubygems_version: 3.1.2
|
45
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: []
|