nub 0.0.42 → 0.0.43

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 289eea77b83026e03f76830c0ef68782f44fdb34237b4077e4a747e44a18e11b
4
- data.tar.gz: f3605f50a0555a2cd8f5f452739a863fd421c07ecffa8bcfedd8548975eaaad6
3
+ metadata.gz: 18e9121a0f51826a76f7c76172ad12a543fdc1ed177e2faafd226c4748ee1c55
4
+ data.tar.gz: b2d67cc335778e825742b6d9778026e47c07a166243b0a0909b6c5318f740812
5
5
  SHA512:
6
- metadata.gz: 0d003170c0aa05f00626d9587b666c0b66923ff4ad20401db1d39d18f7dc0f143bfbd438433f71890485e8b44ccabe3bb497cfcdd74d383fa6cb6853cc842b87
7
- data.tar.gz: 41a27d08399b97a8af5da53acfd63f1b62f7137d143c46cdd783a2947126dbc37d48060ff01b5a4aade8b97c82b3d72b9534b58bd68123fff4134f60ed6cb6e3
6
+ metadata.gz: 1cc75cd89bf288b25b600f4a1f922dd4e8af61cc12c77c61ae798bfb0d20ca2dccc6712cb7ba6d3ca70cea4fd8685519e254f539ef177d893c12a1c3350ae6e0
7
+ data.tar.gz: b910fb8b96b7475db9d7d1b691eb118ee27220f78a0647cecea89c221ba3b29c92630f3058cf435abc70a586e6fc472aef73866890f643af9598533fda6f39b1
data/lib/nub/commander.rb CHANGED
@@ -21,6 +21,7 @@
21
21
 
22
22
  require 'colorize'
23
23
  require_relative 'sys'
24
+ require_relative 'string'
24
25
 
25
26
  # Command option encapsulation
26
27
  class Option
@@ -160,7 +161,7 @@ class Commander
160
161
  def help
161
162
  help = @app.nil? ? "" : "#{banner}\n"
162
163
  if !@examples.nil? && !@examples.empty?
163
- newline = Sys.strip_colorize(@examples)[-1] != "\n" ? "\n" : ""
164
+ newline = @examples.strip_color[-1] != "\n" ? "\n" : ""
164
165
  help += "Examples:\n#{@examples}\n#{newline}"
165
166
  end
166
167
  app = @app || @app_default
data/lib/nub/config.rb CHANGED
@@ -28,8 +28,7 @@ require_relative 'log'
28
28
  # Simple YAML configuration for an application.
29
29
  # Uses singleton pattern for single source of truth
30
30
  module Config
31
-
32
- # Private properties
31
+ extend self
33
32
  @@_yml = nil
34
33
 
35
34
  # Public properties
@@ -39,7 +38,7 @@ module Config
39
38
 
40
39
  # Singleton new alternate
41
40
  # @param config [String] name or path of the config file
42
- def self.init(config)
41
+ def init(config)
43
42
 
44
43
  # Determine caller's file path to look for sidecar config
45
44
  caller_path = caller_locations(1, 1).first.path
@@ -52,29 +51,29 @@ module Config
52
51
  begin
53
52
  @@_yml = File.exists?(@path) ? YAML.load_file(@path) : {}
54
53
  rescue Exception => e
55
- Log.puts("Error: #{e}".colorize(:red)) and exit
54
+ Log.die(e)
56
55
  end
57
56
 
58
57
  return nil
59
58
  end
60
59
 
61
60
  # Simple bool whether the config exists or not on disk
62
- def self.exists?
61
+ def exists?
63
62
  return File.exists?(@path)
64
63
  end
65
64
 
66
65
  # Hash like getter
67
- def self.[](key)
66
+ def [](key)
68
67
  return @@_yml[key]
69
68
  end
70
69
 
71
70
  # Hash like setter
72
- def self.[]=(key, val)
71
+ def []=(key, val)
73
72
  return @@_yml[key] = val
74
73
  end
75
74
 
76
75
  # Save the config file
77
- def self.save
76
+ def save
78
77
  File.write(@path, @@_yml.to_yaml) if @@_yml
79
78
  end
80
79
  end
data/lib/nub/log.rb CHANGED
@@ -24,6 +24,7 @@ require 'monitor'
24
24
  require 'ostruct'
25
25
  require 'colorize'
26
26
  require_relative 'sys'
27
+ require_relative 'string'
27
28
 
