nub 0.0.32 → 0.0.33

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/nub/log.rb +11 -64
  3. data/lib/nub/sys.rb +53 -1
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 42e447303c2f49120a5a2f17132d16d916b9cdd82dd88dc915eea10b236b69bd
4
- data.tar.gz: 56492c14f5aa0b8c319239578516c631f3d5ecea89cf678bf34206681965c95a
3
+ metadata.gz: 1da6279157321d7fc37b92e29dbb5b6aadc736f773b1bb70ffa3b93a2c31edda
4
+ data.tar.gz: 8ba135845e4a368c3bbf738f01684d3ff91079d840f1d9f19f74265d88b30a42
5
5
  SHA512:
6
- metadata.gz: 4bc7fc5addcbcd6b289e79e0f6120aacf1221df145490fb42df42a7aac05999e0bfc4555356aa8ee9eae69c9cadf86eb95824ad43b2c8c6d5cab957f50f2502d
7
- data.tar.gz: 5972d809cda64253357facd5a14757598c27eb25618801130d2bdabfe6b9d0ef99436fb92306c800b883541a02272b93d73c7ca67d894235fc7553f115ce569a
6
+ metadata.gz: 7dbaab1dd5eab84e2f68b74f9174c7e5d94c0d451af66a8299bb5d19312727f65d328537f9fe62dbba543e80184581107fe92e202ac31315bee0a296c0a64c8f
7
+ data.tar.gz: 4ff7dde360e8f1af8892d8853bd26f1808fc540d57134aa68b6547807091eda2016c500ca116dfd30968afedc9dcec73dfcea912ec166c6ce7bb9b39bcc529f5
data/lib/nub/log.rb CHANGED
@@ -25,19 +25,6 @@ require 'ostruct'
25
25
  require 'colorize'
26
26
  require_relative 'sys'
27
27
 
28
- ColorPair = Struct.new(:str, :color)
29
- ColorMap = {
30
- "30" => "black",
31
- "31" => "red",
32
- "32" => "green",
33
- "33" => "yellow",
34
- "34" => "blue",
35
- "35" => "magenta",
36
- "36" => "cyan",
37
- "37" => "white",
38
- "39" => "gray88" # default
39
- }
40
-
41
28
  LogLevel = OpenStruct.new({
42
29
  error: 0,
43
30
  warn: 1,
@@ -79,30 +66,33 @@ module Log
79
66
  @file = File.open(@path, 'a')
80
67
  @file.sync = true
81
68
  end
82
-
83
- return nil
84
69
  end
85
70
 
86
71
  # Format the given string for use in log
87
72
  def format(str)
88
73
  @@_monitor.synchronize{
89
74
 
90
- # Locate caller
91
- stack = caller_locations(3,10)
75
+ # Skip first 3 on stack (i.e. 0 = block in format, 1 = synchronize, 2 = format)
76
+ stack = caller_locations(3, 20)
77
+
78
+ # Skip past any calls in 'log.rb' or 'monitor.rb'
92
79
  i = -1
93
80
  while i += 1 do
94
81
  mod = File.basename(stack[i].path, '.rb')
95
82
  break if !['log', 'monitor'].include?(mod)
96
83
  end
97
84
 
98
- # Save lineno from non log call but use method name rather than rescue or block
85
+ # Save lineno from original location
99
86
  lineno = stack[i].lineno
87
+
88
+ # Skip over block type functions to use method
100
89
  nested = ['rescue in', 'block in', 'each']
101
90
  while nested.any?{|x| stack[i].label.include?(x)} do
102
91
  i += 1
103
92
  end
104
- loc = ":#{File.basename(stack[i].path, '.rb')}:#{stack[i].label}:#{lineno}"
105
93
 
94
+ # Construct stamp
95
+ loc = ":#{File.basename(stack[i].path, '.rb')}:#{stack[i].label}:#{lineno}"
106
96
  return "#{Time.now.utc.iso8601(3)}#{loc}:: #{str}"
107
97
  }
108
98
  end
@@ -155,8 +145,7 @@ module Log
155
145
  end
156
146
 
157
147
  def error(*args)
158
- return puts(*args) if LogLevel.error <= @@_level
159
- return true
148
+ return puts(*args)
160
149
  end
161
150
 
162
151
  def warn(*args)
@@ -181,7 +170,7 @@ module Log
181
170
  end
182
171
 
183
172
  # Remove an item from the queue, block until one exists
184
- def pop()
173
+ def pop
185
174
  return @@_queue ? @@_queue.pop : nil
186
175
  end
187
176
 
@@ -189,48 +178,6 @@ module Log
189
178
  def empty?
190
179
  return @@_queue ? @@_queue.empty? : true
191
180
  end
192
-
193
- # Tokenize the given colorized string
194
- # @param str [String] string with ansi color codes
195
- # @returns [Array] array of Token
196
- def tokenize_colorize(str)
197
- @@_monitor.synchronize{
198
- tokens = []
199
- matches = str.to_enum(:scan, /\e\[0;[39]\d;49m(.*?[\s]*)\e\[0m/).map{Regexp.last_match}
200
-
201
- i, istart, iend = 0, 0, 0
202
- match = matches[i]
203
- while istart < str.size
204
- color = "39"
205
- iend = str.size
206
- token = str[istart..iend]
207
-
208
- # Current token is not a match
209
- if match && match.begin(0) != istart
210
- iend = match.begin(0)-1
211
- token = str[istart..iend]
212
- istart = iend + 1
213
-
214
- # Current token is a match
215
- elsif match && match.begin(0) == istart
216
- iend = match.end(0)
217
- token = match.captures.first
218
- color = match.to_s[/\e\[0;(\d+);49m.*/, 1]
219
- i += 1; match = matches[i]
220
- istart = iend
221
-
222
- # Ending
223
- else
224
- istart = iend
225
- end
226
-
227
- # Create token and advance
228
- tokens << ColorPair.new(token, color)
229
- end
230
-
231
- return tokens
232
- }
233
- end
234
181
  end
235
182
 
236
183
  # vim: ft=ruby:ts=2:sw=2:sts=2
data/lib/nub/sys.rb CHANGED
@@ -23,6 +23,19 @@ require 'io/console'
23
23
  require 'ostruct'
24
24
  require 'stringio'
25
25
 
26
+ ColorPair = Struct.new(:str, :color_code, :color_name)
27
+ ColorMap = {
28
+ 30 => "black",
29
+ 31 => "red",
30
+ 32 => "green",
31
+ 33 => "yellow",
32
+ 34 => "blue",
33
+ 35 => "magenta",
34
+ 36 => "cyan",
35
+ 37 => "white",
36
+ 39 => "gray88" # default
37
+ }
38
+
26
39
  module Sys
27
40
 
28
41
  # Capture STDOUT to a string
@@ -47,7 +60,6 @@ module Sys
47
60
  ensure
48
61
  `stty #{state}`
49
62
  end
50
-
51
63
  end
52
64
 
53
65
  # Strip the ansi color codes from the given string
@@ -57,6 +69,46 @@ module Sys
57
69
  return str.gsub(/\e\[0;[39]\d;49m/, '').gsub(/\e\[0m/, '')
58
70
  end
59
71
 
72
+ # Tokenize the given colorized string
73
+ # @param str [String] string with ansi color codes
74
+ # @returns [Array] array of Token
75
+ def self.tokenize_colorize(str)
76
+ tokens = []
77
+ matches = str.to_enum(:scan, /\e\[0;[39]\d;49m(.*?[\s]*)\e\[0m/).map{Regexp.last_match}
78
+
79
+ i, istart, iend = 0, 0, 0
80
+ match = matches[i]
81
+ while istart < str.size
82
+ color = "39"
83
+ iend = str.size
84
+ token = str[istart..iend]
85
+
86
+ # Current token is not a match
87
+ if match && match.begin(0) != istart
88
+ iend = match.begin(0)-1
89
+ token = str[istart..iend]
90
+ istart = iend + 1
91
+
92
+ # Current token is a match
93
+ elsif match && match.begin(0) == istart
94
+ iend = match.end(0)
95
+ token = match.captures.first
96
+ color = match.to_s[/\e\[0;(\d+);49m.*/, 1]
97
+ i += 1; match = matches[i]
98
+ istart = iend
99
+
100
+ # Ending
101
+ else
102
+ istart = iend
103
+ end
104
+
105
+ # Create token and advance
106
+ tokens << ColorPair.new(token, color.to_i, ColorMap[color.to_i])
107
+ end
108
+
109
+ return tokens
110
+ end
111
+
60
112
  end
61
113
 
62
114
  # vim: ft=ruby:ts=2:sw=2:sts=2
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nub
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.32
4
+ version: 0.0.33
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrick Crummett
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-29 00:00:00.000000000 Z
11
+ date: 2018-03-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize