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



46
47
48
49
50
51
52
53
54
# File 'lib/fontist/utils/ui.rb', line 46

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



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

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

.debug?Boolean

Returns:

  • (Boolean)


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

def self.debug?
  log_levels.include?(:debug)
end

.default_levelObject



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

def self.default_level
  :fatal
end

.env_levelObject



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

def self.env_level
  ENV["FONTIST_LOG"]&.to_sym
end

.error(message) ⇒ Object



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

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 ||= env_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



64
65
66
67
# File 'lib/fontist/utils/ui.rb', line 64

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


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

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

.say(message) ⇒ Object



42
43
44
# File 'lib/fontist/utils/ui.rb', line 42

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

.success(message) ⇒ Object



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

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