table-formatter 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 35630fef5882d3f10725cf89cf97629e288cf83a
4
- data.tar.gz: c242dd193ffbf9a2d9a1a77c825d4a9f8a58f8b0
3
+ metadata.gz: 56ff8ada05a246d2184ea4fed379eb0324a95719
4
+ data.tar.gz: 3ca9233356b47601cb4cf73a0f7d2c8e1f1f68ad
5
5
  SHA512:
6
- metadata.gz: ba7e6dba293803906167489a270b6d2124a810311bd136a3d245199f4eeecb56f3d94eaed9659a3698af152e6a24eaf239b99ffa949fe34958e924f2567d95d6
7
- data.tar.gz: bb4a5a6adef0397a7093ad5cfaf0b053ff26f77762a71ef40d36b09b222aed471a3e7b787ccc72630052c6163f3b8440294a6d6a47c278dfe8b2766e6570a0b1
6
+ metadata.gz: 5b777176fc5f4b9f413caae17d92fda82f48d4037ce883917eeb7bf8fe95e7e6c5d9f061004f1b02bb7fafa027c4f8c6716b9f751e9f086addf6dc76b6aec352
7
+ data.tar.gz: 2399d85f2b8af41caf38c833031e18a8176476233c3f08444e3889f316ab280ab95b574b15f17e0371dfe755a57988b4c169db3b679832db221832282a8044bb
checksums.yaml.gz.sig CHANGED
Binary file
data.tar.gz.sig CHANGED
@@ -1 +1 @@
1
- w��5u�y|��)n1c��kJ�Ãʿ�1獕f��f:�C�Xü��E���� ����dE��POy�}�����Kp��yt��� =X�1l�ޕp���s|��@dS���߿C ���ީ��^�!�w�WӇ���Oz�Y�@L�4������M����]cqh��.��
1
+ �����؍2n.�2����E~Ӊ��V��l?l57g�I�$q���&��e^�_C�GP��%�@�Ԩ����y����:��fQ�M*��!���p@��F6�B��c<�y�f�����Xқ�S���Q�����QΙ�s8������23�]��ĖW_u��m�̓*�&�F�}�}Nz)YI髰��3Ry�le|5��Ǫ]1Y�!����y���s�4:&c����RY����^�1a�&!!�eyM���SH�Ð�e���
@@ -10,24 +10,27 @@ class TableFormatter
10
10
 
11
11
  super()
12
12
  @source = source
13
- @raw_labels = labels
13
+ @labels = labels
14
14
  @border = border
15
15
  @wrap = wrap
16
16
  @divider = divider
17
17
  @maxwidth = 60
18
+
18
19
  end
19
20
 
20
- def display(width=nil, widths: nil)
21
+ def display(width=nil, widths: nil, markdown: false)
22
+
23
+ return display_markdown(@source, @labels) if markdown
21
24
 
22
- if @raw_labels then
25
+ if @labels then
23
26
 
24
- @align_cols, @labels = [], []
27
+ @align_cols, labels = [], []
25
28
 
26
- @raw_labels.each do |raw_label|
29
+ @labels.each do |raw_label|
27
30
 
28
31
  col_just, label = just(raw_label)
29
32
  @align_cols << col_just
30
- @labels << label
33
+ labels << label
31
34
  end
32
35
 
33
36
  end
@@ -35,8 +38,7 @@ class TableFormatter
35
38
  #width ||= @maxwidth
36
39
  @width = width
37
40
  @maxwidth = width if width
38
- a = @source.map {|x| x.map.to_a}.to_a
39
- labels = @labels
41
+ a = @source.map {|x| x.map{|y| y.length > 0 ? y : ' ' }}.to_a
40
42
 
41
43
  column_widths = widths ? widths : fetch_column_widths(a)
42
44
 
@@ -55,11 +57,33 @@ class TableFormatter
55
57
 
56
58
  alias to_s display
57
59
 
58
- def labels=(a)
59
- @raw_labels = a
60
- end
61
60
 
62
61
  private
