xprint 0.9.5 → 0.9.52

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/version.rb +2 -2
  3. data/lib/xprint.rb +353 -349
  4. metadata +6 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 22338ca33f15d9dedc61c673da48fe9ca2393ec2f3da632371bae995eeac2c11
4
- data.tar.gz: e8ec4ae742f88be96a58841d550d870866f871454fe1f178f20c0ceaa130ad61
3
+ metadata.gz: 28bf765d84d5c5524bb16b450ec410d38458ac07eda20dde13671fb4d616b075
4
+ data.tar.gz: dae561c390ce9de47468327ecddfc32e997780b310f802384adbd358cb5c8138
5
5
  SHA512:
6
- metadata.gz: 144471fa158be54e4fdb2c7e8ab7b5696dae11824ba9944f3bd5c2138c138ebe575a27abcefb3cf25ade46bb09fc48e588d4baca1c2cb5b2f1cb3bed05c12689
7
- data.tar.gz: c6d3de4b9333e7edc0304eb2a31ecc4aebe8f536bd6bbc59d124122261ff9762da3901bb52e22762f2f17d315bfa9fce6b827e5b731319ef6304cb9dc7f84d89
6
+ metadata.gz: 851b4ded37108b45c3bfb879038ce7c0256259f1b1f410f262f8bb97cb52fa36f000f30fa898bbfbb6de49415f2d908ff42f87fa7cb50d91e1732a45e14499bf
7
+ data.tar.gz: 3368c16e1ad863e91dbdc339774f78e898979e584bb82c257758b598c2c1d836f9e801c9584624f10461d4bed5e5bac2ef7ad042cbd60f252c892ed0ce576b9c
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module XPrint
2
- VERSION = '0.9.5'
3
- end
2
+ VERSION = '0.9.52'
3
+ end
data/lib/xprint.rb CHANGED
@@ -5,383 +5,387 @@ require 'yaml'
5
5
  # TODO: Add ability for alphabetically sorting
6
6
  # items in hashes and objects.
7
7
  module XPrint
8
- @data_classes = [
9
- String, Integer, Float, TrueClass, FalseClass, NilClass,
10
- Symbol, Date, Time, DateTime, BigDecimal, Rational
11
- ]
12
- @hash_name_classes = @data_classes + [Proc]
13
- @tab = " "
14
- @indexes = true
15
- @full_proc_path = false
16
- @braces = true
17
- @date_format = '%F'
18
- @time_format = '%c'
19
- @datetime_format = '%FT%T%:z'
20
- @hash_separator = ' => '
21
- @commas = true
22
- @color = true
23
- @colors = {
24
- attribute: :blue,
25
- bigdecimal: :darkcyan,
26
- classname: :darkgreen,
27
- classobject: :green,
28
- comma: :default,
29
- curly_brace: :default,
30
- date: :red,
31
- datetime: :purple,
32
- equals: :default,
33
- false: :darkred,
34
- float: :cyan,
35
- hash_separator: :default,
36
- index: :darkgrey,
37
- integer: :cyan,
38
- module: :green,
39
- nil: :darkpurple,
40
- parentheses: :default,
41
- proc: :darkyellow,
42
- rational: :darkcyan,
43
- string: :yellow,
44
- struct: :green,
45
- square_brace: :default,
46
- symbol: :darkblue,
47
- time: :darkblue,
48
- true: :darkgreen
49
- }
50
- @color_codes = {
51
- black: "\e[30m",
52
- blue: "\e[94m",
53
- darkblue: "\e[34m",
54
- cyan: "\e[96m",
55
- darkcyan: "\e[36m",
56
- green: "\e[92m",
57
- darkgreen: "\e[32m",
58
- grey: "\e[37m",
59
- darkgrey: "\e[90m",
60
- red: "\e[91m",
61
- darkred: "\e[31m",
62
- purple: "\e[95m",
63
- darkpurple: "\e[35m",
64
- yellow: "\e[93m",
65
- darkyellow: "\e[33m",
66
- default: "\e[0m"
67
- }
68
-
69
- def self.set(**kwargs)
70
- kwargs.each do |keyword, arg|
71
- self.instance_variable_set "@#{keyword}", arg
72
- end
73
-
74
- return
8
+ @data_classes = [
9
+ String, Integer, Float, TrueClass, FalseClass, NilClass,
10
+ Symbol, Date, Time, DateTime, BigDecimal, Rational
11
+ ]
12
+ @hash_name_classes = @data_classes + [Proc]
13
+ @tab = " "
14
+ @indexes = true
15
+ @full_proc_path = false
16
+ @braces = true
17
+ @date_format = '%F'
18
+ @time_format = '%c'
19
+ @datetime_format = '%FT%T%:z'
20
+ @hash_separator = ' => '
21
+ @commas = true
22
+ @color = true
23
+ @colors = {
24
+ attribute: :blue,
25
+ bigdecimal: :darkcyan,
26
+ classname: :darkgreen,
27
+ classobject: :green,
28
+ comma: :default,
29
+ curly_brace: :default,
30
+ date: :red,
31
+ datetime: :purple,
32
+ equals: :default,
33
+ false: :darkred,
34
+ float: :cyan,
35
+ hash_separator: :default,
36
+ index: :darkgrey,
37
+ integer: :cyan,
38
+ module: :green,
39
+ nil: :darkpurple,
40
+ parentheses: :default,
41
+ proc: :darkyellow,
42
+ rational: :darkcyan,
43
+ regexp: :darkyellow,
44
+ string: :yellow,
45
+ struct: :green,
46
+ square_brace: :default,
47
+ symbol: :darkblue,
48
+ time: :darkblue,
49
+ true: :darkgreen
50
+ }
51
+ @color_codes = {
52
+ black: "\e[30m",
53
+ blue: "\e[94m",
54
+ darkblue: "\e[34m",
55
+ cyan: "\e[96m",
56
+ darkcyan: "\e[36m",
57
+ green: "\e[92m",
58
+ darkgreen: "\e[32m",
59
+ grey: "\e[37m",
60
+ darkgrey: "\e[90m",
61
+ red: "\e[91m",
62
+ darkred: "\e[31m",
63
+ purple: "\e[95m",
64
+ darkpurple: "\e[35m",
65
+ yellow: "\e[93m",
66
+ darkyellow: "\e[33m",
67
+ default: "\e[0m"
68
+ }
69
+
70
+ def self.set(**kwargs)
71
+ kwargs.each do |keyword, arg|
72
+ instance_variable_set "@#{keyword}", arg
75
73
  end
76
-
77
- def self.load_file(config)
78
- config_data = YAML.load( File.read config )
79
- config_data = self.symbolize_keys(config_data)
80
-
81
- if config_data.key? :general
82
- self.set **config_data[:general]
83
- end
84
-
85
- if config_data.key? :colors
86
- color_data = config_data[:colors]
87
-
88
- color_data.each do |name, color|
89
- color_data[name] = color.to_sym
90
- end
91
-
92
- self.set_color_for **config_data[:colors]
93
- end
94
-
95
- if config_data.key? :'color codes'
96
- self.set_color_code_for **config_data[:'color codes']
97
- end
98
- end
99
-
100
- def self.load(config)
101
- calling_file = caller_locations.first.absolute_path
102
- base_dir = File.dirname calling_file
103
- relative_config = File.expand_path config, base_dir
104
-
105
- self.load_file relative_config
106
- end
107
-
108
- private_class_method def self.symbolize_keys(hash)
109
- hash.inject({}) do |result, (key, value)|
110
- new_key = key.to_sym
111
- new_value = value.is_a?(Hash) ? symbolize_keys(value) : value
112
-
113
- result[new_key] = new_value
114
-
115
- result
116
- end
117
- end
118
-
119
- def self.set_color_for(**kwargs)
120
- kwargs.each do |keyword, arg|
121
- @colors[keyword] = arg
122
- end
123
- end
124
-
125
- def self.set_color_code_for(**kwargs)
126
- kwargs.each do |keyword, arg|
127
- @color_codes[keyword] = arg
128
- end
74
+
75
+ return
76
+ end
77
+
78
+ def self.load_file(config)
79
+ config_data = YAML.load(File.read config)
80
+ config_data = symbolize_keys(config_data)
81
+
82
+ set **config_data[:general] if config_data.key?
83
+
84
+ if config_data.key? :colors
85
+ color_data = config_data[:colors]
86
+
87
+ color_data.each do |name, color|
88
+ color_data[name] = color.to_sym
89
+ end
90
+
91
+ set_color_for **config_data[:colors]
129
92
  end
