Module: Strata::CLI::Output

Constant Summary collapse

THEME =
{
  success: :green,
  error: %i[red bold],
  warning: :yellow,
  info: :cyan,
  title: %i[cyan bold],
  highlight: %i[cyan bold],
  dim: :bright_black,
  primary: :blue,
  secondary: :magenta,
  border: %i[cyan dim],
  selected: %i[green bold],
  disabled: :bright_black
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.format(type, message) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/strata/cli/output.rb', line 65

def format(type, message)
  return "" if message.nil?

  styles = THEME[type]
  return message.to_s unless styles

  if styles.is_a?(Array)
    pastel.decorate(message.to_s, *styles)
  else
    pastel.send(styles, message.to_s)
  end
end

.pastelObject



25
26
27
# File 'lib/strata/cli/output.rb', line 25

def pastel
  @pastel ||= Pastel.new(enabled: $stdout.tty?)
end


48
49
50
# File 'lib/strata/cli/output.rb', line 48

def print_error(message, context: nil)
  shell_for(context).say_error(format(:error, message))
end


52
53
54
55
56
# File 'lib/strata/cli/output.rb', line 52

def print_hint(message, context: nil, stderr: false)
  shell = shell_for(context)
  formatted = format(:dim, message)
  stderr ? shell.say_error(formatted) : shell.say(formatted)
end


36
37
38
# File 'lib/strata/cli/output.rb', line 36

def print_info(message, context: nil)
  shell_for(context).say(format(:info, message))
end

Standard status output with Thor’s prefix formatting. We keep the label color via Thor, but decorate the message via Output theme.



60
61
62
63
# File 'lib/strata/cli/output.rb', line 60

def print_status(label, message, type: :info, context: nil)
  shell = shell_for(context)
  shell.say_status(label, format(type, message), thor_color(type))
end


40
41
42
# File 'lib/strata/cli/output.rb', line 40

def print_success(message, context: nil)
  shell_for(context).say(format(:success, message))
end


44
45
46
# File 'lib/strata/cli/output.rb', line 44

def print_warning(message, context: nil)
  shell_for(context).say(format(:warning, message))
end

.shell_for(context = nil) ⇒ Object



29
30
31
32
33
34
# File 'lib/strata/cli/output.rb', line 29

def shell_for(context = nil)
  return context.shell if context&.respond_to?(:shell)
  return context if context.is_a?(Thor::Shell)

  Thor::Shell::Color.new
end

.thor_color(type) ⇒ Object

Thor only understands a limited palette; keep it simple/stable.



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/strata/cli/output.rb', line 79

def thor_color(type)
  case type
  when :success then :green
  when :error then :red
  when :warning then :yellow
  when :info, :title, :border then :cyan
  when :primary then :blue
  when :secondary then :magenta
  when :dim then :white
  else
    :white
  end
end

Instance Method Details



98
# File 'lib/strata/cli/output.rb', line 98

def print_error(message) = Output.print_error(message, context: self)


99
# File 'lib/strata/cli/output.rb', line 99

def print_hint(message, stderr: false) = Output.print_hint(message, context: self, stderr: stderr)

Convenience instance methods for Thor classes.



95
# File 'lib/strata/cli/output.rb', line 95

def print_info(message) = Output.print_info(message, context: self)


100
# File 'lib/strata/cli/output.rb', line 100

def print_status(label, message, type: :info) = Output.print_status(label, message, type: type, context: self)


96
# File 'lib/strata/cli/output.rb', line 96

def print_success(message) = Output.print_success(message, context: self)


97
# File 'lib/strata/cli/output.rb', line 97

def print_warning(message) = Output.print_warning(message, context: self)