xprint 0.5.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 +189 -25
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a2cc60a347d36661e3bb0fcc6d1ae58ab46f236eeefd8ba40fc869a1cd20439a
4
- data.tar.gz: c0c48517b86a17e3a4219bc9734cd7cf19548883c05b4ba15f3fee783d2ed5d9
3
+ metadata.gz: 0a2df6a9e5eeff509346a2faa00414a4aa87a6abb1ea6bd426edeb9f9050bda2
4
+ data.tar.gz: b6cac4edf725cf6cadf55262a013fb63d8f2dd0b4e9d7cf77d0eeef34a33854a
5
5
  SHA512:
6
- metadata.gz: 9f46bcb7bc0b0536bb83803b623c625dc91c17151ea4bf47173bf50d5def8f1c45408fef73c117742757f4b7f9db6724b0c8255fe1db7f269c2003b022ebe4be
7
- data.tar.gz: 611aceb397a9dcc19788be147711cc253e7b348d39c7dd15880c3476718d9d9c0254f11c47e690ed077aede60a2bc20a5148b52ba61ca62045204672fb670578
6
+ metadata.gz: e5e8257c53b8577455262226b7cd69ae599c53ee82e63ae031e8124bdeb8bf557a11cbce4d8f43a17b791843a0ae0df0f2982512e7daf56e7c3c4db05a73ae49
7
+ data.tar.gz: bf0d3df1881063303b08024197ce55313573de893f339f65fd17d5dc24033953a2f963d54d2a0915579ea7e8d289a36db03b5bc07dc17dbcb5efad2d06d301f6
@@ -1,3 +1,3 @@
1
1
  module XPrint
2
- VERSION = '0.5.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,63 +8,190 @@ module XPrint
5
8
  ]
6
9
  @hash_name_classes = @data_classes + [Proc]
7
10
  @tab = "\t"
8
- @show_indexes = true
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
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
+ }
9
55
 
10
56
  def self.set(**kwargs)
11
- @tab = kwargs[:tab] unless kwargs[:tab].nil?
12
- @show_indexes = kwargs[:show_indexes] unless kwargs[:show_indexes].nil?
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
+ kwargs.each do |keyword, arg|
69
+ if set_vars.key? keyword
70
+ set_vars[keyword].(arg)
71
+ end
72
+ end
73
+
74
+ return
13
75
  end
14
76
 
15
- def self.tab()
16
- @tab
77
+ def self.set_color_for(**kwargs)
78
+ kwargs.each do |keyword, arg|
79
+ @colors[keyword] = arg
80
+ end
17
81
  end
18
82
 
19
- def self.show_indexes()
20
- @show_indexes
83
+ def self.set_color_code_for(**kwargs)
84
+ kwargs.each do |keyword, arg|
85
+ @color_codes[keyword] = arg
86
+ end
21
87
  end
22
88
 
23
89
  def self.xp(*args)
24
90
  args.each do |arg|
25
- puts self.xpand(arg, tab: @tab)
91
+ xpanded_text = self.xpand(arg, tab: @tab)
92
+
93
+ unless @braces
94
+ xpanded_text = self.shift_indentation_down(xpanded_text).lstrip()
95
+ end
96
+
97
+ puts xpanded_text
26
98
  end
27
99
  end
28
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
117
+
118
+ private_class_method def self.shift_indentation_down(text)
119
+ # Only shift if no
120
+ return text if text.match?(/^\S/)
121
+ result = ''
122
+
123
+ text.each_line do |line|
124
+ result += (
125
+ if line.start_with? @tab
126
+ line[@tab.length..-1]
127
+ else
128
+ line
129
+ end
130
+ )
131
+ end
132
+
133
+ return result
134
+ end
135
+
29
136
  def self.xpand(x, indent: '', tab: "\t")
30
137
 
31
138
  _indent = "#{tab}#{indent}"
32
-
139
+
33
140
  # X is a "primitive" kind of data that has no subitems, so
34
141
  # we can just print it.
35
- if @data_classes.include? x.class
36
- 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
+
37
157
  # X is a Proc, print more compact version of standard Proc.inspect
38
158
  # text.
39
159
  elsif x.class == Proc
160
+ type = x.lambda? ? 'Lambda' : 'Proc'
40
161
  source, line = x.source_location
41
- source = source.gsub('\\', '/').split('/')[-2..-1].join('/')
162
+ source = source.gsub('\\', '/')
42
163
 
