Module: Verboss

Defined in:
lib/verboss.rb

Constant Summary collapse

WIDTH =

get number of terminal columns and stick with it # defaults to 64

`tput cols`.to_i rescue WIDTH = 64
@@err_indent =
"$ ".magenta
@@out_indent =
"| ".magenta
@@root_stderr =
$stderr
@@root_stdout =
$stdout
@@temporary_options =
{
  quiet:      false,
  no_logging: false
}
@@spinner =
{
  on:    false,
  chars: %w[| / - \\],
  index: 0,
  delay: 0.5,
  debug_level: 0,
  thread: Thread.new {
    while @@spinner[:thread]
      Thread.stop unless @@spinner[:on]
      sleep @@spinner[:delay]
      c =  (@@spinner[:chars][ @@spinner[:index] = (@@spinner[:index] + 1) % @@spinner[:chars].length ] + "\b").white.bg_black.bold
      @@root_stdout.print c
    end
  }
}

Class Method Summary collapse

Class Method Details

.cheer(*args) ⇒ Object



82
83
84
# File 'lib/verboss.rb', line 82

def self.cheer *args
  args.each { |a| $stdout.puts "#{a.to_s.green}" }
end

.level(arg = 1) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/verboss.rb', line 44

def self.level arg = 1
  case ENV["Verboss"]
  when Numeric then arg <= ENV["Verboss"]
  when nil then false
  else
    true
  end
end

.logging(description = nil) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/verboss.rb', line 146

def self.logging description=nil
  return Verboss.option :logging unless block_given?

  if Verboss.option[:quiet]
    ret = Verboss.quiet! description, &Proc.new
  else
    ret = Verboss.loud!  description, &Proc.new
  end

  @@temporary_options = {}
  ret
end

.loud!(description = "NO DESCRIPTION") ⇒ Object

captures output from inside of the provided block and outputs them formatted



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/verboss.rb', line 178

def self.loud! description="NO DESCRIPTION" # captures output from inside of the provided block and outputs them formatted
  start_time = Time.now
  # save a reference to the two IO's
  out = $stdout
  err = $stderr
  out.puts "/ #{description.to_s.fixed_width(WIDTH-3).bold} ".magenta + "\\"
  begin # IO and Thread stuffs
    Verboss.start_spinner
    read_out, write_out = IO.pipe
    read_err, write_err = IO.pipe
    $stderr    = write_err
    $stdout    = write_out
    out_thread = Thread.new { out.print @@out_indent + read_out.gets("\n") until read_out.eof? }
    err_thread = Thread.new { err.print @@err_indent + read_err.gets("\n") until read_err.eof? }
    ret = yield
  rescue Exception => msg
    err.puts "# #{description.to_s.fixed_width(WIDTH-15)} FAIL ".bold.red
    raise msg
  ensure # whether or not the block fails close the pipes
    write_out.close
    write_err.close
    out_thread.join
    err_thread.join
    Verboss.stop_spinner
  end

  out.puts "\\ #{"_ " * ((WIDTH - 22)/4)}".magenta + "DONE".green.bold + " in #{Time.now - start_time}s".fixed_width(14).cyan + "#{" _" * ((WIDTH - 22)/4)} /".magenta
  return ret
ensure # both IO's go back the way they were found
  $stderr = err
  $stdout = out
end

.loudly(description = nil) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/verboss.rb', line 128

def self.loudly description=nil
  return Verboss.option :loud unless block_given?

  if Verboss.option[:no_logging] # if no logging, then turn if off
    old_logger = ActiveRecord::Base.logger
    ActiveRecord::Base.logger = nil

    ret = Verboss.loud! description, &Proc.new

    ActiveRecord::Base.logger = old_logger
  else
    ret = Verboss.loud! description, &Proc.new # if logging is ok, log away
  end

  @@temporary_options = {}
  ret
end

.mention(*args) ⇒ Object



78
79
80
# File 'lib/verboss.rb', line 78

def self.mention *args
  args.each { |a| $stdout.puts "#{a.to_s.blue}" }
end

.no_logging(description = nil) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/verboss.rb', line 159

