xprint 0.7.9 → 0.9.5

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 +146 -69
  4. metadata +4 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0a2df6a9e5eeff509346a2faa00414a4aa87a6abb1ea6bd426edeb9f9050bda2
4
- data.tar.gz: b6cac4edf725cf6cadf55262a013fb63d8f2dd0b4e9d7cf77d0eeef34a33854a
3
+ metadata.gz: 22338ca33f15d9dedc61c673da48fe9ca2393ec2f3da632371bae995eeac2c11
4
+ data.tar.gz: e8ec4ae742f88be96a58841d550d870866f871454fe1f178f20c0ceaa130ad61
5
5
  SHA512:
6
- metadata.gz: e5e8257c53b8577455262226b7cd69ae599c53ee82e63ae031e8124bdeb8bf557a11cbce4d8f43a17b791843a0ae0df0f2982512e7daf56e7c3c4db05a73ae49
7
- data.tar.gz: bf0d3df1881063303b08024197ce55313573de893f339f65fd17d5dc24033953a2f963d54d2a0915579ea7e8d289a36db03b5bc07dc17dbcb5efad2d06d301f6
6
+ metadata.gz: 144471fa158be54e4fdb2c7e8ab7b5696dae11824ba9944f3bd5c2138c138ebe575a27abcefb3cf25ade46bb09fc48e588d4baca1c2cb5b2f1cb3bed05c12689
7
+ data.tar.gz: c6d3de4b9333e7edc0304eb2a31ecc4aebe8f536bd6bbc59d124122261ff9762da3901bb52e22762f2f17d315bfa9fce6b827e5b731319ef6304cb9dc7f84d89
@@ -1,3 +1,3 @@
1
1
  module XPrint
2
- VERSION = '0.7.9'
2
+ VERSION = '0.9.5'
3
3
  end
@@ -1,40 +1,53 @@
1
1
  require 'bigdecimal'
2
2
  require 'date'
3
+ require 'yaml'
3
4
 
5
+ # TODO: Add ability for alphabetically sorting
6
+ # items in hashes and objects.
4
7
  module XPrint
