Module: GDK::Output::ClassMethods

Included in:
GDK::Output, GDK::Output
Defined in:
lib/gdk/output.rb

Instance Method Summary collapse

Instance Method Details

#abort(message, exception = nil, report_error: true) ⇒ Object



136
137
138
139
140
# File 'lib/gdk/output.rb', line 136

def abort(message, exception = nil, report_error: true)
  Telemetry.capture_exception(exception || message) if report_error

  Kernel.abort(format_error(message))
end

#ansi(code) ⇒ Object



61
62
63
# File 'lib/gdk/output.rb', line 61

def ansi(code)
  "\e[#{code}m"
end

#color(index) ⇒ Object



57
58
59
# File 'lib/gdk/output.rb', line 57

def color(index)
  COLORS.values[index % COLORS.size]
end

#colorize?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'lib/gdk/output.rb', line 156

def colorize?
  interactive? && ENV.fetch('NO_COLOR', '').empty?
end

#debug(message) ⇒ Object



120
121
122
123
124
# File 'lib/gdk/output.rb', line 120

def debug(message)
  return unless GDK.config.gdk.__debug?

  puts(icon(:debug) + wrap_in_color('DEBUG', COLOR_CODE_BLUE) + ": #{message}", stderr: true)
end

#divider(symbol: '-', length: 80, stderr: false) ⇒ Object



100
101
102
# File 'lib/gdk/output.rb', line 100

def divider(symbol: '-', length: 80, stderr: false)
  puts(symbol * length, stderr: stderr)
end

#ensure_utf8(str) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/gdk/output.rb', line 38

def ensure_utf8(str)
  return '' if str.nil?
  return str unless str.is_a?(String)

  str = force_encode_utf8(str)
  return str if str.valid_encoding?

  str.encode('UTF-8', invalid: :replace, undef: :replace)
end

#error(message, exception = nil, report_error: true) ⇒ Object



130
131
132
133
134
# File 'lib/gdk/output.rb', line 130

def error(message, exception = nil, report_error: true)
  Telemetry.capture_exception(exception || message) if report_error

  puts(format_error(message), stderr: true)
end

#force_encode_utf8(message) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/gdk/output.rb', line 49

def force_encode_utf8(message)
  return message if message.encoding == Encoding::UTF_8 && message.valid_encoding?

  message = message.dup if message.respond_to?(:frozen?) && message.frozen?

  message.force_encoding('UTF-8')
end

#format_error(message) ⇒ Object



126
127
128
# File 'lib/gdk/output.rb', line 126

def format_error(message)
  icon(:error) + wrap_in_color('ERROR', COLOR_CODE_RED) + ": #{message}"
end

#icon(code) ⇒ Object



146
147
148
149
150
# File 'lib/gdk/output.rb', line 146

def icon(code)
  return '' unless colorize?

  "#{ICONS[code]} "
end

#info(message) ⇒ Object



112
113
114
# File 'lib/gdk/output.rb', line 112

def info(message)
  puts(icon(:info) + message)
end

#interactive?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/gdk/output.rb', line 152

def interactive?
  $stdin.isatty
end

#notice(message) ⇒ Object



104
105
106
# File 'lib/gdk/output.rb', line 104

def notice(message)
  puts(notice_format(message))
end

#notice_format(message) ⇒ Object



108
109
110
# File 'lib/gdk/output.rb', line 108

def notice_format(message)
  "=> #{message}"
end


87
88
89
90
91
92
# File 'lib/gdk/output.rb', line 87

def print(message = nil, stderr: false)
  output = stderr ? stderr_handle : stdout_handle

  output.print(ensure_utf8(message))
rescue IOError
end

#prompt(message) ⇒ Object



160
161
162
163
164
165
166
167
168
169
# File 'lib/gdk/output.rb', line 160

def prompt(message)
  Kernel.print("#{message}: ")
  $stdout.flush

  raise 'Interactive terminal not available, aborting.' unless interactive?

  $stdin.gets.to_s.chomp
rescue Interrupt
  ''
end

#puts(message = nil, stderr: false) ⇒ Object



94
95
96
97
98
# File 'lib/gdk/output.rb', line 94

def puts(message = nil, stderr: false)
  output = stderr ? stderr_handle : stdout_handle

  output.puts(ensure_utf8(message))
end

#reset_colorObject



65
66
67
# File 'lib/gdk/output.rb', line 65

def reset_color
  ansi(0)
end

#stderr_handleObject



81
82
83
84
85
# File 'lib/gdk/output.rb', line 81

def stderr_handle
  return Support::Rake::TaskLogger.current.file if Support::Rake::TaskLogger.current

  $stderr.tap { |handle| handle.sync = true }
end

#stdout_handleObject



75
76
77
78
79
# File 'lib/gdk/output.rb', line 75

def stdout_handle
  return Support::Rake::TaskLogger.current.file if Support::Rake::TaskLogger.current

  $stdout.tap { |handle| handle.sync = true }
end

#success(message) ⇒ Object



142
143
144
# File 'lib/gdk/output.rb', line 142

def success(message)
  puts(icon(:success) + message)
end

#warn(message) ⇒ Object



116
117
118
# File 'lib/gdk/output.rb', line 116

def warn(message)
  puts(icon(:warning) + wrap_in_color('WARNING', COLOR_CODE_YELLOW) + ": #{message}", stderr: true)
end

#wrap_in_color(message, color_code) ⇒ Object



69
70
71
72
73
# File 'lib/gdk/output.rb', line 69

def wrap_in_color(message, color_code)
  return message unless colorize?

  "#{ansi(color_code)}#{ensure_utf8(message)}#{reset_color}"
end