class TapDance::UI::Shell

Constants

LEVELS

Attributes

shell[W]

Public Class Methods

new(options = {}) click to toggle source
# File lib/tap_dance/ui.rb, line 40
def initialize(options = {})
  if options["no-color"] || !STDOUT.tty?
    Thor::Base.shell = Thor::Shell::Basic
  end
  @shell = Thor::Base.shell.new
  @level = ENV['DEBUG'] ? "debug" : "info"
end

Public Instance Methods

ask(msg) click to toggle source
# File lib/tap_dance/ui.rb, line 81
def ask(msg)
  @shell.ask(msg)
end
confirm(msg, newline = nil) click to toggle source
# File lib/tap_dance/ui.rb, line 52
def confirm(msg, newline = nil)
  tell_me(msg, :green, newline) if level("confirm")
end
debug(msg, newline = nil) click to toggle source
# File lib/tap_dance/ui.rb, line 68
def debug(msg, newline = nil)
  tell_me(msg, nil, newline) if level("debug")
end
debug?() click to toggle source
# File lib/tap_dance/ui.rb, line 72
def debug?
  # needs to be false instead of nil to be newline param to other methods
  level("debug")
end
detail(msg, newline = nil) click to toggle source
# File lib/tap_dance/ui.rb, line 56
def detail(msg, newline = nil)
  tell_me(msg, :cyan, newline) if level("detail")
end
error(msg, newline = nil) click to toggle source
# File lib/tap_dance/ui.rb, line 64
def error(msg, newline = nil)
  tell_me(msg, :red, newline) if level("error")
end
info(msg, newline = nil) click to toggle source
# File lib/tap_dance/ui.rb, line 48
def info(msg, newline = nil)
  tell_me(msg, nil, newline) if level("info")
end
level(name = nil) click to toggle source
# File lib/tap_dance/ui.rb, line 90
def level(name = nil)
  name ? LEVELS.index(name) <= LEVELS.index(@level) : @level
end
level=(level) click to toggle source
# File lib/tap_dance/ui.rb, line 85
def level=(level)
  raise ArgumentError unless LEVELS.include?(level.to_s)
  @level = level
end
quiet?() click to toggle source
# File lib/tap_dance/ui.rb, line 77
def quiet?
  LEVELS.index(@level) <= LEVELS.index("warn")
end
silence() { || ... } click to toggle source
# File lib/tap_dance/ui.rb, line 103
def silence
  old_level, @level = @level, "silent"
  yield
ensure
  @level = old_level
end
trace(e, newline = nil) click to toggle source
# File lib/tap_dance/ui.rb, line 94
def trace(e, newline = nil)
  msg = ["#{e.class}: #{e.message}", *e.backtrace].join("\n")
  if debug?
    tell_me(msg, nil, newline)
  elsif @trace
    STDERR.puts "#{msg}#{newline}"
  end
end
warn(msg, newline = nil) click to toggle source
# File lib/tap_dance/ui.rb, line 60
def warn(msg, newline = nil)
  tell_me(msg, :yellow, newline) if level("warn")
end

Private Instance Methods

logger(message, newline=(message.to_s !~ /( |\t)$/)) click to toggle source
# File lib/tap_dance/ui.rb, line 133
def logger(message, newline=(message.to_s !~ /( |\t)$/))
  @log ||= ""
  @log << message
  @log << $/ if newline
end
tell_me(msg, color = nil, newline = nil) click to toggle source
# File lib/tap_dance/ui.rb, line 112
def tell_me(msg, color = nil, newline = nil)
  return if (msg.nil? || msg == "") && newline.nil?
  msg = word_wrap(msg) if newline.is_a?(Hash) && newline[:wrap]
  line = if newline.nil? then [] else [newline] end

  if msg.is_a? ShellResult
    unless msg.out.chomp == ""
      @shell.say(msg.out.chomp, color, *line)
      logger(msg.out.chomp, *line)
    end

    unless msg.err.chomp == ""
      @shell.say(msg.err.chomp, :red)
      logger(msg.err.chomp)
    end
  else
    @shell.say(msg, color, *line)
    logger(msg, *line)
  end
end
word_wrap(text, line_width = @shell.terminal_width) click to toggle source
# File lib/tap_dance/ui.rb, line 139
def word_wrap(text, line_width = @shell.terminal_width)
  text.split("\n").collect do |line|
    line.length > line_width ? line.gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1\n").strip : line
  end * "\n"
end