xprint 0.5.1 → 0.8.1

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 +193 -27
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 990179163a84db36aa76ba3ba878bb21641efbb5720823cc635d37b61a95721b
4
- data.tar.gz: 513f72918e49b0ff51aecf7181ab4b2a2e288d01d791a6c67b951abbe40fdcdc
3
+ metadata.gz: d8e6e0778467e8ed1efb112fc5a6fb7ee2e5463e1338dbffaf73aeeb61a75d0f
4
+ data.tar.gz: 43fef229c2551c8791d0b009cac768524fdb929a014701f16ed360981a014a7d
5
5
  SHA512:
6
- metadata.gz: c8087e594b361566fe9375e6c77296474a40b5454d24bf7b5e139640f2c033e71d2b9555fadcdac9e1843d5436aaa14b0b928247bd0ffba343994a8a4ff2f903
7
- data.tar.gz: d4435f361143d2d9c5660f634256b6eb7444261b40bfd00337699bd6a9370b3f4c93d9581167083dfab0ddd99b62d93866c5461b3ef66830fad358cd8819c56f
6
+ metadata.gz: 301cc656d0b4d0e62c6cf0c13486866b4249e7668700f11bcd9e58d2c4a28cac9a033b9ed3de430f64afde2ccb46f008a46e232f4ef5ca943e5371d9b17ecde9
7
+ data.tar.gz: 3ef89b719424f34e220ffc9aa77d4591dddb19fc695a64c257144fe3d1bf068b3569c8896b70ff85048a6fad0a4e4bcb6f722cd0af4c541259091228ff229c19
@@ -1,3 +1,3 @@
1
1
  module XPrint
2
- VERSION = '0.5.1'
2
+ VERSION = '0.8.1'
3
3
  end
@@ -1,68 +1,199 @@
1
+ require 'bigdecimal'
2
+ require 'date'
3
+
1
4
  module XPrint
2
5
  @data_classes = [
3
6
  String, Integer, Float, TrueClass, FalseClass, NilClass,
4
- Symbol
7
+ Symbol, Date, Time, DateTime, BigDecimal, Rational
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
+ module: :green,
30
+ nil: :darkpurple,
31
+ proc: :darkyellow,
32
+ rational: :darkcyan,
33
+ string: :yellow,
34
+ struct: :green,
35
+ symbol: :darkblue,
36
+ time: :darkblue,
37
+ true: :darkgreen
38
+ }
39
+ @color_codes = {
40
+ black: "\e[30m",
41
+ blue: "\e[94m",
42
+ darkblue: "\e[34m",
43
+ cyan: "\e[96m",
44
+ darkcyan: "\e[36m",
45
+ green: "\e[92m",
46
+ darkgreen: "\e[32m",
47
+ grey: "\e[37m",
48
+ darkgrey: "\e[90m",
49
+ red: "\e[91m",
50
+ darkred: "\e[31m",
51
+ purple: "\e[95m",
52
+ darkpurple: "\e[35m",
53
+ yellow: "\e[93m",
54
+ darkyellow: "\e[33m",
55
+ reset: "\e[0m"
56
+ }
9
57
 
10
58
  def self.set(**kwargs)
11
- @tab = kwargs[:tab] unless kwargs[:tab].nil?
12
- @show_indexes = kwargs[:show_indexes] unless kwargs[:show_indexes].nil?
59
+ set_vars = {
60
+ tab: ->(data) { @tab = data },
61
+ indexes: ->(data) { @indexes = data },
62
+ full_proc_path: ->(data) { @full_proc_path = data },
63
+ braces: ->(data) { @braces = data },
64
+ date_format: ->(data) { @date_format = data },
65
+ time_format: ->(data) { @time_format = data },
66
+ datetime_format: ->(data) { @datetime_format = data },
67
+ color: ->(data) { @color = data }
68
+ }
69
+
70
+ kwargs.each do |keyword, arg|
71
+ if set_vars.key? keyword
72
+ set_vars[keyword].(arg)
73
+ end
74
+ end
75
+
76
+ return
13
77
  end
14
78
 
15
- def self.tab()
16
- @tab
79
+ def self.set_color_for(**kwargs)
80
+ kwargs.each do |keyword, arg|
81
+ @colors[keyword] = arg
82
+ end
17
83
  end
18
84
 