93
+
94
+ codekey = :'color codes'
95
+
96
+ set_color_code_for(**config_data[codekey]) if config_data.key codekey
97
+
98
+ return
99
+ end
100
+
101
+ def self.load(config)
102
+ calling_file = caller_locations.first.absolute_path
103
+ base_dir = File.dirname calling_file
104
+ relative_config = File.expand_path config, base_dir
105
+
106
+ load_file relative_config
107
+ end
130
108
 
131
- def self.xp(*args)
132
- args.each do |arg|
133
- xpanded_text = self.xpand(arg, tab: @tab)
134
109
 
135
- unless @braces
136
- xpanded_text = self.shift_indentation_down(xpanded_text).lstrip()
137
- end
138
110
 
139
- puts xpanded_text
140
- end
111
+ private_class_method def self.symbolize_keys(hash)
112
+ hash.inject({}) do |result, (key, value)|
113
+ new_key = key.to_sym
114
+ new_value = value.is_a?(Hash) ? symbolize_keys(value) : value
115
+
116
+ result[new_key] = new_value
117
+
118
+ result
141
119
  end
142
-
143
- private_class_method def self.color_for(colorname)
144
- @color_codes[colorname]
120
+ end
121
+
122
+ def self.set_color_for(**kwargs)
123
+ kwargs.each do |keyword, arg|
124
+ @colors[keyword] = arg
145
125
  end
146
-
147
- private_class_method def self.reset_color()
148
- @color_codes[:default]
126
+ end
127
+
128
+ def self.set_color_code_for(**kwargs)
129
+ kwargs.each do |keyword, arg|
130
+ @color_codes[keyword] = arg
149
131
  end
150
-
151
- private_class_method def self.colorize(text, type)
152
- if @color
153
- item_color = color_for @colors[type]
154
- "#{item_color}#{text}#{reset_color}"
155
- else
156
- text
157
- end
132
+ end
133
+
134
+ def self.xp(*args)
135
+ args.each do |arg|
136
+ xpanded_text = xpand(arg, tab: @tab)
137
+
138
+ xpanded_text = shift_indentation_down(xpanded_text).lstrip unless @braces
139
+
140
+ puts xpanded_text
158
141
  end
159
-
160
- private_class_method def self.shift_indentation_down(text)
161
- # Only shift if no
162
- return text if text.match?(/^\S/)
163
- result = ''
164
-
165
- text.each_line do |line|
166
- result += (
167
- if line.start_with? @tab
168
- line[@tab.length..-1]
169
- else
170
- line
171
- end
172
- )
173
- end
174
-
175
- return result
142
+ end
143
+
144
+ private_class_method def self.color_for(colorname)
145
+ @color_codes[colorname]
146
+ end
147
+
148
+ private_class_method def self.reset_color()
149
+ @color_codes[:default]
150
+ end
151
+
152
+ private_class_method def self.colorize(text, type)
153
+ if @color
154
+ item_color = color_for @colors[type]
155
+ "#{item_color}#{text}#{reset_color}"
156
+ else
157
+ text
176
158
  end
177
-
178
- def self.xpand(x, indent: '', tab: ' ')
179
-
180
- _indent = "#{tab}#{indent}"
181
-
182
- # X is a "primitive" kind of data that has no subitems, so
183
- # we can just print it.
184
- if x.class == String
185
- return colorize(x.inspect, :string)
186
- elsif x.class == Integer
187
- return colorize(x.inspect, :integer)
188
- elsif x.class == Float
189
- return colorize(x.inspect, :float)
190
- elsif x.class == TrueClass
191
- return colorize(x.inspect, :true)
192
- elsif x.class == FalseClass
193
- return colorize(x.inspect, :false)
194
- elsif x.class == NilClass
195
- return colorize(x.inspect, :nil)
196
- elsif x.class == Symbol
197
- return colorize(x.inspect, :symbol)
198
-
199
- # X is a Proc, print more compact version of standard Proc.inspect
200
- # text.
201
- elsif x.class == Proc
202
- type = x.lambda? ? 'Lambda' : 'Proc'
203
- source, line = x.source_location
204
- source = source.gsub('\\', '/')
205
-
206
- unless @full_proc_path
207
- source = source.split('/')[-2..-1].join('/')
208
- end
209
-
210
- return colorize("<#{type} @ #{source} [Line #{line}]>", :proc)
211
-
212
- elsif x.class == Class
213
- return colorize("<Class #{x}>", :classobject)
214
-
215
- # X is an Array, print list of all items.
216
- elsif x.class == Array
217
- result = "#{@braces ? colorize('[', :square_brace) : ''}\n"
218
- comma = colorize(',', :comma)
159
+ end
160
+
161
+ private_class_method def self.shift_indentation_down(text)
162
+ # Only shift if no
163
+ return text if text.match?(/^\S/)
219
164
 