43
- return "<Proc @ #{source} [Line #{line}]>"
164
+ unless @full_proc_path
165
+ source = source.split('/')[-2..-1].join('/')
166
+ end
167
+
168
+ return colorize("<#{type} @ #{source} [Line #{line}]>", :proc)
169
+
170
+ elsif x.class == Class
171
+ return colorize("<Class #{x}>", :classobject)
172
+
44
173
  # X is an Array, print list of all items.
45
174
  elsif x.class == Array
46
- result = "[\n"
175
+ result = "#{@braces ? '[' : ''}\n"
47
176
 
48
177
  x.each_with_index do |item, index|
49
178
  data = xpand(item, indent: _indent, tab: tab)
50
179
 
51
180
  result += "#{_indent}"
52
- result += "[#{index}] " if @show_indexes
181
+ result += "#{colorize("[#{index}]", :index)} " if @indexes
53
182
  result += "#{data}"
54
183
 
55
184
  unless index + 1 == x.length
56
- result += ", \n"
185
+ result += "#{@braces ? ', ' : ''} \n"
57
186
  end
58
187
  end
59
188
 
60
- result += "\n#{indent}]"
189
+ result += "\n#{indent}]" if @braces
61
190
  return result
191
+
62
192
  # X is a Hash, print all keys and values.
63
193
  elsif x.class == Hash
64
- result = "{\n"
194
+ result = "#{@braces ? '{' : ''}\n"
65
195
 
66
196
  longest_key = (
67
197
  x.keys.filter do |k, v|
@@ -74,6 +204,13 @@ module XPrint
74
204
  )
75
205
 
76
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
77
214
 
78
215
  x.each_with_index do |(key, value), index|
79
216
  data_key = "#{xpand(key, indent: _indent, tab: tab)}"
@@ -91,36 +228,63 @@ module XPrint
91
228
  result += "#{_indent}#{data_key} => #{data_value}"
92
229
 
93
230
  unless index + 1 == x.length
94
- result += ", \n"
231
+ result += "#{@braces ? ', ' : ''} \n"
95
232
  end
96
233
  end
97
234
 
98
- result += "\n#{indent}}"
235
+ result += "\n#{indent}}" if @braces
99
236
 
100
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
+
101
258
  # X is a Structure; essentially a special case of X being an object.
102
259
  elsif x.is_a? Struct
103
- result = "Struct #{x.class}(\n"
260
+ struct_word = colorize('Struct', :classname)
261
+ classname = colorize(x.class, :classname)
262
+ result = "#{struct_word} #{classname}#{@braces ? '(' : ''}\n"
104
263
  longest_item = x.members.map { |m| m.to_s.length }.max()
105
264
 
106
265
  x.each_pair do |name, value|
107
- attr_name = name.to_s.ljust(longest_item)
266
+ attr_name = colorize(name.to_s.ljust(longest_item), :attribute)
108
267
  attr_data = xpand(value, indent: _indent, tab: tab)
109
268
 
110
269
  result += "#{_indent}#{attr_name} = #{attr_data}\n"
111
270
  end
112
271
 
113
- result += "#{indent})"
272
+ result += "#{indent})" if @braces
114
273
 
115
274
  return result
275
+
116
276
  # X is any arbitrary object; print all instance variables.
117
277
  else
118
- result = "#{x.class}(\n"
278
+ classname = x.class == Module ? "Module #{x}" : x.class
279
+ classname = colorize(classname, :classname)
280
+ result = "#{classname}#{@braces ? '(' : ''}"
119
281
  ivars = x.instance_variables
282
+ result += "\n" if ivars.length > 0
120
283
  longest_var = ivars.map { |v| v.to_s.length }.max()
121
284
 
122
285
  ivars.each_with_index do |var, index|
123
286
  attr_name = var.to_s.ljust(longest_var)
287
+ attr_name = colorize(attr_name, :attribute)
124
288
  attr_data = xpand(
125
289
  x.instance_variable_get(var),
126
290
  indent: _indent,
@@ -130,7 +294,7 @@ module XPrint
130
294
  result += "#{_indent}#{attr_name} = #{attr_data}\n"
131
295
  end
132
296
 
133
- result += "#{indent})"
297
+ result += "#{ivars.length > 0 ? indent: ''})" if @braces
134
298
 
135
299
  return result
136
300
  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.5.0
4
+ version: 0.7.9
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-20 00:00:00.000000000 Z
11
+ date: 2020-07-21 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.