19
- def self.show_indexes()
20
- @show_indexes
85
+ def self.set_color_code_for(**kwargs)
86
+ kwargs.each do |keyword, arg|
87
+ @color_codes[keyword] = arg
88
+ end
21
89
  end
22
90
 
23
91
  def self.xp(*args)
24
92
  args.each do |arg|
25
- puts self.xpand(arg, tab: @tab)
93
+ xpanded_text = self.xpand(arg, tab: @tab)
94
+
95
+ unless @braces
96
+ xpanded_text = self.shift_indentation_down(xpanded_text).lstrip()
97
+ end
98
+
99
+ puts xpanded_text
100
+ end
101
+ end
102
+
103
+ private_class_method def self.color_for(colorname)
104
+ @color_codes[colorname]
105
+ end
106
+
107
+ private_class_method def self.reset_color()
108
+ @color_codes[:reset]
109
+ end
110
+
111
+ private_class_method def self.colorize(text, type)
112
+ if @color
113
+ item_color = color_for @colors[type]
114
+ "#{item_color}#{text}#{reset_color}"
115
+ else
116
+ text
117
+ end
118
+ end
119
+
120
+ private_class_method def self.shift_indentation_down(text)
121
+ # Only shift if no
122
+ return text if text.match?(/^\S/)
123
+ result = ''
124
+
125
+ text.each_line do |line|
126
+ result += (
127
+ if line.start_with? @tab
128
+ line[@tab.length..-1]
129
+ else
130
+ line
131
+ end
132
+ )
26
133
  end
134
+
135
+ return result
27
136
  end
28
137
 
29
138
  def self.xpand(x, indent: '', tab: "\t")
30
139
 
31
140
  _indent = "#{tab}#{indent}"
32
-
141
+
33
142
  # X is a "primitive" kind of data that has no subitems, so
34
143
  # we can just print it.
35
- if @data_classes.include? x.class
36
- return x.inspect
144
+ if x.class == String
145
+ return colorize(x.inspect, :string)
146
+ elsif x.class == Integer
147
+ return colorize(x.inspect, :integer)
148
+ elsif x.class == Float
149
+ return colorize(x.inspect, :float)
150
+ elsif x.class == TrueClass
151
+ return colorize(x.inspect, :true)
152
+ elsif x.class == FalseClass
153
+ return colorize(x.inspect, :false)
154
+ elsif x.class == NilClass
155
+ return colorize(x.inspect, :nil)
156
+ elsif x.class == Symbol
157
+ return colorize(x.inspect, :symbol)
158
+
37
159
  # X is a Proc, print more compact version of standard Proc.inspect
38
160
  # text.
39
161
  elsif x.class == Proc
40
162
  type = x.lambda? ? 'Lambda' : 'Proc'
41
163
  source, line = x.source_location
42
- source = source.gsub('\\', '/').split('/')[-2..-1].join('/')
164
+ source = source.gsub('\\', '/')
165
+
166
+ unless @full_proc_path
167
+ source = source.split('/')[-2..-1].join('/')
168
+ end
43
169
 
44
- return "<#{type} @ #{source} [Line #{line}]>"
170
+ return colorize("<#{type} @ #{source} [Line #{line}]>", :proc)
171
+
172
+ elsif x.class == Class
173
+ return colorize("<Class #{x}>", :classobject)
174
+
45
175
  # X is an Array, print list of all items.
46
176
  elsif x.class == Array
47
- result = "[\n"
177
+ result = "#{@braces ? '[' : ''}\n"
48
178
 
49
179
  x.each_with_index do |item, index|
50
180
  data = xpand(item, indent: _indent, tab: tab)
51
181
 
52
182
  result += "#{_indent}"
