verboss 0.0.1

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 (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/verboss.rb +248 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6ce928218095b131b217870720d3dc17f744ed87
4
+ data.tar.gz: 3c949945475b723daed38ebb6fd932d65b26b930
5
+ SHA512:
6
+ metadata.gz: 5d28b4888318679dd8b1f1ac3c1d5fe4d6363e26b70ef32c25d0cab67d80afb8e06d7aed3dbc2ec173a98664ff1eae30bcef72f7e361110bf476dca40707ed16
7
+ data.tar.gz: 5caf431481a9d5bdf09e6b7ba2006ce0b80edd9af97c6653bebbe9175601cfe686733ee6dc45abe41e915b1c55c1e73cc3f32e30ae402eca089d81fd1c0dc713
data/lib/verboss.rb ADDED
@@ -0,0 +1,248 @@
1
+ ##### Verbose Module####
2
+ # allows concise method calls to organize and standardize terminal output
3
+ # Verbose.doing organizes all output that occurs during its block
4
+ # Verbose.quiet allows for all output to be silenced, displaying only the passed argument (if given)
5
+ #
6
+ # The last few methods: say, mention, yell, scream
7
+ # These are intended to standardize the format of certain kinds of output by
8
+ # level of importance
9
+ # say -> general information -> normal font
10
+ # mention -> important information -> blue font
11
+ # cheer -> success was reached -> green font
12
+ # yell -> problem was discovered -> red font
13
+ # scream -> even the heavens must know -> disgusting font
14
+ #
15
+ module Verboss
16
+ @@err_indent = "$ ".color(:magenta)
17
+ @@out_indent = "| ".color(:magenta)
18
+ @@root_stderr = $stderr
19
+ @@root_stdout = $stdout
20
+
21
+ @@temporary_options = {
22
+ quiet: false,
23
+ no_logging: false
24
+ }
25
+
26
+
27
+ @@spinner = {
28
+ on: false,
29
+ chars: %w[| / - \\],
30
+ index: 0,
31
+ delay: 0.5,
32
+ debug_level: 0,
33
+ thread: Thread.new {
34
+ while @@spinner[:thread]
35
+ Thread.stop unless @@spinner[:on]
36
+ sleep @@spinner[:delay]
37
+ c = (@@spinner[:chars][ @@spinner[:index] = (@@spinner[:index] + 1) % @@spinner[:chars].length ] + "\b").color(:white).bright.background(:black)
38
+ @@root_stdout.print c
39
+ end
40
+ }
41
+ }
42
+
43
+ ## FLAG METHODS
44
+ def self.migrations? # helper used to determine if ActiveRecord::Migration.verbose should be true
45
+ ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : false
46
+ end
47
+
48
+ def self.trace? # helper used to determine if stack traces should be displayed or not
49
+ ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : false
50
+ end
51
+
52
+ def self.level arg = 1
53
+ case ENV["VERBOSE"]
54
+ when Numeric then arg <= ENV["VERBOSE"]
55
+ when nil then false
56
+ else
57
+ true
58
+ end
59
+ end
60
+
61
+ ## WAIT SPINNER ##
62
+ def self.start_spinner
63
+ @@spinner[:on] = true
64
+ @@spinner[:thread].wakeup if @@spinner[:thread].status == "sleep"
65
+ end
66
+
67
+ def self.stop_spinner
68
+ @@spinner[:on] = false
69
+ end
70
+
71
+ def self.wait_spinner options = {}
72
+ @@spinner[:delay] = options[:fps] * 60 if options[:fps]
73
+ @@spinner[:delay] = options[:delay] if options[:delay]
74
+ Verbose.start_spinner
75
+ yield
76
+ Verbose.stop_spinner
77
+ end
78
+
79
+
80
+ ## MESSAGE CONSISTENCY METHODS
81
+
82
+ def self.say *args
83
+ args.each { |a| $stdout.puts a }
84
+ end
85
+
86
+ def self.mention *args
87
+ args.each { |a| $stdout.puts "#{a.to_s.color(:blue)}" }
88
+ end
89
+
90
+ def self.cheer *args
91
+ args.each { |a| $stdout.puts "#{a.to_s.color(:green)}" }
92
+ end
93
+
94
+ def self.yell *args
95
+ args.each { |a| $stderr.puts "\n#{a.to_s.color(:red)}" }
96
+ end
97
+
98
+ def self.scream *args
99
+ args.each { |a| $stderr.puts "\n#{a.to_s.color(:red).background(:yellow).underline.bright.blink}" }
100
+ end
101
+
102
+
103
+ ## IO CAPTURING/FORMATTING METHODS ## allow method invocations such as Verbose.quiet.no_logging to work as expected
104
+
105
+ def self.option keyword=false
106
+ case keyword
107
+ when :loud then @@temporary_options[:quiet] = false
108
+ when :quiet then @@temporary_options[:quiet] = true
109
+ when :no_logging then @@temporary_options[:no_logging] = true
110
+ when :logging then @@temporary_options[:no_logging] = false
111
+ else
112
+ return @@temporary_options # else return option hash
113
+ end
114
+ self # return self for chaining
115
+ end
116
+
117
+ def self.quietly description=nil # allows chaining unless a block is given
118
+ return Verbose.option :quiet unless block_given? # if no block set 'quiet: true'
119
+
120
+ if Verbose.option[:no_logging] # if no logging, then turn if off
121
+ old_logger = ActiveRecord::Base.logger
122
+ ActiveRecord::Base.logger = nil
123
+
124
+ ret = Verbose.quiet! description, &Proc.new
125
+
126
+ ActiveRecord::Base.logger = old_logger
127
+ else
128
+ ret = Verbose.quiet! description, &Proc.new # if logging is ok, log away
129
+ end
130
+
131
+ @@temporary_options = {}
132
+
133
+ ret
134
+ end
135
+
136
+ def self.loudly description=nil
137
+ return Verbose.option :loud unless block_given?
138
+
139
+ if Verbose.option[:no_logging] # if no logging, then turn if off
140
+ old_logger = ActiveRecord::Base.logger
141
+ ActiveRecord::Base.logger = nil
142
+
143
+ ret = Verbose.loud! description, &Proc.new
144
+
145
+ ActiveRecord::Base.logger = old_logger
146
+ else
147
+ ret = Verbose.loud! description, &Proc.new # if logging is ok, log away
148
+ end
149
+
150
+ @@temporary_options = {}
151
+ ret
152
+ end
153
+
154
+ def self.logging description=nil
155
+ return Verbose.option :logging unless block_given?
156
+
157
+ if Verbose.option[:quiet]
158
+ ret = Verbose.quiet! description, &Proc.new
159
+ else
160
+ ret = Verbose.loud! description, &Proc.new
161
+ end
162
+
163
+ @@temporary_options = {}
164
+ ret
165
+ end
166
+
167
+ def self.no_logging description=nil
168
+ return Verbose.option :no_logging unless block_given?
169
+
170
+ old_logger = ActiveRecord::Base.logger
171
+ ActiveRecord::Base.logger = nil
172
+
173
+ if Verbose.option[:quiet]
174
+ ret = Verbose.quiet! description, &Proc.new
175
+ else
176
+ ret = Verbose.loud! description, &Proc.new
177
+ end
178
+ ActiveRecord::Base.logger = old_logger
179
+ @@temporary_options = {}
180
+ ret
181
+ end
182
+
183
+ ##
184
+
185
+
186
+ def self.loud! description="NO DESCRIPTION" # captures output from inside of the provided block and outputs them formatted
187
+ start_time = Time.now
188
+ # save a reference to the two IO's
189
+ out = $stdout
190
+ err = $stderr
191
+ out.puts "/ #{description.to_s.widthize(WIDTH-4).bright} \\".color(:magenta)
192
+ begin # IO and Thread stuffs
193
+ Verbose.start_spinner
194
+ read_out, write_out = IO.pipe
195
+ read_err, write_err = IO.pipe
196
+ $stderr = write_err
197
+ $stdout = write_out
198
+ out_thread = Thread.new { out.print @@out_indent + read_out.gets("\n") until read_out.eof? }
199
+ err_thread = Thread.new { err.print @@err_indent + read_err.gets("\n") until read_err.eof? }
200
+ ret = yield
201
+ rescue Exception => msg
202
+ err.puts "# #{description.to_s.widthize(WIDTH-4)} FAIL ".bright.color(:red)
203
+ raise msg
204
+ ensure # whether or not the block fails close the pipes
205
+ write_out.close
206
+ write_err.close
207
+ out_thread.join
208
+ err_thread.join
209
+ Verbose.stop_spinner
210
+ end
211
+ out.puts "\\ #{"_ " * 14}".color(:magenta) + "DONE".color(:green).bright + " in #{Time.now - start_time}s".widthize(14).color(:cyan) + " _ _ _ _ _ _ _ /".color(:magenta)
212
+ return ret
213
+ ensure # both IO's go back the way they were found
214
+ $stderr = err
215
+ $stdout = out
216
+ end
217
+
218
+ def self.quiet! description = false
219
+ start_time = Time.now
220
+ # save a reference to the two IO's
221
+ out = $stdout
222
+ err = $stderr
223
+ out.puts description.to_s.widthize(WIDTH-10).bright.color(:magenta) + " ... ".bright.color(:blue) if description
224
+ begin # IO and Thread stuffs
225
+ Verbose.start_spinner
226
+ $stderr = StringIO.new
227
+ $stdout = StringIO.new
228
+ ret = yield
229
+ rescue Exception => msg
230
+ err.print "\e[1A" if description
231
+ err.puts "# #{description.to_s.widthize(WIDTH-6)}".color(:red) + "FAIL".bright.color(:red)
232
+ raise msg
233
+ ensure
234
+ Verbose.stop_spinner
235
+ end
236
+ if description
237
+ out.print "\e[1A"
238
+ out.puts description.to_s.widthize(WIDTH-8).bright.color(:magenta) + "DONE".color(:green).bright + " in #{Time.now - start_time}s".widthize(14).color(:cyan)
239
+ end
240
+ return ret
241
+ ensure # both IO's go back the way they were found
242
+ $stderr = err
243
+ $stdout = out
244
+ end
245
+
246
+
247
+ end
248
+
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: verboss
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ben Behar
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-23 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: lightweight and simple terminal formatting
14
+ email: lazymunky94@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/verboss.rb
20
+ homepage: https://github.com/Ben-Behar/verboss
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.1.9
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: verboss is a terminal formatting tool!
44
+ test_files: []