Class: TapDance::UI::Shell

Inherits:
TapDance::UI show all
Defined in:
lib/tap_dance/ui.rb

Direct Known Subclasses

Logger

Constant Summary collapse

LEVELS =
%w(silent error warn detail confirm info debug)

Instance Attribute Summary collapse

Attributes inherited from TapDance::UI

#log

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Shell

Returns a new instance of Shell.



40
41
42
43
44
45
46
# 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

Instance Attribute Details

#shell=(value) ⇒ Object (writeonly)

Sets the attribute shell

Parameters:

  • value

    the value to set the attribute shell to.



38
39
40
# File 'lib/tap_dance/ui.rb', line 38

def shell=(value)
  @shell = value
end

Instance Method Details

#ask(msg) ⇒ Object



81
82
83
# File 'lib/tap_dance/ui.rb', line 81

def ask(msg)
  @shell.ask(msg)
end

#confirm(msg, newline = nil) ⇒ Object



52
53
54
# 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) ⇒ Object



68
69
70
# File 'lib/tap_dance/ui.rb', line 68

def debug(msg, newline = nil)
  tell_me(msg, nil, newline) if level("debug")
end

#debug?Boolean

Returns:

  • (Boolean)


72
73
74
75
# 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) ⇒ Object



56
57
58
# 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) ⇒ Object



64
65
66
# 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) ⇒ Object



48
49
50
# 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) ⇒ Object



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

def level(name = nil)
  name ? LEVELS.index(name) <= LEVELS.index(@level) : @level
end

#level=(level) ⇒ Object

Raises:

  • (ArgumentError)


85
86
87
88
# File 'lib/tap_dance/ui.rb', line 85

def level=(level)
  raise ArgumentError unless LEVELS.include?(level.to_s)
  @level = level
end

#quiet?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/tap_dance/ui.rb', line 77

def quiet?
  LEVELS.index(@level) <= LEVELS.index("warn")
end

#silenceObject



103
104
105
106
107
108
# 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) ⇒ Object



94
95
96
97
98
99
100
101
# 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) ⇒ Object



60
61
62
# File 'lib/tap_dance/ui.rb', line 60

def warn(msg, newline = nil)
  tell_me(msg, :yellow, newline) if level("warn")
end