53
- result += "[#{index}] " if @show_indexes
183
+ result += "#{colorize("[#{index}]", :index)} " if @indexes
54
184
  result += "#{data}"
55
185
 
56
186
  unless index + 1 == x.length
57
- result += ", \n"
187
+ result += "#{@braces ? ', ' : ''} \n"
58
188
  end
59
189
  end
60
190
 
61
- result += "\n#{indent}]"
191
+ result += "\n#{indent}]" if @braces
62
192
  return result
193
+
63
194
  # X is a Hash, print all keys and values.
64
195
  elsif x.class == Hash
65
- result = "{\n"
196
+ result = "#{@braces ? '{' : ''}\n"
66
197
 
67
198
  longest_key = (
68
199
  x.keys.filter do |k, v|
@@ -75,6 +206,13 @@ module XPrint
75
206
  )
76
207
 
77
208
  longest_key = 0 if longest_key.nil?
209
+
210
+ # Color codes throw the text length, so we need to add the
211
+ # length of the color code and the code to reset the color
212
+ # that wrap around the colored word.
213
+ # The color code is like "\e[99m" and the reset "\e[0m",
214
+ # so the total length to add when using color is 9.
215
+ longest_key += 9 if @color
78
216
 
79
217
  x.each_with_index do |(key, value), index|
80
218
  data_key = "#{xpand(key, indent: _indent, tab: tab)}"
@@ -92,36 +230,64 @@ module XPrint
92
230
  result += "#{_indent}#{data_key} => #{data_value}"
93
231
 
94
232
  unless index + 1 == x.length
95
- result += ", \n"
233
+ result += "#{@braces ? ', ' : ''} \n"
96
234
  end
97
235
  end
98
236
 
99
- result += "\n#{indent}}"
237
+ result += "\n#{indent}}" if @braces
100
238
 
101
239
  return result
240
+
241
+ # X is a commonly used special kind of object.
242
+ elsif x.class == DateTime
243
+ datetime = x.strftime @datetime_format
244
+ return colorize("DateTime(#{datetime})", :datetime)
245
+
246
+ elsif x.class == Date
247
+ date = x.strftime @date_format
248
+ return colorize("Date(#{date})", :date)
249
+
250
+ elsif x.class == Time
251
+ time = x.strftime @time_format
252
+ return colorize("Time(#{time})", :time)
253
+
254
+ elsif x.class == BigDecimal
255
+ return colorize("BigDecimal(#{x.to_s('f')})", :bigdecimal)
256
+
257
+ elsif x.class == Rational
258
+ return colorize("Rational(#{x})", :rational)
259
+
102
260
  # X is a Structure; essentially a special case of X being an object.
103
261
  elsif x.is_a? Struct
104
- result = "Struct #{x.class}(\n"
262
+ struct_word = colorize('Struct', :struct)
263
+ classname = colorize(x.class, :struct)
264
+ result = "#{struct_word} #{classname}#{@braces ? '(' : ''}\n"
105
265
  longest_item = x.members.map { |m| m.to_s.length }.max()
106
266
 
107
267
  x.each_pair do |name, value|
108
- attr_name = name.to_s.ljust(longest_item)
268
+ attr_name = colorize(name.to_s.ljust(longest_item), :attribute)
109
269
  attr_data = xpand(value, indent: _indent, tab: tab)
110
270
 
111
271
  result += "#{_indent}#{attr_name} = #{attr_data}\n"
112
272
  end
113
273
 
114
- result += "#{indent})"
274
+ result += "#{indent})" if @braces
115
275
 
116
276
  return result
277
+
117
278
  # X is any arbitrary object; print all instance variables.
118
279
  else
119
- result = "#{x.class}(\n"
280
+ is_module = x.class == Module
281
+ classname = is_module ? "Module #{x}" : x.class
282
+ classname = colorize(classname, is_module ? :module : :classname)
283
+ result = "#{classname}#{@braces ? '(' : ''}"
120
284
  ivars = x.instance_variables
285
+ result += "\n" if ivars.length > 0
121
286
  longest_var = ivars.map { |v| v.to_s.length }.max()
122
287
 
123
288
  ivars.each_with_index do |var, index|
124
289
  attr_name = var.to_s.ljust(longest_var)
290
+ attr_name = colorize(attr_name, :attribute)
125
291
  attr_data = xpand(
126
292
  x.instance_variable_get(var),
127
293
  indent: _indent,
@@ -131,7 +297,7 @@ module XPrint
131
297
  result += "#{_indent}#{attr_name} = #{attr_data}\n"
132
298
  end
133
299
 
134
- result += "#{indent})"
300
+ result += "#{ivars.length > 0 ? indent: ''})" if @braces
135
301
 
136
302
  return result
137
303
  end
@@ -144,4 +310,4 @@ end
144
310
 
145
311
  def xpand(item, tab: "\t")
146
312
  XPrint::xpand(item, tab: tab)
147
- end
313
+ 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.1
4
+ version: 0.8.1
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.