220
- x.each_with_index do |item, index|
221
- data = xpand(item, indent: _indent, tab: tab)
222
-
223
- result += "#{_indent}"
224
- if @indexes
225
- adjustment = x.length.to_s.length + 3
226
- # Account for characters used for color coding.
227
- adjustment += 9 if @color
165
+ result = ''
166
+
167
+ text.each_line do |line|
168
+ result += (
169
+ if line.start_with? @tab
170
+ line[@tab.length..-1]
171
+ else
172
+ line
173
+ end
174
+ )
175
+ end
228
176
 
229
- result += "#{colorize("[#{index}]", :index)} ".
230
- ljust(adjustment)
231
- end
232
- result += "#{data}"
177
+ return result
178
+ end
179
+
180
+ def self.xpand(x, indent: '', tab: ' ')
233
181
 
234
- unless index + 1 == x.length
235
- show_commas = @commas && @braces
236
-
237
- result += "#{show_commas ? "#{comma}" : ''}"
238
- result += "\n" unless result.end_with? "\n"
239
- end
240
- end
182
+ _indent = "#{tab}#{indent}"
241
183
 
242
- result += "\n#{indent}#{colorize(']', :square_brace)}" if @braces
243
- return result
244
-
245
- # X is a Hash, print all keys and values.
246
- elsif x.class == Hash
247
- comma = colorize(',', :comma)
248
- result = "#{@braces ? colorize('{', :curly_brace) : ''}\n"
249
-
250
- longest_key = (
251
- x.keys.filter do |k, v|
252
- @hash_name_classes.include? k.class
253
- end.
254
- map do |k, v|
255
- k.to_s.length
256
- end.
257
- max()
258
- )
259
-
260
- longest_key = 0 if longest_key.nil?
261
-
262
- # Color codes throw the text length, so we need to add the
263
- # length of the color code and the code to reset the color
264
- # that wrap around the colored word.
265
- # The color code is like "\e[99m" and the reset "\e[0m",
266
- # so the total length to add when using color is 9.
267
- longest_key += 9 if @color
184
+ # X is a "primitive" kind of data that has no subitems, so
185
+ # we can just print it.
186
+ if x.class == String
187
+ return colorize(x.inspect, :string)
188
+ elsif x.class == Integer
189
+ return colorize(x.inspect, :integer)
190
+ elsif x.class == Float
191
+ return colorize(x.inspect, :float)
192
+ elsif x.class == TrueClass
193
+ return colorize(x.inspect, :true)
194
+ elsif x.class == FalseClass
195
+ return colorize(x.inspect, :false)
196
+ elsif x.class == NilClass
197
+ return colorize(x.inspect, :nil)
198
+ elsif x.class == Symbol
199
+ return colorize(x.inspect, :symbol)
200
+ elsif x.class == Regexp
201
+ return colorize(x.inspect, :regexp)
202
+
203
+ # X is a Proc, print more compact version of standard Proc.inspect
204
+ # text.
205
+ elsif x.class == Proc
206
+ type = x.lambda? ? 'Lambda' : 'Proc'
207
+ source, line = x.source_location
208
+ source = source.gsub('\\', '/')
209
+
210
+ unless @full_proc_path
211
+ source = source.split('/')[-2..-1].join('/')
212
+ end
213
+
214
+ return colorize("<#{type} @ #{source} [Line #{line}]>", :proc)
268
215
 
269
- x.each_with_index do |(key, value), index|
270
- data_key = "#{xpand(key, indent: _indent, tab: tab)}"
271
-
272
- data_key = (
273
- if @hash_name_classes.include? key.class
274
- data_key.ljust(longest_key + 1)
275
- else
276
- data_key.ljust(data_key.length + longest_key)
277
- end
278
- )
279
-
280
- data_value = xpand(value, indent: _indent, tab: tab)
281
-
282
- hash_separator = colorize(@hash_separator, :hash_separator)
283
- result += "#{_indent}#{data_key}#{hash_separator}#{data_value}"
284
-
285
- unless index + 1 == x.length
286
- show_commas = @commas && @braces
287
- result += "#{show_commas ? "#{comma} " : ''}"
288
- result += "\n" unless result.end_with? "\n"
289
- end
290
- end
291
-
292
- result += "\n#{indent}#{colorize('}', :curly_brace)}" if @braces
293
-
294
- return result
295
-
296
- # X is a commonly used special kind of object.
297
- elsif x.class == DateTime
298
- datetime = x.strftime @datetime_format
299
- p1 = colorize('(', :parentheses)
300
- p2 = colorize(')', :parentheses)
301
- return colorize("DateTime#{p1}#{datetime}#{p2}", :datetime)
216
+ elsif x.class == Class
217
+ return colorize("<Class #{x}>", :classobject)
218
+
219
+ # X is an Array, print list of all items.
220
+ elsif x.class == Array
221
+ result = "#{@braces ? colorize('[', :square_brace) : ''}\n"
222
+ comma = colorize(',', :comma)
223
+
224
+ x.each_with_index do |item, index|
225
+ data = xpand(item, indent: _indent, tab: tab)
302
226
 
303
- elsif x.class == Date
304
- date = x.strftime @date_format
305
- p1 = colorize('(', :parentheses)
306
- p2 = colorize(')', :parentheses)
307
- return colorize("Date#{p1}#{date}#{p2}", :date)
227
+ result += "#{_indent}"
228
+ if @indexes
229
+ adjustment = x.length.to_s.length + 3
230
+ # Account for characters used for color coding.
231
+ adjustment += 9 if @color
232
+
233
+ result += "#{colorize("[#{index}]", :index)} ".
234
+ ljust(adjustment)
235
+ end
236
+ result += "#{data}"
308
237
 
309
- elsif x.class == Time
310
- time = x.strftime @time_format
311
- p1 = colorize('(', :parentheses)
312
- p2 = colorize(')', :parentheses)
313
- return colorize("Time#{p1}#{time}#{p2}", :time)
238
+ unless index + 1 == x.length
239
+ show_commas = @commas && @braces
240
+
241
+ result += "#{show_commas ? "#{comma}" : ''}"
242
+ result += "\n" unless result.end_with? "\n"
243
+ end
244
+ end
245
+
246
+ result += "\n#{indent}#{colorize(']', :square_brace)}" if @braces
247
+ return result
248
+
249
+ # X is a Hash, print all keys and values.
250
+ elsif x.class == Hash
251
+ comma = colorize(',', :comma)
252
+ result = "#{@braces ? colorize('{', :curly_brace) : ''}\n"
253
+
254
+ longest_key = (
255
+ x.keys.filter do |k, v|
256
+ @hash_name_classes.include? k.class
257
+ end.
258
+ map do |k, v|
259
+ k.to_s.length
260
+ end.
261
+ max()
262
+ )
263
+
264
+ longest_key = 0 if longest_key.nil?
265
+
266
+ # Color codes throw the text length, so we need to add the
267
+ # length of the color code and the code to reset the color
268
+ # that wrap around the colored word.
269
+ # The color code is like "\e[99m" and the reset "\e[0m",
270
+ # so the total length to add when using color is 9.
271
+ longest_key += 9 if @color
272
+
273
+ x.each_with_index do |(key, value), index|
274
+ data_key = "#{xpand(key, indent: _indent, tab: tab)}"
314
275
 
315
- elsif x.class == BigDecimal
316
- p1 = colorize('(', :parentheses)
317
- p2 = colorize(')', :parentheses)
318
- return colorize("BigDecimal#{p1}#{x.to_s('f')}#{p2}", :bigdecimal)
276
+ data_key = (
277
+ if @hash_name_classes.include? key.class
278
+ data_key.ljust(longest_key + 1)
279
+ else
280
+ data_key.ljust(data_key.length + longest_key)
281
+ end
282
+ )
319
283
 
320
- elsif x.class == Rational
321
- p1 = colorize('(', :parentheses)
322
- p2 = colorize(')', :parentheses)
323
- return colorize("Rational#{p1}#{x}#{p2}", :rational)
284
+ data_value = xpand(value, indent: _indent, tab: tab)
324
285
 
325
- # X is a Structure; essentially a special case of X being an object.
326
- elsif x.is_a? Struct
327
- struct_word = colorize('Struct', :struct)
328
- classname = colorize(x.class, :struct)
329
- p1 = colorize('(', :parentheses)
330
- p2 = colorize(')', :parentheses)
331
- result = "#{struct_word} #{classname}#{@braces ? p1 : ''}\n"
332
- longest_item = x.members.map { |m| m.to_s.length }.max()
333
- eq_sign = colorize('=', :equals)
334
-
335
- x.each_pair do |name, value|
336
- attr_name = colorize(name.to_s.ljust(longest_item), :attribute)
337
- attr_data = xpand(value, indent: _indent, tab: tab)
338
-
339
- result += "#{_indent}#{attr_name} #{eq_sign} #{attr_data}"
340
- result += "\n" unless result.end_with? "\n"
341
- end
342
-
343
- result += "#{indent}#{p2}" if @braces
344
-
345
- return result
286
+ hash_separator = colorize(@hash_separator, :hash_separator)
287
+ result += "#{_indent}#{data_key}#{hash_separator}#{data_value}"
346
288
 
347
- # X is any arbitrary object; print all instance variables.
348
- else
349
- p1 = colorize('(', :parentheses)
350
- p2 = colorize(')', :parentheses)
351
-
352
- is_module = x.class == Module
353
- classname = is_module ? "Module #{x}" : x.class
354
- classname = colorize(classname, is_module ? :module : :classname)
355
- result = "#{classname}#{@braces ? p1 : ''}"
356
- ivars = x.instance_variables
357
- result += "\n" if ivars.length > 0
358
- longest_var = ivars.map { |v| v.to_s.length }.max()
359
- eq_sign = colorize('=', :equals)
289
+ unless index + 1 == x.length
290
+ show_commas = @commas && @braces
291
+ result += "#{show_commas ? "#{comma} " : ''}"
292
+ result += "\n" unless result.end_with? "\n"
293
+ end
294
+ end
295
+
296
+ result += "\n#{indent}#{colorize('}', :curly_brace)}" if @braces
297
+
298
+ return result
299
+
300
+ # X is a commonly used special kind of object.
301
+ elsif x.class == DateTime
302
+ datetime = x.strftime @datetime_format
303
+ p1 = colorize('(', :parentheses)
304
+ p2 = colorize(')', :parentheses)
305
+ return colorize("DateTime#{p1}#{datetime}#{p2}", :datetime)
360
306
 
361
- ivars.each_with_index do |var, index|
362
- attr_name = var.to_s.ljust(longest_var)
363
- attr_name = colorize(attr_name, :attribute)
364
- attr_data = xpand(
365
- x.instance_variable_get(var),
366
- indent: _indent,
367
- tab: tab
368
- )
369
-
370
- result += "#{_indent}#{attr_name} #{eq_sign} #{attr_data}"
371
- result += "\n" unless result.end_with? "\n"
372
- end
307
+ elsif x.class == Date
308
+ date = x.strftime @date_format
309
+ p1 = colorize('(', :parentheses)
310
+ p2 = colorize(')', :parentheses)
311
+ return colorize("Date#{p1}#{date}#{p2}", :date)
373
312
 
374
- result += "#{ivars.length > 0 ? indent: ''}#{p2}" if @braces
313
+ elsif x.class == Time
314
+ time = x.strftime @time_format
315
+ p1 = colorize('(', :parentheses)
316
+ p2 = colorize(')', :parentheses)
317
+ return colorize("Time#{p1}#{time}#{p2}", :time)
375
318
 
376
- return result
377
- end
378
- end
319
+ elsif x.class == BigDecimal
320
+ p1 = colorize('(', :parentheses)
321
+ p2 = colorize(')', :parentheses)
322
+ return colorize("BigDecimal#{p1}#{x.to_s('f')}#{p2}", :bigdecimal)
323
+
324
+ elsif x.class == Rational
325
+ p1 = colorize('(', :parentheses)
326
+ p2 = colorize(')', :parentheses)
327
+ return colorize("Rational#{p1}#{x}#{p2}", :rational)
328
+
329
+ # X is a Structure; essentially a special case of X being an object.
330
+ elsif x.is_a? Struct
331
+ struct_word = colorize('Struct', :struct)
332
+ classname = colorize(x.class, :struct)
333
+ p1 = colorize('(', :parentheses)
334
+ p2 = colorize(')', :parentheses)
335
+ result = "#{struct_word} #{classname}#{@braces ? p1 : ''}\n"
336
+ longest_item = x.members.map { |m| m.to_s.length }.max()
337
+ eq_sign = colorize('=', :equals)
338
+
339
+ x.each_pair do |name, value|
340
+ attr_name = colorize(name.to_s.ljust(longest_item), :attribute)
341
+ attr_data = xpand(value, indent: _indent, tab: tab)
342
+
343
+ result += "#{_indent}#{attr_name} #{eq_sign} #{attr_data}"
344
+ result += "\n" unless result.end_with? "\n"
345
+ end
346
+
347
+ result += "#{indent}#{p2}" if @braces
348
+
349
+ return result
350
+
351
+ # X is any arbitrary object; print all instance variables.
352
+ else
353
+ p1 = colorize('(', :parentheses)
354
+ p2 = colorize(')', :parentheses)
355
+
356
+ is_module = x.class == Module
357
+ classname = is_module ? "Module #{x}" : x.class
358
+ classname = colorize(classname, is_module ? :module : :classname)
359
+ result = "#{classname}#{@braces ? p1 : ''}"
360
+ ivars = x.instance_variables
361
+ result += "\n" if ivars.length > 0
362
+ longest_var = ivars.map { |v| v.to_s.length }.max()
363
+ eq_sign = colorize('=', :equals)
364
+
365
+ ivars.each_with_index do |var, index|
366
+ attr_name = var.to_s.ljust(longest_var)
367
+ attr_name = colorize(attr_name, :attribute)
368
+ attr_data = xpand(
369
+ x.instance_variable_get(var),
370
+ indent: _indent,
371
+ tab: tab
372
+ )
373
+
374
+ result += "#{_indent}#{attr_name} #{eq_sign} #{attr_data}"
375
+ result += "\n" unless result.end_with? "\n"
376
+ end
377
+
378
+ result += "#{ivars.length > 0 ? indent : ''}#{p2}" if @braces
379
+
380
+ return result
381
+ end
382
+ end
379
383
  end
380
384
 
381
385
  def xp(*args)
382
- XPrint::xp(*args)
386
+ XPrint.xp(*args)
383
387
  end
384
388
 
385
389
  def xpand(item, tab: "\t")
386
- XPrint::xpand(item, tab: tab)
390
+ XPrint.xpand(item, tab: tab)
387
391
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xprint
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.5
4
+ version: 0.9.52
5
5
  platform: ruby
6
6
  authors:
7
7
  - JCabr
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-26 00:00:00.000000000 Z
11
+ date: 2021-05-24 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Gem that allows for pretty printing data over multiple lines and with
14
14
  indentation, and works with objects as well as basic data types. Also allows color,
@@ -25,7 +25,7 @@ homepage: https://github.com/JCabr/xprint.rb
25
25
  licenses:
26
26
  - MIT
27
27
  metadata: {}
28
- post_install_message:
28
+ post_install_message:
29
29
  rdoc_options: []
30
30
  require_paths:
31
31
  - lib
@@ -40,8 +40,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
40
40
  - !ruby/object:Gem::Version
41
41
  version: '0'
42
42
  requirements: []
43
- rubygems_version: 3.1.2
44
- signing_key:
43
+ rubygems_version: 3.2.15
44
+ signing_key:
45
45
  specification_version: 4
46
46
  summary: Gem for pretty printing any kind of object.
47
47
  test_files: []