xprint 0.9.1 → 0.9.3

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 +1 -1
  3. data/lib/xprint.rb +79 -64
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 72c5d81ad083539ab972c38b51405d5275034ce535f4014696257dbec0d6e5ab
4
- data.tar.gz: 950f5d948e5098902d02d25eea8a62c1ec146a0dd58bb052bed139df50d06512
3
+ metadata.gz: cb5466745989b6f6b023b74f46af89fd0dfb6ff6c10ced23260209bb288aa80d
4
+ data.tar.gz: e434c017113835cf167843c788eff0ae215336ec0922fcb14ff3e0b4da8092cc
5
5
  SHA512:
6
- metadata.gz: 33fb5f501119a0cb8015797c93f72516276eb332a03b9dba5f01696965a2ab2b8d06659724c8e12dcb3c9dd287df0311a6a11b48e9bef51fdf5bc329e9d114b5
7
- data.tar.gz: 11492f4674dcd327edf54cfe909c1c830236ce798f34c830a8ac5f74b36e860ea3548d44af465a72a5ca73af90c8ff5ce3d92f8f8fc429baff92e8d0c2bafaa4
6
+ metadata.gz: af1bf6ed25d0ec37d81e06cb1dec4d4608b4f4cf6e247aa072df8673014dc4f0a6411b0db2edfe6dd32289353d3662c6368eb3148b277edb69182bb756ef0b93
7
+ data.tar.gz: 8465b18e98ec0d5c71e3e0536d86db8789c7b8625017627175570c883801ce77e951d3e4fc03f5ef949ac61cd1c15c6f123de514718149d28185315971d4544b
@@ -1,3 +1,3 @@
1
1
  module XPrint
2
- VERSION = '0.9.1'
2
+ VERSION = '0.9.3'
3
3
  end
@@ -3,41 +3,49 @@ require 'date'
3
3
  require 'yaml'
4
4
 
5
5
  module XPrint