28
29
  LogLevel = OpenStruct.new({
29
30
  error: 0,
@@ -73,7 +74,7 @@ module Log
73
74
  @@_monitor.synchronize{
74
75
 
75
76
  # Skip first 3 on stack (i.e. 0 = block in call_details, 1 = synchronize, 2 = call_detail)
76
- stack = caller_locations(3, 10)
77
+ stack = caller_locations(3, 20)
77
78
 
78
79
  # Skip past any calls in 'log.rb' or 'monitor.rb'
79
80
  i = -1
@@ -114,7 +115,7 @@ module Log
114
115
  type = (opts && opts.key?(:type)) ? opts[:type] : ""
115
116
  stamp = (opts && opts.key?(:stamp)) ? opts[:stamp] : true
116
117
  if stamp or loc
117
- timestamp, location = call_details
118
+ timestamp, location = self.call_details
118
119
  location = loc ? location : ""
119
120
  type = ":#{type}" if !type.empty?
120
121
  str = "#{timestamp}#{location}#{type}:: #{str}"
@@ -122,7 +123,7 @@ module Log
122
123
 
123
124
  # Handle output
124
125
  if !str.empty?
125
- @file << Sys.strip_colorize(str) if @path
126
+ @file << str.strip_color if @path
126
127
  @@_queue << str if @@_queue
127
128
  $stdout.print(str) if @@_stdout
128
129
  end
@@ -145,14 +146,14 @@ module Log
145
146
  str = str.colorize(:light_yellow) if type == 'W'
146
147
 
147
148
  if stamp or loc
148
- timestamp, location = call_details
149
+ timestamp, location = self.call_details
149
150
  location = loc ? location : ""
150
151
  type = ":#{type}" if !type.empty?
151
152
  str = "#{timestamp}#{location}#{type}:: #{str}"
152
153
  end
153
154
 
154
155
  # Handle output
155
- @file.puts(Sys.strip_colorize(str)) if @path
156
+ @file.puts(str.strip_color) if @path
156
157
  @@_queue << "#{str}\n" if @@_queue
157
158
  $stdout.puts(str) if @@_stdout
158
159
 
@@ -165,7 +166,7 @@ module Log
165
166
  opts[:loc] = true and opts[:type] = 'E' if opts
166
167
  args << {:loc => true, :type => 'E'} if !opts
167
168
 
168
- return puts(*args)
169
+ return self.puts(*args)
169
170
  end
170
171
 
171
172
  def warn(*args)
@@ -173,7 +174,7 @@ module Log
173
174
  opts = args.find{|x| x.is_a?(Hash)}
174
175
  opts[:type] = 'W' if opts
175
176
  args << {:type => 'W'} if !opts
176
- return puts(*args)
177
+ return self.puts(*args)
177
178
  end
178
179
  return true
179
180
  end
@@ -183,7 +184,7 @@ module Log
183
184
  opts = args.find{|x| x.is_a?(Hash)}
184
185
  opts[:type] = 'I' if opts
185
186
  args << {:type => 'I'} if !opts
186
- return puts(*args)
187
+ return self.puts(*args)
187
188
  end
188
189
  return true
189
190
  end
@@ -193,7 +194,7 @@ module Log
193
194
  opts = args.find{|x| x.is_a?(Hash)}
194
195
  opts[:type] = 'D' if opts
195
196
  args << {:type => 'D'} if !opts
196
- return puts(*args)
197
+ return self.puts(*args)
197
198
  end
198
199
  return true
199
200
  end
@@ -201,7 +202,7 @@ module Log
201
202
  # Log the given message in red and exit
202
203
  # @param msg [String] message to log
203
204
  def die(msg)
204
- puts("Error: #{msg}".colorize(:red), stamp: false) and exit
205
+ self.puts("Error: #{msg}".colorize(:red), stamp: false) and exit
205
206
  end
206
207
 
207
208
  # Remove an item from the queue, block until one exists
data/lib/nub/string.rb CHANGED
@@ -19,6 +19,19 @@
19
19
  #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
20
  #SOFTWARE.
21
21
 
22
+ ColorPair = Struct.new(:str, :color_code, :color_name)
23
+ ColorMap = {
24
+ 30 => "black",
25
+ 31 => "red",
26
+ 32 => "green",
27
+ 33 => "yellow",
28
+ 34 => "blue",
29
+ 35 => "magenta",
30
+ 36 => "cyan",
31
+ 37 => "white",
32
+ 39 => "gray88" # default
33
+ }
34
+
22
35
  # Monkey patch string with some useful methods
23
36
  class String
24
37
 
@@ -32,6 +45,53 @@ class String
32
45
  }
33
46
  return self.encode(Encoding.find('ASCII'), options)
34
47
  end
48
+
49
+ # Strip the ansi color codes from the given string
50
+ # @returns [String] string without any ansi codes
51
+ def strip_color
52
+ return self.gsub(/\e\[0;[39]\d;49m/, '').gsub(/\e\[0m/, '')
53
+ end
54
+
55
+ # Tokenize the given colorized string
56
+ # @returns [Array] array of Token
57
+ def tokenize_color
58
+ tokens = []
59
+ matches = self.to_enum(:scan, /\e\[0;[39]\d;49m(.*?[\s]*)\e\[0m/).map{Regexp.last_match}
60
+
61
+ i, istart, iend = 0, 0, 0
62
+ match = matches[i]
63
+ while istart < self.size
64
+ color = "39"
65
+ iend = self.size
66
+ token = self[istart..iend]
67
+
68
+ # Current token is not a match
69
+ if match && match.begin(0) != istart
70
+ iend = match.begin(0)-1
71
+ token = self[istart..iend]
72
+ istart = iend + 1
73
+
74
+ # Current token is a match
75
+ elsif match && match.begin(0) == istart
76
+ iend = match.end(0)
77
+ token = match.captures.first
78
+ color = match.to_s[/\e\[0;(\d+);49m.*/, 1]
79
+ i += 1; match = matches[i]
80
+ istart = iend
81
+
82
+ # Ending
83
+ else
84
+ istart = iend
85
+ end
86
+
87
+ # Create token and advance
88
+ tokens << ColorPair.new(token, color.to_i, ColorMap[color.to_i])
89
+ end
90
+
91
+ return tokens
92
+ end
93
+
94
+
35
95
  end
36
96
 
37
97
  # vim: ft=ruby:ts=2:sw=2:sts=2
data/lib/nub/sys.rb CHANGED
@@ -22,24 +22,22 @@
22
22
  require 'io/console'
23
23
  require 'ostruct'
24
24
  require 'stringio'
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
- }
25
+ require_relative 'log'
38
26
 
39
27
  module Sys
28
+ extend self
29
+
30
+ # Get the given environment variable by nam
31
+ # @param var [String] name of the environment var
32
+ # @param required [Bool] require that the variable exists by default
33
+ def env(var, required:true)
34
+ value = ENV[var]
35
+ Log.die("#{var} env variable is required!") if required && !value
36
+ return value
37
+ end
40
38
 
41
39
  # Wait for any key to be pressed
42
- def self.any_key?
40
+ def any_key?
43
41
  begin
44
42
  state = `stty -g`
45
43
  `stty raw -echo -icanon isig`
@@ -52,14 +50,14 @@ module Sys
52
50
  # Get the caller's filename for the caller of the function this call is nested in
53
51
  # not the function this call is called in
54
52
  # @returns [String] the caller's filename
55
- def self.caller_filename
53
+ def caller_filename
56
54
  path = caller_locations(2, 1).first.path
57
55
  return File.basename(path)
58
56
  end
59
57
 
60
58
  # Capture STDOUT to a string
61
59
  # @returns [String] the redirected output
62
- def self.capture(&block)
60
+ def capture(&block)
63
61
  stdout, stderr = StringIO.new, StringIO.new
64
62
  $stdout, $stderr = stdout, stderr
65
63
 
@@ -69,54 +67,6 @@ module Sys
69
67
 
70
68
  return OpenStruct.new(result: result, stdout: stdout.string, stderr: stderr.string)
71
69
  end
72
-
73
- # Strip the ansi color codes from the given string
74
- # @param str [String] string with ansi color codes
75
- # @returns [String] string without any ansi codes
76
- def self.strip_colorize(str)
77
- return str.gsub(/\e\[0;[39]\d;49m/, '').gsub(/\e\[0m/, '')
78
- end
79
-
80
- # Tokenize the given colorized string
81
- # @param str [String] string with ansi color codes
82
- # @returns [Array] array of Token
83
- def self.tokenize_colorize(str)
84
- tokens = []
85
- matches = str.to_enum(:scan, /\e\[0;[39]\d;49m(.*?[\s]*)\e\[0m/).map{Regexp.last_match}
86
-
87
- i, istart, iend = 0, 0, 0
88
- match = matches[i]
89
- while istart < str.size
90
- color = "39"
91
- iend = str.size
92
- token = str[istart..iend]
93
-
94
- # Current token is not a match
95
- if match && match.begin(0) != istart
96
- iend = match.begin(0)-1
97
- token = str[istart..iend]
98
- istart = iend + 1
99
-
100
- # Current token is a match
101
- elsif match && match.begin(0) == istart
102
- iend = match.end(0)
103
- token = match.captures.first
104
- color = match.to_s[/\e\[0;(\d+);49m.*/, 1]
105
- i += 1; match = matches[i]
106
- istart = iend
107
-
108
- # Ending
109
- else
110
- istart = iend
111
- end
112
-
113
- # Create token and advance
114
- tokens << ColorPair.new(token, color.to_i, ColorMap[color.to_i])
115
- end
116
-
117
- return tokens
118
- end
119
-
120
70
  end
121
71
 
122
72
  # vim: ft=ruby:ts=2:sw=2:sts=2
data/lib/nub/user.rb CHANGED
@@ -24,14 +24,15 @@ require 'etc'
24
24
 
25
25
  # Some user related helper methods
26
26
  module User
27
+ extend self
27
28
 
28
29
  # Check if the current user has root privileges
29
- def self.root?
30
+ def root?
30
31
  return Process.uid.zero?
31
32
  end
32
33
 
33
34
  # Get the current user taking into account sudo priviledges
34
- def self.name
35
+ def name
35
36
  return Process.uid.zero? ? Etc.getpwuid(ENV['SUDO_UID'].to_i).name : ENV['USER']
36
37
  end
37
38
  end
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.42
4
+ version: 0.0.43
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-04-03 00:00:00.000000000 Z
11
+ date: 2018-04-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize