xprint 0.6.0 → 0.7.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/version.rb +1 -1
  3. data/lib/xprint.rb +135 -19
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2f22ce4523589e6cbeb06e54a7a1977a75ec1756ab30f089732bcc8c3b45dff1
4
- data.tar.gz: fb2846b5666efd1edcc52724f9abfbb1800d519c2a38677e7307fc819e935555
3
+ metadata.gz: 0a2df6a9e5eeff509346a2faa00414a4aa87a6abb1ea6bd426edeb9f9050bda2
4
+ data.tar.gz: b6cac4edf725cf6cadf55262a013fb63d8f2dd0b4e9d7cf77d0eeef34a33854a
5
5
  SHA512:
6
- metadata.gz: 9b92e59c74c96172d0f8a30922588d0cfae72eb7eff4ad7c3ca0eaa97c6884b93a06866d70559fac46ce76818dfeb978e94f044b1a4ceccaffea7c9ecbb20304
7
- data.tar.gz: 38a4585448847ffebc5c873dafa351f16a4ef082f2d41c121161b7d6e405f4b660d648197600f332549da5d835e346c5684a21f5365ac046c5e4c223b983cfbb
6
+ metadata.gz: e5e8257c53b8577455262226b7cd69ae599c53ee82e63ae031e8124bdeb8bf557a11cbce4d8f43a17b791843a0ae0df0f2982512e7daf56e7c3c4db05a73ae49
7
+ data.tar.gz: bf0d3df1881063303b08024197ce55313573de893f339f65fd17d5dc24033953a2f963d54d2a0915579ea7e8d289a36db03b5bc07dc17dbcb5efad2d06d301f6
@@ -1,3 +1,3 @@
1
1
  module XPrint
2
- VERSION = '0.6.0'
2
+ VERSION = '0.7.9'
3
3
  end
@@ -1,3 +1,6 @@
1
+ require 'bigdecimal'
2
+ require 'date'
3
+
1
4
  module XPrint
2
5
  @data_classes = [
3
6
  String, Integer, Float, TrueClass, FalseClass, NilClass,
@@ -5,16 +8,61 @@ module XPrint
5
8
  ]
6
9
  @hash_name_classes = @data_classes + [Proc]
7
10
  @tab = "\t"
8
- @show_indexes = true
11
+ @indexes = true
9
12
  @full_proc_path = false
10
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
36
+ }
37
+ @color_codes = {
38
+ black: "\e[30m",
39
+ blue: "\e[94m",
40
+ darkblue: "\e[34m",
41
+ cyan: "\e[96m",
42
+ darkcyan: "\e[36m",
43
+ green: "\e[92m",
44
+ darkgreen: "\e[32m",
45
+ grey: "\e[37m",
46
+ darkgrey: "\e[90m",
47
+ red: "\e[91m",
48
+ darkred: "\e[31m",
49
+ purple: "\e[95m",
50
+ darkpurple: "\e[35m",
51
+ yellow: "\e[93m",
52
+ darkyellow: "\e[33m",
53
+ reset: "\e[0m"
54
+ }
11
55
 
12
56
  def self.set(**kwargs)
13
57
  set_vars = {
14
- tab: ->(data) { @tab = data },
15
- show_indexes: ->(data) { @show_indexes = data },
16
- full_proc_path: ->(data) { @full_proc_path = data },
17
- braces: ->(data) { @braces = data }
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 }
18
66
  }
19
67
 
20
68
  kwargs.each do |keyword, arg|
@@ -26,12 +74,16 @@ module XPrint
26
74
  return
27
75
  end
28
76
 
29
- def self.tab()
30
- @tab
77
+ def self.set_color_for(**kwargs)
78
+ kwargs.each do |keyword, arg|
79
+ @colors[keyword] = arg
80
+ end
31
81
  end
32
82
 
33
- def self.show_indexes()
34
- @show_indexes
83
+ def self.set_color_code_for(**kwargs)
84
+ kwargs.each do |keyword, arg|
85
+ @color_codes[keyword] = arg
86
+ end
35
87
  end
36
88
 
37
89
  def self.xp(*args)
@@ -46,6 +98,22 @@ module XPrint
46
98
  end
47
99
  end
48
100
 
101
+ private_class_method def self.color_for(colorname)
102
+ @color_codes[colorname]
103
+ end
104
+
105
+ private_class_method def self.reset_color()
106
+ @color_codes[:reset]
107
+ end
108
+
109
+ private_class_method def self.colorize(text, type)
110
+ if @color
111
+ item_color = color_for @colors[type]
112
+ "#{item_color}#{text}#{reset_color}"
113
+ else
114
+ text
115
+ end
116
+ end
49
117
 
50
118
  private_class_method def self.shift_indentation_down(text)
51
119
  # Only shift if no
@@ -68,11 +136,24 @@ module XPrint
68
136
  def self.xpand(x, indent: '', tab: "\t")
69
137
 
70
138
  _indent = "#{tab}#{indent}"
71
-
139
+
72
140
  # X is a "primitive" kind of data that has no subitems, so
73
141
  # we can just print it.
74
- if @data_classes.include? x.class
75
- return x.inspect
142
+ if x.class == String
143
+ return colorize(x.inspect, :string)
144
+ elsif x.class == Integer
145
+ return colorize(x.inspect, :integer)
146
+ elsif x.class == Float
147
+ return colorize(x.inspect, :float)
148
+ elsif x.class == TrueClass
149
+ return colorize(x.inspect, :true)
150
+ elsif x.class == FalseClass
151
+ return colorize(x.inspect, :false)
152
+ elsif x.class == NilClass
153
+ return colorize(x.inspect, :nil)
154
+ elsif x.class == Symbol
155
+ return colorize(x.inspect, :symbol)
156
+
76
157
  # X is a Proc, print more compact version of standard Proc.inspect
77
158
  # text.
78
159
  elsif x.class == Proc
@@ -84,10 +165,12 @@ module XPrint
84
165
  source = source.split('/')[-2..-1].join('/')
85
166
  end
86
167
 
87
- return "<#{type} @ #{source} [Line #{line}]>"
88
- # X is an Array, print list of all items.
168
+ return colorize("<#{type} @ #{source} [Line #{line}]>", :proc)
169
+
89
170
  elsif x.class == Class
90
- return "<Class #{x}>"
171
+ return colorize("<Class #{x}>", :classobject)
172
+
173
+ # X is an Array, print list of all items.
91
174
  elsif x.class == Array
92
175
  result = "#{@braces ? '[' : ''}\n"
93
176
 
@@ -95,7 +178,7 @@ module XPrint
95
178
  data = xpand(item, indent: _indent, tab: tab)
96
179
 
97
180
  result += "#{_indent}"
98
- result += "[#{index}] " if @show_indexes
181
+ result += "#{colorize("[#{index}]", :index)} " if @indexes
99
182
  result += "#{data}"
100
183
 
101
184
  unless index + 1 == x.length
@@ -105,6 +188,7 @@ module XPrint
105
188
 
106
189
  result += "\n#{indent}]" if @braces
107
190
  return result
191
+
108
192
  # X is a Hash, print all keys and values.
109
193
  elsif x.class == Hash
110
194
  result = "#{@braces ? '{' : ''}\n"
@@ -120,6 +204,13 @@ module XPrint
120
204
  )
121
205
 
122
206
  longest_key = 0 if longest_key.nil?
207
+
208
+ # Color codes throw the text length, so we need to add the
209
+ # length of the color code and the code to reset the color
210
+ # that wrap around the colored word.
211
+ # The color code is like "\e[99m" and the reset "\e[0m",
212
+ # so the total length to add when using color is 9.
213
+ longest_key += 9 if @color
123
214
 
124
215
  x.each_with_index do |(key, value), index|
125
216
  data_key = "#{xpand(key, indent: _indent, tab: tab)}"
@@ -137,20 +228,42 @@ module XPrint
137
228
  result += "#{_indent}#{data_key} => #{data_value}"
138
229
 
139
230
  unless index + 1 == x.length
140
- result += ", \n"
231
+ result += "#{@braces ? ', ' : ''} \n"
141
232
  end
142
233
  end
143
234
 
144
235
  result += "\n#{indent}}" if @braces
145
236
 
146
237
  return result
238
+
239
+ # X is a commonly used special kind of object.
240
+ elsif x.class == DateTime
241
+ datetime = x.strftime @datetime_format
242
+ return colorize("DateTime(#{datetime})", :datetime)
243
+
244
+ elsif x.class == Date
245
+ date = x.strftime @date_format
246
+ return colorize("Date(#{date})", :date)
247
+
248
+ elsif x.class == Time
249
+ time = x.strftime @time_format
250
+ return colorize("Time(#{time})", :time)
251
+
252
+ elsif x.class == BigDecimal
253
+ return colorize("BigDecimal(#{x.to_s('f')})", :bigdecimal)
254
+
255
+ elsif x.class == Rational
256
+ return colorize("Rational(#{x})", :rational)
257
+
147
258
  # X is a Structure; essentially a special case of X being an object.
148
259
  elsif x.is_a? Struct
149
- result = "Struct #{x.class}#{@braces ? '(' : ''}\n"
260
+ struct_word = colorize('Struct', :classname)
261
+ classname = colorize(x.class, :classname)
262
+ result = "#{struct_word} #{classname}#{@braces ? '(' : ''}\n"
150
263
  longest_item = x.members.map { |m| m.to_s.length }.max()
151
264
 
152
265
  x.each_pair do |name, value|
153
- attr_name = name.to_s.ljust(longest_item)
266
+ attr_name = colorize(name.to_s.ljust(longest_item), :attribute)
154
267
  attr_data = xpand(value, indent: _indent, tab: tab)
155
268
 
156
269
  result += "#{_indent}#{attr_name} = #{attr_data}\n"
@@ -159,9 +272,11 @@ module XPrint
159
272
  result += "#{indent})" if @braces
160
273
 
161
274
  return result
275
+
162
276
  # X is any arbitrary object; print all instance variables.
163
277
  else
164
278
  classname = x.class == Module ? "Module #{x}" : x.class
279
+ classname = colorize(classname, :classname)
165
280
  result = "#{classname}#{@braces ? '(' : ''}"
166
281
  ivars = x.instance_variables
167
282
  result += "\n" if ivars.length > 0
@@ -169,6 +284,7 @@ module XPrint
169
284
 
170
285
  ivars.each_with_index do |var, index|
171
286
  attr_name = var.to_s.ljust(longest_var)
287
+ attr_name = colorize(attr_name, :attribute)
172
288
  attr_data = xpand(
173
289
  x.instance_variable_get(var),
174
290
  indent: _indent,
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xprint
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - JCabr