def self.no_logging description=nil
  return Verboss.option :no_logging unless block_given?

  old_logger = ActiveRecord::Base.logger
  ActiveRecord::Base.logger = nil

  if Verboss.option[:quiet]
    ret = Verboss.quiet! description, &Proc.new
  else
    ret = Verboss.loud!  description, &Proc.new
  end
  ActiveRecord::Base.logger = old_logger
  @@temporary_options = {}
  ret
end

.option(keyword = false) ⇒ Object

IO CAPTURING/FORMATTING METHODS ## allow method invocations such as Verboss.quiet.no_logging to work as expected



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/verboss.rb', line 97

def self.option keyword=false
  case keyword
  when :loud        then @@temporary_options[:quiet] = false
  when :quiet       then @@temporary_options[:quiet] = true
  when :no_logging  then @@temporary_options[:no_logging] = true
  when :logging     then @@temporary_options[:no_logging] = false
  else
    return @@temporary_options # else return option hash
  end
  self # return self for chaining
end

.quiet!(description = false) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/verboss.rb', line 211

def self.quiet! description = false
  start_time = Time.now
  # save a reference to the two IO's
  out = $stdout
  err = $stderr
  out.puts description.to_s.fixed_width(WIDTH-18).bold.magenta  + "....  ".bold.blue if description
  begin # IO and Thread stuffs
    Verboss.start_spinner
    $stderr = StringIO.new
    $stdout = StringIO.new
    ret = yield
  rescue Exception => msg
    if description
      err.print "\e[1A"
      err.print "# #{description.to_s.fixed_width(WIDTH-16)}".red
    end
    err.puts "FAIL".bold.red
    raise msg
  ensure
    Verboss.stop_spinner
  end
  if description
    out.print "\e[1A"
    out.puts description.to_s.fixed_width(WIDTH-18).bold.magenta + "DONE".green.bold + " in #{Time.now - start_time}s".fixed_width(14).cyan
  end
  return ret
ensure # both IO's go back the way they were found
  $stderr = err
  $stdout = out
end

.quietly(description = nil) ⇒ Object

allows chaining unless a block is given



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/verboss.rb', line 109

def self.quietly description=nil # allows chaining unless a block is given
  return Verboss.option :quiet unless block_given? # if no block set 'quiet: true'

  if Verboss.option[:no_logging] # if no logging, then turn if off
    old_logger = ActiveRecord::Base.logger
    ActiveRecord::Base.logger = nil

    ret = Verboss.quiet! description, &Proc.new

    ActiveRecord::Base.logger = old_logger
  else
    ret = Verboss.quiet! description, &Proc.new  # if logging is ok, log away
  end

  @@temporary_options = {}

  ret
end

.say(*args) ⇒ Object

MESSAGE CONSISTENCY METHODS



74
75
76
# File 'lib/verboss.rb', line 74

def self.say *args
  args.each { |a| $stdout.puts a }
end

.scream(*args) ⇒ Object



90
91
92
# File 'lib/verboss.rb', line 90

def self.scream *args
  args.each { |a| $stderr.puts "\n#{a.to_s.red.bg_yellow.underline.bold.blink}" }
end

.start_spinnerObject

WAIT SPINNER ##



54
55
56
57
# File 'lib/verboss.rb', line 54

def self.start_spinner
  @@spinner[:on] = true
  @@spinner[:thread].wakeup if @@spinner[:thread].status == "sleep"
end

.stop_spinnerObject



59
60
61
# File 'lib/verboss.rb', line 59

def self.stop_spinner
  @@spinner[:on] = false
end

.wait_spinner(options = {}) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/verboss.rb', line 63

def self.wait_spinner options = {}
  @@spinner[:delay] = options[:fps] * 60 if options[:fps]
  @@spinner[:delay] = options[:delay] if options[:delay]
  Verboss.start_spinner
  yield
  Verboss.stop_spinner
end

.yell(*args) ⇒ Object



86
87
88
# File 'lib/verboss.rb', line 86

def self.yell *args
  args.each { |a| $stderr.puts "\n#{a.to_s.red}" }
end