console_table 0.1.2 → 0.1.4

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: 8df10271a0fee5c99ea39e43b65df2b2f7d4f047
4
- data.tar.gz: 210bf58871ea3015c9ce28944018d4049d4dc89b
3
+ metadata.gz: 7bc8c0f79d2fc85c38865c3b33db892ff2a432db
4
+ data.tar.gz: 77c8ef6f4172966e2bd66e98e900522145dd19d8
5
5
  SHA512:
6
- metadata.gz: 9d05a9e405b606cb76de88b09be313ceccd17d847bf5880a96a8c6693b26e15bd287981e72ffe5fd52ed4921243aa9d5d43d67a53b526789f88e213b869028f9
7
- data.tar.gz: cbd331465a4707a54852801b57a0d5f00bce0bb5e4d82a1ef546022781239cb4e09ec51f4c2fdcad73da1c51ada46ae7dd25cad15ac7937dcfbc35a5becf4eba
6
+ metadata.gz: 8fa13fbebea0d09c30a2a84bab7c4bca6ecfc0b8c43ecdbe255c142f1115ba43a8698527149034f7f80e97c027febfb01b29ecf6571ec905f8cb5cff95a0cc57
7
+ data.tar.gz: 0c35bba2d213990e7bf7e292a566df77d788cdda194ff4136004df021c6ff2518d3b40e89359d5cb60e9827ccec1ccb4164cc0b2a34e78f6aeb2d7a93b6d8618
data/README.md CHANGED
@@ -143,6 +143,44 @@ Rod 04-14-80 Chainsaw This is a very long motto, I don't...
143
143
 
144
144
  Due to limitations of this readme format, you'll have to take my word for it that the colors placed by the 'colorize' gem are preserved, and that the table correctly handles resetting ANSI colors when truncating messages that have formatting.
145
145
 
146
+ Another little trick is that you can specify justification options by convention without using hashes for the values, simply by appending or prepending a tab character to the string. For example:
147
+
148
+ ```ruby
149
+ table_config = [
150
+ {:key=>:col1, :size=>26, :title=>"Column 1", :justify=>:right},
151
+ {:key=>:col2, :size=>26, :title=>"Column 2", :justify=>:left},
152
+ {:key=>:col3, :size=>26, :title=>"Column 3", :justify=>:center}
153
+ ]
154
+
155
+ ConsoleTable.define(table_config) do |table|
156
+
157
+ table << {
158
+ :col1=>"Right",
159
+ :col2=>"Left",
160
+ :col3=>"Center"
161
+ }
162
+
163
+ table << {
164
+ :col1=>"Left\t",
165
+ :col2=>"\tCenter\t",
166
+ :col3=>"\tRight"
167
+ }
168
+ end
169
+ ```
170
+
171
+ This will output:
172
+
173
+ ```
174
+ ================================================================================
175
+ Column 1 Column 2 Column 3
176
+ --------------------------------------------------------------------------------
177
+ Right Left Center
178
+ Left Center Right
179
+ ================================================================================
180
+ ```
181
+
182
+ Notice here how the columns all have justifications set, but the values in the second row override all of them, simply by appending a tab character for left justified, prepending a tab character for right justified, and doing both for center. The tab characters themselves are removed. Please note this convention is not supported for the column definitions using the "title" attribute, there you must use hash properties to set a column-level justification.
183
+
146
184
  You can also add a title and a footer to the table, or indent the entire table within the window using different options. Again, here's another example that should more-or-less speak for itself.
147
185
 
148
186
 
@@ -211,8 +249,4 @@ Note the alternative method of calling `<<` where you can supply an Array instea
211
249
  2. Create your feature branch (`git checkout -b my-new-feature`)
212
250
  3. Commit your changes (`git commit -am 'Add some feature'`)
213
251
  4. Push to the branch (`git push origin my-new-feature`)
