xprint 0.5.0

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 +7 -0
  2. data/lib/version.rb +3 -0
  3. data/lib/xprint.rb +146 -0
  4. metadata +46 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a2cc60a347d36661e3bb0fcc6d1ae58ab46f236eeefd8ba40fc869a1cd20439a
4
+ data.tar.gz: c0c48517b86a17e3a4219bc9734cd7cf19548883c05b4ba15f3fee783d2ed5d9
5
+ SHA512:
6
+ metadata.gz: 9f46bcb7bc0b0536bb83803b623c625dc91c17151ea4bf47173bf50d5def8f1c45408fef73c117742757f4b7f9db6724b0c8255fe1db7f269c2003b022ebe4be
7
+ data.tar.gz: 611aceb397a9dcc19788be147711cc253e7b348d39c7dd15880c3476718d9d9c0254f11c47e690ed077aede60a2bc20a5148b52ba61ca62045204672fb670578
@@ -0,0 +1,3 @@
1
+ module XPrint
2
+ VERSION = '0.5.0'
3
+ end
@@ -0,0 +1,146 @@
1
+ module XPrint
2
+ @data_classes = [
3
+ String, Integer, Float, TrueClass, FalseClass, NilClass,
4
+ Symbol
5
+ ]
6
+ @hash_name_classes = @data_classes + [Proc]
7
+ @tab = "\t"
8
+ @show_indexes = true
9
+
10
+ def self.set(**kwargs)
11
+ @tab = kwargs[:tab] unless kwargs[:tab].nil?
12
+ @show_indexes = kwargs[:show_indexes] unless kwargs[:show_indexes].nil?
13
+ end
14
+
15
+ def self.tab()
16
+ @tab
17
+ end
18
+
19
+ def self.show_indexes()
20
+ @show_indexes
21
+ end
22
+
23
+ def self.xp(*args)
24
+ args.each do |arg|
25
+ puts self.xpand(arg, tab: @tab)
26
+ end
27
+ end
28
+
29
+ def self.xpand(x, indent: '', tab: "\t")
30
+
31
+ _indent = "#{tab}#{indent}"
32
+
33
+ # X is a "primitive" kind of data that has no subitems, so
34
+ # we can just print it.
35
+ if @data_classes.include? x.class
36
+ return x.inspect
37
+ # X is a Proc, print more compact version of standard Proc.inspect
38
+ # text.
39
+ elsif x.class == Proc
40
+ source, line = x.source_location
41
+ source = source.gsub('\\', '/').split('/')[-2..-1].join('/')
42
+
43
+ return "<Proc @ #{source} [Line #{line}]>"
44
+ # X is an Array, print list of all items.
45
+ elsif x.class == Array
46
+ result = "[\n"
47
+
48
+ x.each_with_index do |item, index|
49
+ data = xpand(item, indent: _indent, tab: tab)
50
+
51
+ result += "#{_indent}"
52
+ result += "[#{index}] " if @show_indexes
53
+ result += "#{data}"
54
+
55
+ unless index + 1 == x.length
56
+ result += ", \n"
57
+ end
58
+ end
59
+
60
+ result += "\n#{indent}]"
61
+ return result
62
+ # X is a Hash, print all keys and values.
63
+ elsif x.class == Hash
64
+ result = "{\n"
65
+
66
+ longest_key = (
67
+ x.keys.filter do |k, v|
68
+ @hash_name_classes.include? k.class
69
+ end.
70
+ map do |k, v|
71
+ k.to_s.length
72
+ end.
73
+ max()
74
+ )
75
+
76
+ longest_key = 0 if longest_key.nil?
77
+
78
+ x.each_with_index do |(key, value), index|
79
+ data_key = "#{xpand(key, indent: _indent, tab: tab)}"
80
+
81
+ data_key = (
82
+ if @hash_name_classes.include? key.class
83
+ data_key.ljust(longest_key + 1)
84
+ else
85
+ data_key.ljust(data_key.length + longest_key)
86
+ end
87
+ )
88
+
89
+ data_value = xpand(value, indent: _indent, tab: tab)
90
+
91
+ result += "#{_indent}#{data_key} => #{data_value}"
92
+
93
+ unless index + 1 == x.length
94
+ result += ", \n"
95
+ end
96
+ end
97
+
98
+ result += "\n#{indent}}"
99
+
100
+ return result
101
+ # X is a Structure; essentially a special case of X being an object.
102
+ elsif x.is_a? Struct
103
+ result = "Struct #{x.class}(\n"
104
+ longest_item = x.members.map { |m| m.to_s.length }.max()
105
+
106
+ x.each_pair do |name, value|
107
+ attr_name = name.to_s.ljust(longest_item)
108
+ attr_data = xpand(value, indent: _indent, tab: tab)
109
+
110
+ result += "#{_indent}#{attr_name} = #{attr_data}\n"
111
+ end
112
+
113
+ result += "#{indent})"
114
+
115
+ return result
116
+ # X is any arbitrary object; print all instance variables.
117
+ else
118
+ result = "#{x.class}(\n"
119
+ ivars = x.instance_variables
120
+ longest_var = ivars.map { |v| v.to_s.length }.max()
121
+
122
+ ivars.each_with_index do |var, index|
123
+ attr_name = var.to_s.ljust(longest_var)
124
+ attr_data = xpand(
125
+ x.instance_variable_get(var),
126
+ indent: _indent,
127
+ tab: tab
128
+ )
129
+
130
+ result += "#{_indent}#{attr_name} = #{attr_data}\n"
131
+ end
132
+
133
+ result += "#{indent})"
134
+
135
+ return result
136
+ end
137
+ end
138
+ end
139
+
140
+ def xp(*args)
141
+ XPrint::xp(*args)
142
+ end
143
+
144
+ def xpand(item, tab: "\t")
145
+ XPrint::xpand(item, tab: tab)
146
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: xprint
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ platform: ruby
6
+ authors:
7
+ - JCabr
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-07-20 00:00:00.000000000 Z
12
+ dependencies: []
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.
15
+ email:
16
+ - jcabr.dev@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/version.rb
22
+ - lib/xprint.rb
23
+ homepage: https://github.com/JCabr/xprint.rb
24
+ licenses:
25
+ - MIT
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubygems_version: 3.1.2
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: Gem for pretty printing any kind of object.
46
+ test_files: []