5
- @data_classes = [
8
+ @data_classes = [
6
9
  String, Integer, Float, TrueClass, FalseClass, NilClass,
7
- Symbol
10
+ Symbol, Date, Time, DateTime, BigDecimal, Rational
8
11
  ]
9
12
  @hash_name_classes = @data_classes + [Proc]
10
- @tab = "\t"
11
- @indexes = true
12
- @full_proc_path = false
13
- @braces = true
14
- @date_format = '%F'
15
- @time_format = '%c'
16
- @datetime_format = '%FT%T%:z'
17
- @color = false
18
- @colors = {
19
- attribute: :blue,
20
- bigdecimal: :darkcyan,
21
- classname: :darkgreen,
22
- classobject: :green,
23
- date: :red,
24
- datetime: :purple,
25
- false: :darkred,
26
- float: :cyan,
27
- index: :darkgrey,
28
- integer: :cyan,
29
- nil: :darkpurple,
30
- proc: :darkyellow,
31
- rational: :darkcyan,
32
- string: :yellow,
33
- symbol: :darkblue,
34
- time: :darkblue,
35
- true: :darkgreen
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
36
49
  }
37
- @color_codes = {
50
+ @color_codes = {
38
51
  black: "\e[30m",
39
52
  blue: "\e[94m",
40
53
  darkblue: "\e[34m",
@@ -50,30 +63,59 @@ module XPrint
50
63
  darkpurple: "\e[35m",
51
64
  yellow: "\e[93m",
52
65
  darkyellow: "\e[33m",
53
- reset: "\e[0m"
66
+ default: "\e[0m"
54
67
  }
55
68
 
56
69
  def self.set(**kwargs)
57
- set_vars = {
58
- tab: ->(data) { @tab = data },
59
- indexes: ->(data) { @indexes = data },
60
- full_proc_path: ->(data) { @full_proc_path = data },
61
- braces: ->(data) { @braces = data },
62
- date_format: ->(data) { @date_format = data },
63
- time_format: ->(data) { @time_format = data },
64
- datetime_format: ->(data) { @datetime_format = data },
65
- color: ->(data) { @color = data }
66
- }
67
-
68
70
  kwargs.each do |keyword, arg|
69
- if set_vars.key? keyword
70
- set_vars[keyword].(arg)
71
- end
71
+ self.instance_variable_set "@#{keyword}", arg
72
72
  end
73
73
 
74
74
  return
75
75
  end
76
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
+
77
119
  def self.set_color_for(**kwargs)
78
120
  kwargs.each do |keyword, arg|
79
121
  @colors[keyword] = arg
@@ -103,7 +145,7 @@ module XPrint
103
145
  end
104
146
 
105
147
  private_class_method def self.reset_color()
106
- @color_codes[:reset]
148
+ @color_codes[:default]
107
149
  end
108
150
 
109
151
  private_class_method def self.colorize(text, type)
@@ -133,7 +175,7 @@ module XPrint
133
175
  return result
134
176
  end
135
177
 
136
- def self.xpand(x, indent: '', tab: "\t")
178
+ def self.xpand(x, indent: '', tab: ' ')
137
179
 
138
180
  _indent = "#{tab}#{indent}"
139
181
 
@@ -172,26 +214,38 @@ module XPrint
172
214
 
173
215
  # X is an Array, print list of all items.
174
216
  elsif x.class == Array
175
- result = "#{@braces ? '[' : ''}\n"
217
+ result = "#{@braces ? colorize('[', :square_brace) : ''}\n"
218
+ comma = colorize(',', :comma)
176
219
 
177
220
  x.each_with_index do |item, index|
178
221
  data = xpand(item, indent: _indent, tab: tab)
179
222
 
180
223
  result += "#{_indent}"
181
- result += "#{colorize("[#{index}]", :index)} " if @indexes
224
+ if @indexes
225
+ adjustment = x.length.to_s.length + 3
226
+ # Account for characters used for color coding.
227
+ adjustment += 9 if @color
228
+
229
+ result += "#{colorize("[#{index}]", :index)} ".
230
+ ljust(adjustment)
231
+ end
182
232
  result += "#{data}"
183
233
 
184
234
  unless index + 1 == x.length
185
- result += "#{@braces ? ', ' : ''} \n"
235
+ show_commas = @commas && @braces
236
+
237
+ result += "#{show_commas ? "#{comma}" : ''}"
238
+ result += "\n" unless result.end_with? "\n"
186
239
  end
187
240
  end
188
241
 
189
- result += "\n#{indent}]" if @braces
242
+ result += "\n#{indent}#{colorize(']', :square_brace)}" if @braces
190
243
  return result
191
244
 
192
245
  # X is a Hash, print all keys and values.
193
246
  elsif x.class == Hash
194
- result = "#{@braces ? '{' : ''}\n"
247
+ comma = colorize(',', :comma)
248
+ result = "#{@braces ? colorize('{', :curly_brace) : ''}\n"
195
249
 
196
250
  longest_key = (
197
251
  x.keys.filter do |k, v|
@@ -225,62 +279,84 @@ module XPrint
225
279
 
226
280
  data_value = xpand(value, indent: _indent, tab: tab)
227
281
 
228
- result += "#{_indent}#{data_key} => #{data_value}"
282
+ hash_separator = colorize(@hash_separator, :hash_separator)
283
+ result += "#{_indent}#{data_key}#{hash_separator}#{data_value}"
229
284
 
230
285
  unless index + 1 == x.length
231
- result += "#{@braces ? ', ' : ''} \n"
286
+ show_commas = @commas && @braces
287
+ result += "#{show_commas ? "#{comma} " : ''}"
288
+ result += "\n" unless result.end_with? "\n"
232
289
  end
233
290
  end
234
291
 
235
- result += "\n#{indent}}" if @braces
292
+ result += "\n#{indent}#{colorize('}', :curly_brace)}" if @braces
236
293
 
237
294
  return result
238
295
 
239
296
  # X is a commonly used special kind of object.
240
297
  elsif x.class == DateTime
241
298
  datetime = x.strftime @datetime_format
242
- return colorize("DateTime(#{datetime})", :datetime)
299
+ p1 = colorize('(', :parentheses)
300
+ p2 = colorize(')', :parentheses)
301
+ return colorize("DateTime#{p1}#{datetime}#{p2}", :datetime)
243
302
 
244
303
  elsif x.class == Date
245
304
  date = x.strftime @date_format
246
- return colorize("Date(#{date})", :date)
305
+ p1 = colorize('(', :parentheses)
306
+ p2 = colorize(')', :parentheses)
307
+ return colorize("Date#{p1}#{date}#{p2}", :date)
247
308
 
248
309
  elsif x.class == Time
249
310
  time = x.strftime @time_format
250
- return colorize("Time(#{time})", :time)
311
+ p1 = colorize('(', :parentheses)
312
+ p2 = colorize(')', :parentheses)
313
+ return colorize("Time#{p1}#{time}#{p2}", :time)
251
314
 
252
315
  elsif x.class == BigDecimal
253
- return colorize("BigDecimal(#{x.to_s('f')})", :bigdecimal)
316
+ p1 = colorize('(', :parentheses)
317
+ p2 = colorize(')', :parentheses)
318
+ return colorize("BigDecimal#{p1}#{x.to_s('f')}#{p2}", :bigdecimal)
254
319
 
255
320
  elsif x.class == Rational
256
- return colorize("Rational(#{x})", :rational)
321
+ p1 = colorize('(', :parentheses)
322
+ p2 = colorize(')', :parentheses)
323
+ return colorize("Rational#{p1}#{x}#{p2}", :rational)
257
324
 
258
325
  # X is a Structure; essentially a special case of X being an object.
259
326
  elsif x.is_a? Struct
260
- struct_word = colorize('Struct', :classname)
261
- classname = colorize(x.class, :classname)
262
- result = "#{struct_word} #{classname}#{@braces ? '(' : ''}\n"
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"
263
332
  longest_item = x.members.map { |m| m.to_s.length }.max()
333
+ eq_sign = colorize('=', :equals)
264
334
 
265
335
  x.each_pair do |name, value|
266
336
  attr_name = colorize(name.to_s.ljust(longest_item), :attribute)
267
337
  attr_data = xpand(value, indent: _indent, tab: tab)
268
338
 
269
- result += "#{_indent}#{attr_name} = #{attr_data}\n"
339
+ result += "#{_indent}#{attr_name} #{eq_sign} #{attr_data}"
340
+ result += "\n" unless result.end_with? "\n"
270
341
  end
271
342
 
272
- result += "#{indent})" if @braces
343
+ result += "#{indent}#{p2}" if @braces
273
344
 
274
345
  return result
275
346
 
276
347
  # X is any arbitrary object; print all instance variables.
277
348
  else
278
- classname = x.class == Module ? "Module #{x}" : x.class
279
- classname = colorize(classname, :classname)
280
- result = "#{classname}#{@braces ? '(' : ''}"
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 : ''}"
281
356
  ivars = x.instance_variables
282
357
  result += "\n" if ivars.length > 0
283
358
  longest_var = ivars.map { |v| v.to_s.length }.max()
359
+ eq_sign = colorize('=', :equals)
284
360
 
285
361
  ivars.each_with_index do |var, index|
286
362
  attr_name = var.to_s.ljust(longest_var)
@@ -291,10 +367,11 @@ module XPrint
291
367
  tab: tab
292
368
  )
293
369
 
294
- result += "#{_indent}#{attr_name} = #{attr_data}\n"
370
+ result += "#{_indent}#{attr_name} #{eq_sign} #{attr_data}"
371
+ result += "\n" unless result.end_with? "\n"
295
372
  end
296
373
 
297
- result += "#{ivars.length > 0 ? indent: ''})" if @braces
374
+ result += "#{ivars.length > 0 ? indent: ''}#{p2}" if @braces
298
375
 
299
376
  return result
300
377
  end
metadata CHANGED
@@ -1,17 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xprint
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.9
4
+ version: 0.9.5
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-26 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Gem that allows for pretty printing data over multiple lines and with
14
- indentation, and works with objects as well as basic data types.
14
+ indentation, and works with objects as well as basic data types. Also allows color,
15
+ and loading settings via a YAML config file.
15
16
  email:
16
17
  - jcabr.dev@gmail.com
17
18
  executables: []