62
+
63
+ def display_markdown(a, fields)
64
+
65
+ print_row = -> (row, widths) do
66
+ '| ' + row.map\
67
+ .with_index {|y,i| y.to_s.ljust(widths[i])}.join(' | ') + " |\n"
68
+ end
69
+
70
+ print_thline = -> (row, widths) do
71
+ '|:' + row.map\
72
+ .with_index {|y,i| y.to_s.ljust(widths[i])}.join('|:') + "|\n"
73
+ end
74
+
75
+ print_rows = -> (rows, widths) do
76
+ rows.map {|x| print_row.call(x,widths)}.join
77
+ end
78
+
79
+ widths = ([fields] + a).transpose.map{|x| x.max_by(&:length).length}
80
+ th = '|' + fields.join('|') + "|\n"
81
+ th = print_row.call(fields, widths)
82
+ th_line = print_thline.call widths.map {|x| '-' * (x+1)}, widths
83
+
84
+ tb = print_rows.call(a, widths)
85
+ table = th + th_line + tb
86
+ end
63
87
 
64
88
  def fetch_column_widths(a)
65
89
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: table-formatter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
@@ -12,26 +12,26 @@ cert_chain:
12
12
  -----BEGIN CERTIFICATE-----
13
13
  MIIDljCCAn6gAwIBAgIBATANBgkqhkiG9w0BAQUFADBIMRIwEAYDVQQDDAlnZW1t
14
14
  YXN0ZXIxHjAcBgoJkiaJk/IsZAEZFg5qYW1lc3JvYmVydHNvbjESMBAGCgmSJomT
15
- 8ixkARkWAmV1MB4XDTE1MDcwMzE5MjA0MloXDTE2MDcwMjE5MjA0MlowSDESMBAG
15
+ 8ixkARkWAmV1MB4XDTE2MDcxMjE3MDUyNloXDTE3MDcxMjE3MDUyNlowSDESMBAG
16
16
  A1UEAwwJZ2VtbWFzdGVyMR4wHAYKCZImiZPyLGQBGRYOamFtZXNyb2JlcnRzb24x
17
17
  EjAQBgoJkiaJk/IsZAEZFgJldTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoC
18
- ggEBANT1YzpsNgKn73Jgp0dXfO4equ1i3K9gj4V8Mic2kt7m6OgVjdtwxSf3UVX0
19
- 3Bro1ni/q0sylxme3IOwRsJfC3tEKZIOaGVM5UzZZimqgUoKMAsK5UMd2lUL/S+t
20
- mLxYaREIa2me6iIt+rfmhCDQJ9UAfuxMavtpV7UrJNK6KnQ8IiFtcDWb1YfvMVT+
21
- le3NCdB1R4yNMt7cm64Xx4527/otov7GLHaT9yPVzcs44KYU0eCuqTEnPGke5bv4
22
- ASp09e3Dg0wfB4pQ8ntZnGph9OS+Ojlvs4bVsnf7/fjEYWttgddMELnPu36keBVD
23
- zbmLL30iHPLvNxmJvwotGZBfaUECAwEAAaOBij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zsMfVQ==
26
+ c3RlckBqYW1lc3JvYmVydHNvbi5ldTANBgkqhkiG9w0BAQUFAAOCAQEAD82ILMdg
27
+ +aSp0sLXD4J3RVxXq4FwEp1qc2YzOD/bq3hjEMOIpaUTn4mDgpBZHTi+Im27abmC
28
+ IOOExDOuJyMiPf9tqAumOaPkRqLiejQN1C8b6oGM3+iaJJ/RVJRa8RA9FfbwuBWV
29
+ Ls4X+NVJNZjCGJdfyUVlD+4TvyVqqJw5ZojdZV1Gg4CfweBYq9R674yg41EaeNOh
30
+ Wo2B+S/H8f/hpJ2VKPkq6jf3l4Qt7/d+cFstruyVfkHotwvGP8Z+Xh3zDrxU6xFg
31
+ 3N7fAlUbFYq1qPg9cSfZni7dwYupZfs1vLjUbKCYyF3LGUR1jnLn4hkP1lFClWwy
32
+ wGnSddXdhZVZSQ==
33
33
  -----END CERTIFICATE-----
34
- date: 2015-07-03 00:00:00.000000000 Z
34
+ date: 2016-07-12 00:00:00.000000000 Z
35
35
  dependencies: []
36
36
  description:
37
37
  email: james@r0bertson.co.uk
@@ -60,8 +60,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
60
60
  version: '0'
61
61
  requirements: []
62
62
  rubyforge_project:
63
- rubygems_version: 2.4.6
63
+ rubygems_version: 2.5.1
64
64
  signing_key:
65
65
  specification_version: 4
66
- summary: table-formatter prints a table in plain text format from an array
66
+ summary: table-formatter prints a table in plain text format or Markdown format from
67
+ an array
67
68
  test_files: []
metadata.gz.sig CHANGED
Binary file