214
- 5. Create new Pull Request
215
-
216
- ## Warning
217
-
218
- This gem grew out of a skunkworks project and was originally just 300 or so lines as part of a single shell script, which I moved into its own gem simply to reduce the filesize of the script. It was written and tested simply by making changes and running the script, and only when moving it to a gem did I write any tests for it at all. It is somewhat undertested and likely buggy in places that my own usage of the gem never uncovered. Please report any bugs [here](https://github.com/rodhilton/console_table/issues), but also know that I would not recommend usage of this gem for critical-path coding.
252
+ 5. Create new Pull Request
data/lib/console_table.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'terminfo'
2
2
 
3
3
  module ConsoleTable
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.4"
5
5
 
6
6
  def self.define(layout, options={}, &block)
7
7
  table = ConsoleTableClass.new(layout, options)
@@ -172,12 +172,19 @@ module ConsoleTable
172
172
  @column_widths.each_with_index do |column, i|
173
173
  to_print = options[column[:key]] || ""
174
174
  justify = column[:justify] || :left
175
+ ellipsize = column[:ellipsize] || false
176
+
175
177
  if to_print.is_a? String
176
- @out.print format(column[:size], normalize(to_print), false, justify)
178
+ justify = infer_justify_from_string(to_print, justify)
179
+
180
+ @out.print format(column[:size], normalize(to_print), ellipsize, justify)
177
181
  elsif to_print.is_a? Hash
182
+ justify = infer_justify_from_string(to_print[:text], justify)
183
+
178
184
  text = normalize(to_print[:text]) || ""
179
- ellipsize = to_print[:ellipsize] || false
180
- justify = to_print[:justify] || justify #can override
185
+
186
+ ellipsize = to_print[:ellipsize] unless to_print[:ellipsize].nil?
187
+ justify = to_print[:justify] unless to_print[:justify].nil?
181
188
 
182
189
  formatted=format(column[:size], text, ellipsize, justify)
183
190
 
@@ -197,11 +204,29 @@ module ConsoleTable
197
204
  @count = @count + 1
198
205
  end
199
206
 
207
+ def infer_justify_from_string(to_print, justify)
208
+ uncolorized = uncolorize(to_print)
209
+ if uncolorized.start_with?("\t") and uncolorized.end_with?("\t")
210
+ justify = :center
211
+ elsif uncolorized.start_with?("\t")
212
+ justify = :right
213
+ elsif uncolorized.end_with?("\t")
214
+ justify = :left
215
+ end
216
+ justify
217
+ end
218
+
200
219
  def normalize(string)
201
220
  if string.nil?
202
221
  nil
203
222
  else
204
- string.to_s.gsub(/\s+/, " ").strip #Primarily to remove any tabs or newlines
223
+ require 'pp'
224
+ normalized = string.to_s
225
+
226
+ normalized = normalized.sub(/^(\e\[\d.*?m)(\s+)/, '\2\1') #Any leading spaces preceeded by a color code should be swapped with the color code itself, so the spaces themselves aren't colored
227
+ normalized = normalized.sub(/(\s+)(\e\[\d.*?m)$/, '\2\1')
228
+
229
+ normalized.gsub(/\s+/, " ").strip #Primarily to remove any tabs or newlines
205
230
  end
206
231
  end
207
232
 
@@ -226,7 +251,7 @@ module ConsoleTable
226
251
  used_up = set_sizes.inject(:+) || 0
227
252
  available = total_width - used_up - @left_margin - @right_margin - num_spacers
228
253
 
229
- if available <= 0
254
+ if available < 0
230
255
  raise("ConsoleTable configuration invalid, current window is too small to display required sizes")
231
256
  end
232
257
 
@@ -2,6 +2,10 @@ require 'minitest/autorun'
2
2
  require 'console_table'
3
3
  require 'colorize'
4
4
 
5
+ #TODO: What happens if you have a linebreak in what you print?
6
+ # - what if it occurs inside of an active color code?
7
+ #TODO: trimming from different sides depending on justification?
8
+
5
9
  class ConsoleTableTest < Minitest::Test
6
10
 
7
11
  def setup
@@ -43,6 +47,278 @@ Row 2, Column 1 Row 2, Column 1
43
47
  assert_output_equal expected, @mock_out.string
44
48
  end
45
49
 
50
+ def test_unicode
51
+ table_config = [
52
+ {:key=>:col1, :size=>20, :title=>"Column 1"},
53
+ {:key=>:col2, :size=>20, :title=>"Column 2"},
54
+ {:key=>:col3, :size=>20, :title=>"Column 3"},
55
+ ]
56
+
57
+ ConsoleTable.define(table_config, :width=> 62, :output=>@mock_out) do |table|
58
+ table << {
59
+ :col1 => "I ♥ Unicode",
60
+ :col2 => "I ☂ Unicode",
61
+ :col3 => "I ♞ Unicode"
62
+ }
63
+
64
+ table << {
65
+ :col1 => "I ⚂ Unicode Even More",
66
+ :col2 => "I \u00A5 Unicode Even More",
67
+ :col3 => "I µ Unicode Even More"
68
+ }
69
+
70
+ table << {
71
+ :col1 => {:text => "I ⁂ Unicode", :justify=>:right},
72
+ :col2 => {:text => "I \u2190 Unicode", :justify=>:center},
73
+ :col3 => {:text => "I ✓ Unicode", :justify=>:left}
74
+ }
75
+ end
76
+
77
+ expected=<<-END
78
+ ==============================================================
79
+ Column 1 Column 2 Column 3
80
+ --------------------------------------------------------------
81
+ I ♥ Unicode I ☂ Unicode I ♞ Unicode
82
+ I ⚂ Unicode Even Mor I ¥ Unicode Even Mor I µ Unicode Even Mor
83
+ I ⁂ Unicode I ← Unicode I ✓ Unicode
84
+ ==============================================================
85
+ END
86
+
87
+ assert_output_equal expected, @mock_out.string
88
+ end
89
+
90
+ def test_spacing_convention_sets_justification
91
+ table_config = [
92
+ {:key=>:col1, :size=>20, :title=>"Column 1"},
93
+ {:key=>:col2, :size=>20, :title=>"Column 2"},
94
+ {:key=>:col3, :size=>20, :title=>"Column 3"},
95
+ ]
96
+
97
+ ConsoleTable.define(table_config, :width=> 62, :output=>@mock_out) do |table|
98
+ table << [
99
+ "\tRight",
100
+ "\tCenter\t",
101
+ "Left\t"
102
+ ]
103
+ end
104
+
105
+ expected=<<-END
106
+ ==============================================================
107
+ Column 1 Column 2 Column 3
108
+ --------------------------------------------------------------
109
+ Right Center Left
110
+ ==============================================================
111
+ END
112
+
113
+ assert_output_equal expected, @mock_out.string
114
+ end
115
+
116
+ def test_newlines_converted_to_spaces_in_middle_stripped_at_ends
117
+ table_config = [
118
+ {:key=>:col1, :size=>20, :title=>"Column 1"},
119
+ {:key=>:col2, :size=>20, :title=>"Column 2"},
120
+ {:key=>:col3, :size=>20, :title=>"Column 3"},
121
+ ]
122
+
123
+ ConsoleTable.define(table_config, :width=> 62, :output=>@mock_out) do |table|
124
+ table << [
125
+ {:text=>"Bl\nah", :justify=>:left},
126
+ {:text=>"\nStuff", :justify=>:left},
127
+ {:text=>"Junk\n", :justify=>:right}
128
+ ]
129
+ end
130
+
131
+ expected=<<-END
132
+ ==============================================================
133
+ Column 1 Column 2 Column 3
134
+ --------------------------------------------------------------
135
+ Bl ah Stuff Junk
136
+ ==============================================================
137
+ END
138
+
139
+ assert_output_equal expected, @mock_out.string
140
+ end
141
+
142
+ def test_linebreak_inside_colorcode_still_resets
143
+ table_config = [
144
+ {:key=>:col1, :size=>20, :title=>"Column 1"},
145
+ {:key=>:col2, :size=>5, :title=>"Column 2"},
146
+ ]
147
+
148
+ ConsoleTable.define(table_config, :width=> 62, :output=>@mock_out) do |table|
149
+ table << [
150
+ "Bl\nah".blue,
151
+ "1234\nStuff".red
152
+ ]
153
+ end
154
+
155
+ expected=<<-END
156
+ ==========================
157
+ Column 1 Colum
158
+ --------------------------
159
+ Bl ah 1234
160
+ ==========================
161
+ END
162
+
163
+ require 'pp'
164
+ puts @mock_out.string
165
+
166
+ assert_includes @mock_out.string, "\e[0;34;49mBl ah\e[0m" #ensure the color got reset
167
+ assert_includes @mock_out.string, "\e[0;31;49m1234 \e[0m" #ensure the color got reset
168
+
169
+ assert_output_equal expected, @mock_out.string
170
+ end
171
+
172
+ def test_can_ellipsize_at_column_level
173
+ table_config = [
174
+ {:key=>:col1, :size=>20, :title=>"Column 1", :ellipsize=>true},
175
+ {:key=>:col2, :size=>20, :title=>"Column 2"},
176
+ ]
177
+
178
+ ConsoleTable.define(table_config, :width=> 62, :output=>@mock_out) do |table|
179
+ table << [
180
+ "This is way too long to fit here",
181
+ "This is way too long to fit here",
182
+ ]
183
+
184
+ table << [
185
+ {text: "This is way too long to fit here", :ellipsize=>false},
186
+ {text: "This is way too long to fit here", :ellipsize=>true},
187
+ ]
188
+ end
189
+
190
+ expected=<<-END
191
+ =========================================
192
+ Column 1 Column 2
193
+ -----------------------------------------
194
+ This is way too l... This is way too long
195
+ This is way too long This is way too l...
196
+ =========================================
197
+ END
198
+
199
+ assert_output_equal expected, @mock_out.string
200
+ end
201
+
202
+ def test_justify_convention_followed_in_hash_text_but_overrideable
203
+ table_config = [
204
+ {:key=>:col1, :size=>20, :title=>"Column 1", :justify=>:center},
205
+ ]
206
+
207
+ ConsoleTable.define(table_config, :width=> 62, :output=>@mock_out) do |table|
208
+ table << ["Short"]
209
+ table << ["\tShort"]
210
+ table << ["Short\t"]
211
+ table << [{:text=>"Short", :justify=>:right}]
212
+ table << [{:text=>"Short", :justify=>:left}]
213
+ table << [{:text=>"\tShort"}]
214
+ table << [{:text=>"Short\t"}]
215
+ table << [{:text=>"\tShort", :justify=>:left}] #Override
216
+ table << [{:text=>"Short\t", :justify=>:right}] #Override
217
+ end
218
+
219
+ expected=<<-END
220
+ ====================
221
+ Column 1
222
+ --------------------
223
+ Short
224
+ Short
225
+ Short
226
+ Short
227
+ Short
228
+ Short
229
+ Short
230
+ Short
231
+ Short
232
+ ====================
233
+ END
234
+
235
+ puts @mock_out.string
236
+
237
+ assert_output_equal expected, @mock_out.string
238
+ end
239
+
240
+ def test_should_not_color_tabs_or_ignore_tab_justify_convention_if_inside_color
241
+ table_config = [
242
+ {:key=>:col1, :size=>20, :title=>"Column 1", :justify=>:center},
243
+ {:key=>:col2, :size=>20, :title=>"Column 2", :justify=>:right},
244
+ {:key=>:col3, :size=>20, :title=>"Column 3", :justify=>:left},
245
+ ]
246
+
247
+ ConsoleTable.define(table_config, :width=> 62, :output=>@mock_out) do |table|
248
+ table << [
249
+ "\tRight".blue,
250
+ "\tCenter\t".red,
251
+ "Left\t".magenta,
252
+ ]
253
+ end
254
+
255
+ expected=<<-END
256
+ ==============================================================
257
+ Column 1 Column 2 Column 3
258
+ --------------------------------------------------------------
259
+ Right Center Left
260
+ ==============================================================
261
+ END
262
+
263
+ assert_includes @mock_out.string, " \e[0;34;49mRight\e[0m" #space is on outside of coor
264
+ assert_includes @mock_out.string, " \e[0;35;49mLeft\e[0m " #space is on outside of color
265
+ #assert_includes @mock_out.string, " \e[0;31;49mCenter\e[0m " #this assert fails due to what I'm pretty sure is a bug in gsub(), but it's not the end of the world so I'm not doing a workaround
266
+
267
+ assert_output_equal expected, @mock_out.string
268
+ end
269
+
270
+ def test_spaces_preserved_in_middle_but_stripped_at_ends
271
+ table_config = [
272
+ {:key=>:col1, :size=>20, :title=>"Column 1"},
273
+ {:key=>:col2, :size=>20, :title=>"Column 2"},
274
+ {:key=>:col3, :size=>20, :title=>"Column 3"},
275
+ ]
276
+
277
+ ConsoleTable.define(table_config, :width=> 62, :output=>@mock_out) do |table|
278
+ table << [
279
+ {:text=>"Bl ah", :justify=>:left},
280
+ {:text=>" Stuff", :justify=>:left},
281
+ {:text=>"Junk ", :justify=>:right}
282
+ ]
283
+ end
284
+
285
+ expected=<<-END
286
+ ==============================================================
287
+ Column 1 Column 2 Column 3
288
+ --------------------------------------------------------------
289
+ Bl ah Stuff Junk
290
+ ==============================================================
291
+ END
292
+
293
+ assert_output_equal expected, @mock_out.string
294
+ end
295
+
296
+ def test_ignores_tabbing_convention_if_setting_justification_explicitly
297
+ table_config = [
298
+ {:key=>:col1, :size=>20, :title=>"Column 1"},
299
+ {:key=>:col2, :size=>20, :title=>"Column 2"},
300
+ {:key=>:col3, :size=>20, :title=>"Column 3"},
301
+ ]
302
+
303
+ ConsoleTable.define(table_config, :width=> 62, :output=>@mock_out) do |table|
304
+ table << [
305
+ {:text=>"\tBlah", :justify=>:left},
306
+ {:text=>"\tStuff\t", :justify=>:right},
307
+ {:text=>"\tJunk", :justify=>:center}
308
+ ]
309
+ end
310
+
311
+ expected=<<-END
312
+ ==============================================================
313
+ Column 1 Column 2 Column 3
314
+ --------------------------------------------------------------
315
+ Blah Stuff Junk
316
+ ==============================================================
317
+ END
318
+
319
+ assert_output_equal expected, @mock_out.string
320
+ end
321
+
46
322
  def test_can_use_convenient_operator
47
323
  table_config = [
48
324
  {:key=>:col1, :size=>20, :title=>"Column 1"},
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: console_table
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rod Hilton
@@ -112,7 +112,6 @@ files:
112
112
  - console_table_screenshot.png
113
113
  - lib/console_table.rb
114
114
  - test/test_console_table.rb
115
- - todo.txt
116
115
  homepage: https://github.com/rodhilton/console_table
117
116
  licenses:
118
117
  - MIT
data/todo.txt DELETED
@@ -1 +0,0 @@
1
- better handling of colors. should you be able to supply colored strings and the spacing is smart enough to ignore control characters?