Class: Openmeta::UI::Shell

Inherits:
Openmeta::UI show all
Defined in:
lib/openmeta/ui.rb

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Shell

Returns a new instance of Shell.



33
34
35
36
37
38
39
# File 'lib/openmeta/ui.rb', line 33

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.



31
32
33
# File 'lib/openmeta/ui.rb', line 31

def shell=(value)
  @shell = value
end

Instance Method Details

#ask(msg) ⇒ Object



70
71
72
# File 'lib/openmeta/ui.rb', line 70

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

#confirm(msg, newline = nil) ⇒ Object



45
46
47
# File 'lib/openmeta/ui.rb', line 45

def confirm(msg, newline = nil)
  tell_me(msg, :green, newline) if level("confirm")
end

#debug(msg, newline = nil) ⇒ Object



57
58
59
# File 'lib/openmeta/ui.rb', line 57

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

#debug?Boolean

Returns:

  • (Boolean)


61
62
63
64
# File 'lib/openmeta/ui.rb', line 61

def debug?
  # needs to be false instead of nil to be newline param to other methods
  level("debug")
end

#error(msg, newline = nil) ⇒ Object



53
54
55
# File 'lib/openmeta/ui.rb', line 53

def error(msg, newline = nil)
  tell_me(msg, :red, newline) if level("error")
end

#info(msg, newline = nil) ⇒ Object



41
42
43
# File 'lib/openmeta/ui.rb', line 41

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

#level(name = nil) ⇒ Object



79
80
81
# File 'lib/openmeta/ui.rb', line 79

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

#level=(level) ⇒ Object

Raises:

  • (ArgumentError)


74
75
76
77
# File 'lib/openmeta/ui.rb', line 74

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

#quiet?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/openmeta/ui.rb', line 66

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

#silenceObject



92
93
94
95
96
97
# File 'lib/openmeta/ui.rb', line 92

def silence
  old_level, @level = @level, "silent"
  yield
ensure
  @level = old_level
end

#trace(e, newline = nil) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/openmeta/ui.rb', line 83

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



49
50
51
# File 'lib/openmeta/ui.rb', line 49

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