module Verboss

Constants

WIDTH

Public Class Methods

cheer(*args) click to toggle source
# File lib/verboss.rb, line 82
def self.cheer *args
  args.each { |a| $stdout.puts "#{a.to_s.green}" }
end
level(arg = 1) click to toggle source
# 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) click to toggle source
# 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") { || ... } click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/verboss.rb, line 78
def self.mention *args
  args.each { |a| $stdout.puts "#{a.to_s.blue}" }
end
no_logging(description=nil) click to toggle source
# 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) click to toggle source

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

# 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) { || ... } click to toggle source
# 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) click to toggle source
# 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) click to toggle source

MESSAGE CONSISTENCY METHODS

# File lib/verboss.rb, line 74
def self.say *args
  args.each { |a| $stdout.puts a }
end
scream(*args) click to toggle source
# 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_spinner() click to toggle source

WAIT SPINNER ##

# File lib/verboss.rb, line 54
def self.start_spinner
  @@spinner[:on] = true
  @@spinner[:thread].wakeup if @@spinner[:thread].status == "sleep"
end
stop_spinner() click to toggle source
# File lib/verboss.rb, line 59
def self.stop_spinner
  @@spinner[:on] = false
end
wait_spinner(options = {}) { || ... } click to toggle source
# 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) click to toggle source
# File lib/verboss.rb, line 86
def self.yell *args
  args.each { |a| $stderr.puts "\n#{a.to_s.red}" }
end