6
- @data_classes = [
6
+ @data_classes = [
7
7
  String, Integer, Float, TrueClass, FalseClass, NilClass,
8
8
  Symbol, Date, Time, DateTime, BigDecimal, Rational
9
9
  ]
10
10
  @hash_name_classes = @data_classes + [Proc]
11
- @tab = " "
12
- @indexes = true
13
- @full_proc_path = false
14
- @braces = true
15
- @date_format = '%F'
16
- @time_format = '%c'
17
- @datetime_format = '%FT%T%:z'
18
- @color = true
19
- @colors = {
20
- attribute: :blue,
21
- bigdecimal: :darkcyan,
22
- classname: :darkgreen,
23
- classobject: :green,
24
- date: :red,
25
- datetime: :purple,
26
- false: :darkred,
27
- float: :cyan,
28
- index: :darkgrey,
29
- integer: :cyan,
30
- module: :green,
31
- nil: :darkpurple,
32
- proc: :darkyellow,
33
- rational: :darkcyan,
34
- string: :yellow,
35
- struct: :green,
36
- symbol: :darkblue,
37
- time: :darkblue,
38
- true: :darkgreen
11
+ @tab = " "
12
+ @indexes = true
13
+ @full_proc_path = false
14
+ @braces = true
15
+ @date_format = '%F'
16
+ @time_format = '%c'
17
+ @datetime_format = '%FT%T%:z'
18
+ @hash_separator = ' => '
19
+ @commas = true
20
+ @color = true
21
+ @colors = {
22
+ attribute: :blue,
23
+ bigdecimal: :darkcyan,
24
+ classname: :darkgreen,
25
+ classobject: :green,
26
+ comma: :default,
27
+ curly_brace: :default,
28
+ date: :red,
29
+ datetime: :purple,
30
+ equals: :default,
31
+ false: :darkred,
32
+ float: :cyan,
33
+ hash_separator: :default,
34
+ index: :darkgrey,
35
+ integer: :cyan,
36
+ module: :green,
37
+ nil: :darkpurple,
38
+ parentheses: :default,
39
+ proc: :darkyellow,
40
+ rational: :darkcyan,
41
+ string: :yellow,
42
+ struct: :green,
43
+ square_brace: :default,
44
+ symbol: :darkblue,
45
+ time: :darkblue,
46
+ true: :darkgreen
39
47
  }
40
- @color_codes = {
48
+ @color_codes = {
41
49
  black: "\e[30m",
42
50
  blue: "\e[94m",
43
51
  darkblue: "\e[34m",
@@ -53,25 +61,12 @@ module XPrint
53
61
  darkpurple: "\e[35m",
54
62
  yellow: "\e[93m",
55
63
  darkyellow: "\e[33m",
56
- reset: "\e[0m"
64
+ default: "\e[0m"
57
65
  }
58
66
 
59
67
  def self.set(**kwargs)
60
- set_vars = {
61
- tab: ->(data) { @tab = data },
62
- indexes: ->(data) { @indexes = data },
63
- full_proc_path: ->(data) { @full_proc_path = data },
64
- braces: ->(data) { @braces = data },
65
- date_format: ->(data) { @date_format = data },
66
- time_format: ->(data) { @time_format = data },
67
- datetime_format: ->(data) { @datetime_format = data },
68
- color: ->(data) { @color = data }
69
- }
70
-
71
68
  kwargs.each do |keyword, arg|
72
- if set_vars.key? keyword
73
- set_vars[keyword].(arg)
74
- end
69
+ self.instance_variable_set "@#{keyword}", arg
75
70
  end
76
71
 
77
72
  return
@@ -148,7 +143,7 @@ module XPrint
148
143
  end
149
144
 
150
145
  private_class_method def self.reset_color()
151
- @color_codes[:reset]
146
+ @color_codes[:default]
152
147
  end
153
148
 
154
149
  private_class_method def self.colorize(text, type)
@@ -217,7 +212,8 @@ module XPrint
217
212
 
218
213
  # X is an Array, print list of all items.
219
214
  elsif x.class == Array
220
- result = "#{@braces ? '[' : ''}\n"
215
+ result = "#{@braces ? colorize('[', :square_brace) : ''}\n"
216
+ comma = colorize(',', :comma)
221
217
 
222
218
  x.each_with_index do |item, index|
223
219
  data = xpand(item, indent: _indent, tab: tab)
@@ -234,16 +230,17 @@ module XPrint
234
230
  result += "#{data}"
235
231
 
236
232
  unless index + 1 == x.length
237
- result += "#{@braces ? ', ' : ''} \n"
233
+ result += "#{@commas ? "#{comma} " : ''} \n"
238
234
  end
239
235
  end
240
236
 
241
- result += "\n#{indent}]" if @braces
237
+ result += "\n#{indent}#{colorize(']', :square_brace)}" if @braces
242
238
  return result
243
239
 
244
240
  # X is a Hash, print all keys and values.
245
241
  elsif x.class == Hash
246
- result = "#{@braces ? '{' : ''}\n"
242
+ comma = colorize(',', :comma)
243
+ result = "#{@braces ? colorize('{', :curly_brace) : ''}\n"
247
244
 
248
245
  longest_key = (
249
246
  x.keys.filter do |k, v|
@@ -277,63 +274,81 @@ module XPrint
277
274
 
278
275
  data_value = xpand(value, indent: _indent, tab: tab)
279
276
 
280
- result += "#{_indent}#{data_key} => #{data_value}"
277
+ hash_separator = colorize(@hash_separator, :hash_separator)
278
+ result += "#{_indent}#{data_key}#{hash_separator}#{data_value}"
281
279
 
282
280
  unless index + 1 == x.length
283
- result += "#{@braces ? ', ' : ''} \n"
281
+ result += "#{@commas ? "#{comma} " : ''} \n"
284
282
  end
285
283
  end
286
284
 
287
- result += "\n#{indent}}" if @braces
285
+ result += "\n#{indent}#{colorize('}', :curly_brace)}" if @braces
288
286
 
289
287
  return result
290
288
 
291
289
  # X is a commonly used special kind of object.
292
290
  elsif x.class == DateTime
293
291
  datetime = x.strftime @datetime_format
294
- return colorize("DateTime(#{datetime})", :datetime)
292
+ p1 = colorize('(', :parentheses)
293
+ p2 = colorize(')', :parentheses)
294
+ return colorize("DateTime#{p1}#{datetime}#{p2}", :datetime)
295
295
 
296
296
  elsif x.class == Date
297
297
  date = x.strftime @date_format
298
- return colorize("Date(#{date})", :date)
298
+ p1 = colorize('(', :parentheses)
299
+ p2 = colorize(')', :parentheses)
300
+ return colorize("Date#{p1}#{date}#{p2}", :date)
299
301
 
300
302
  elsif x.class == Time
301
303
  time = x.strftime @time_format
302
- return colorize("Time(#{time})", :time)
304
+ p1 = colorize('(', :parentheses)
305
+ p2 = colorize(')', :parentheses)
306
+ return colorize("Time#{p1}#{time}#{p2}", :time)
303
307
 
304
308
  elsif x.class == BigDecimal
305
- return colorize("BigDecimal(#{x.to_s('f')})", :bigdecimal)
309
+ p1 = colorize('(', :parentheses)
310
+ p2 = colorize(')', :parentheses)
311
+ return colorize("BigDecimal#{p1}#{x.to_s('f')}#{p2}", :bigdecimal)
306
312
 
307
313
  elsif x.class == Rational
308
- return colorize("Rational(#{x})", :rational)
314
+ p1 = colorize('(', :parentheses)
315
+ p2 = colorize(')', :parentheses)
316
+ return colorize("Rational#{p1}#{x}#{p2}", :rational)
309
317
 
310
318
  # X is a Structure; essentially a special case of X being an object.
311
319
  elsif x.is_a? Struct
312
320
  struct_word = colorize('Struct', :struct)
313
321
  classname = colorize(x.class, :struct)
314
- result = "#{struct_word} #{classname}#{@braces ? '(' : ''}\n"
322
+ p1 = colorize('(', :parentheses)
323
+ p2 = colorize(')', :parentheses)
324
+ result = "#{struct_word} #{classname}#{@braces ? p1 : ''}\n"
315
325
  longest_item = x.members.map { |m| m.to_s.length }.max()
326
+ eq_sign = colorize('=', :equals)
316
327
 
317
328
  x.each_pair do |name, value|
318
329
  attr_name = colorize(name.to_s.ljust(longest_item), :attribute)
319
330
  attr_data = xpand(value, indent: _indent, tab: tab)
320
331
 
321
- result += "#{_indent}#{attr_name} = #{attr_data}\n"
332
+ result += "#{_indent}#{attr_name} #{eq_sign} #{attr_data}\n"
322
333
  end
323
334
 
324
- result += "#{indent})" if @braces
335
+ result += "#{indent}#{p2}" if @braces
325
336
 
326
337
  return result
327
338
 
328
339
  # X is any arbitrary object; print all instance variables.
329
340
  else
341
+ p1 = colorize('(', :parentheses)
342
+ p2 = colorize(')', :parentheses)
343
+
330
344
  is_module = x.class == Module
331
345
  classname = is_module ? "Module #{x}" : x.class
332
346
  classname = colorize(classname, is_module ? :module : :classname)
333
- result = "#{classname}#{@braces ? '(' : ''}"
347
+ result = "#{classname}#{@braces ? p1 : ''}"
334
348
  ivars = x.instance_variables
335
349
  result += "\n" if ivars.length > 0
336
350
  longest_var = ivars.map { |v| v.to_s.length }.max()
351
+ eq_sign = colorize('=', :equals)
337
352
 
338
353
  ivars.each_with_index do |var, index|
339
354
  attr_name = var.to_s.ljust(longest_var)
@@ -344,10 +359,10 @@ module XPrint
344
359
  tab: tab
345
360
  )
346
361
 
347
- result += "#{_indent}#{attr_name} = #{attr_data}\n"
362
+ result += "#{_indent}#{attr_name} #{eq_sign} #{attr_data}\n"
348
363
  end
349
364
 
350
- result += "#{ivars.length > 0 ? indent: ''})" if @braces
365
+ result += "#{ivars.length > 0 ? indent: ''}#{p2}" if @braces
351
366
 
352
367
  return result
353
368
  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.1
4
+ version: 0.9.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - JCabr
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-21 00:00:00.000000000 Z
11
+ date: 2020-07-22 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,