Class: Fontist::Utils::UI

Inherits:
Thor
  • Object
show all
Defined in:
lib/fontist/utils/ui.rb

Constant Summary collapse

ALL_LEVELS =
%i[debug info warn error fatal unknown].freeze

Class Method Summary collapse

Class Method Details

.ask(message, options = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/fontist/utils/ui.rb', line 38

def self.ask(message, options = {})
  new.ask(message, options)
rescue Errno::EBADF
  say(<<~MSG.chomp)
    ERROR: Fontist is unable to obtain agreement without an interactive prompt.
    Please provide explicit agreement at execution or re-run Fontist with an interactive prompt.
  MSG
  "error"
end

.debug(message) ⇒ Object



52
53
54
# File 'lib/fontist/utils/ui.rb', line 52

def self.debug(message)
  new.say(message) if log_levels.include?(:debug)
end

.default_levelObject



22
23
24
# File 'lib/fontist/utils/ui.rb', line 22

def self.default_level
  :fatal
end

.error(message) ⇒ Object



30
31
32
# File 'lib/fontist/utils/ui.rb', line 30

def self.error(message)
  new.say(message, :red) if log_levels.include?(:warn)
end

.levelObject



18
19
20
# File 'lib/fontist/utils/ui.rb', line 18

def self.level
  @level || default_level
end

.level=(level) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/fontist/utils/ui.rb', line 8

def self.level=(level)
  unless ALL_LEVELS.include?(level)
    raise Errors::GeneralError,
          "Unknown log level: #{level.inspect}. " \
          "Supported levels are #{ALL_LEVELS.map(&:inspect).join(', ')}."
  end

  @level = level
end

.log_levelsObject



56
57
58
59
# File 'lib/fontist/utils/ui.rb', line 56

def self.log_levels
  @log_levels ||= {}
  @log_levels[@level] ||= ALL_LEVELS.drop_while { |l| l != level }
end


48
49
50
# File 'lib/fontist/utils/ui.rb', line 48

def self.print(message)
  super if log_levels.include?(:info)
end

.say(message) ⇒ Object



34
35
36
# File 'lib/fontist/utils/ui.rb', line 34

def self.say(message)
  new.say(message) if log_levels.include?(:info)
end

.success(message) ⇒ Object



26
27
28
# File 'lib/fontist/utils/ui.rb', line 26

def self.success(message)
  new.say(message, :green) if log_levels.include?(:info)
end