xprint 0.8.1 → 0.9.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 +55 -5
  4. metadata +3 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d8e6e0778467e8ed1efb112fc5a6fb7ee2e5463e1338dbffaf73aeeb61a75d0f
4
- data.tar.gz: 43fef229c2551c8791d0b009cac768524fdb929a014701f16ed360981a014a7d
3
+ metadata.gz: 72c5d81ad083539ab972c38b51405d5275034ce535f4014696257dbec0d6e5ab
4
+ data.tar.gz: 950f5d948e5098902d02d25eea8a62c1ec146a0dd58bb052bed139df50d06512
5
5
  SHA512:
6
- metadata.gz: 301cc656d0b4d0e62c6cf0c13486866b4249e7668700f11bcd9e58d2c4a28cac9a033b9ed3de430f64afde2ccb46f008a46e232f4ef5ca943e5371d9b17ecde9
7
- data.tar.gz: 3ef89b719424f34e220ffc9aa77d4591dddb19fc695a64c257144fe3d1bf068b3569c8896b70ff85048a6fad0a4e4bcb6f722cd0af4c541259091228ff229c19
6
+ metadata.gz: 33fb5f501119a0cb8015797c93f72516276eb332a03b9dba5f01696965a2ab2b8d06659724c8e12dcb3c9dd287df0311a6a11b48e9bef51fdf5bc329e9d114b5
7
+ data.tar.gz: 11492f4674dcd327edf54cfe909c1c830236ce798f34c830a8ac5f74b36e860ea3548d44af465a72a5ca73af90c8ff5ce3d92f8f8fc429baff92e8d0c2bafaa4
@@ -1,3 +1,3 @@
1
1
  module XPrint
2
- VERSION = '0.8.1'
2
+ VERSION = '0.9.1'
3
3
  end
@@ -1,5 +1,6 @@
1
1
  require 'bigdecimal'
2
2
  require 'date'
3
+ require 'yaml'
3
4
 
4
5
  module XPrint
5
6
  @data_classes = [
@@ -7,14 +8,14 @@ module XPrint
7
8
  Symbol, Date, Time, DateTime, BigDecimal, Rational
8
9
  ]
9
10
  @hash_name_classes = @data_classes + [Proc]
10
- @tab = "\t"
11
+ @tab = " "
11
12
  @indexes = true
12
13
  @full_proc_path = false
13
14
  @braces = true
14
15
  @date_format = '%F'
15
16
  @time_format = '%c'
16
17
  @datetime_format = '%FT%T%:z'
17
- @color = false
18
+ @color = true
18
19
  @colors = {
19
20
  attribute: :blue,
20
21
  bigdecimal: :darkcyan,
@@ -76,6 +77,48 @@ module XPrint
76
77
  return
77
78
  end
78
79
 
80
+ def self.load_file(config)
81
+ config_data = YAML.load( File.read config )
82
+ config_data = self.symbolize_keys(config_data)
83
+
84
+ if config_data.key? :general
85
+ self.set **config_data[:general]
86
+ end
87
+
88
+ if config_data.key? :colors
89
+ color_data = config_data[:colors]
90
+
91
+ color_data.each do |name, color|
92
+ color_data[name] = color.to_sym
93
+ end
94
+
95
+ self.set_color_for **config_data[:colors]
96
+ end
97
+
98
+ if config_data.key? :'color codes'
99
+ self.set_color_code_for **config_data[:'color codes']
100
+ end
101
+ end
102
+
103
+ def self.load(config)
104
+ calling_file = caller_locations.first.absolute_path
105
+ base_dir = File.dirname calling_file
106
+ relative_config = File.expand_path config, base_dir
107
+
108
+ self.load_file relative_config
109
+ end
110
+
111
+ private_class_method def self.symbolize_keys(hash)
112
+ hash.inject({}) do |result, (key, value)|
113
+ new_key = key.to_sym
114
+ new_value = value.is_a?(Hash) ? symbolize_keys(value) : value
115
+
116
+ result[new_key] = new_value
117
+
118
+ result
119
+ end
120
+ end
121
+
79
122
  def self.set_color_for(**kwargs)
80
123
  kwargs.each do |keyword, arg|
81
124
  @colors[keyword] = arg
@@ -135,7 +178,7 @@ module XPrint
135
178
  return result
136
179
  end
137
180
 
138
- def self.xpand(x, indent: '', tab: "\t")
181
+ def self.xpand(x, indent: '', tab: ' ')
139
182
 
140
183
  _indent = "#{tab}#{indent}"
141
184
 
@@ -180,7 +223,14 @@ module XPrint
180
223
  data = xpand(item, indent: _indent, tab: tab)
181
224
 
182
225
  result += "#{_indent}"
183
- result += "#{colorize("[#{index}]", :index)} " if @indexes
226
+ if @indexes
227
+ adjustment = x.length.to_s.length + 3
228
+ # Account for characters used for color coding.
229
+ adjustment += 9 if @color
230
+
231
+ result += "#{colorize("[#{index}]", :index)} ".
232
+ ljust(adjustment)
233
+ end
184
234
  result += "#{data}"
185
235
 
186
236
  unless index + 1 == x.length
@@ -310,4 +360,4 @@ end
310
360
 
311
361
  def xpand(item, tab: "\t")
312
362
  XPrint::xpand(item, tab: tab)
313
- end
363
+ end
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.8.1
4
+ version: 0.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - JCabr
@@ -11,7 +11,8 @@ cert_chain: []
11
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
- 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: []