Module: GDK::Output::ClassMethods
- Included in:
- GDK::Output, GDK::Output
- Defined in:
- lib/gdk/output.rb
Instance Method Summary collapse
- #abort(message, exception = nil, report_error: true) ⇒ Object
- #ansi(code) ⇒ Object
- #color(index) ⇒ Object
- #colorize? ⇒ Boolean
- #debug(message) ⇒ Object
- #divider(symbol: '-', length: 80, stderr: false) ⇒ Object
- #ensure_utf8(str) ⇒ Object
- #error(message, exception = nil, report_error: true) ⇒ Object
- #force_encode_utf8(message) ⇒ Object
- #format_error(message) ⇒ Object
- #icon(code) ⇒ Object
- #info(message) ⇒ Object
- #interactive? ⇒ Boolean
- #notice(message) ⇒ Object
- #notice_format(message) ⇒ Object
- #print(message = nil, stderr: false) ⇒ Object
- #prompt(message) ⇒ Object
- #puts(message = nil, stderr: false) ⇒ Object
- #reset_color ⇒ Object
- #stderr_handle ⇒ Object
- #stdout_handle ⇒ Object
- #success(message) ⇒ Object
- #warn(message) ⇒ Object
- #wrap_in_color(message, color_code) ⇒ Object
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(, exception = nil, report_error: true) Telemetry.capture_exception(exception || ) if report_error Kernel.abort(format_error()) 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
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() return unless GDK.config.gdk.__debug? puts(icon(:debug) + wrap_in_color('DEBUG', COLOR_CODE_BLUE) + ": #{}", 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(, exception = nil, report_error: true) Telemetry.capture_exception(exception || ) if report_error puts(format_error(), 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() return if .encoding == Encoding::UTF_8 && .valid_encoding? = .dup if .respond_to?(:frozen?) && .frozen? .force_encoding('UTF-8') end |
#format_error(message) ⇒ Object
126 127 128 |
# File 'lib/gdk/output.rb', line 126 def format_error() icon(:error) + wrap_in_color('ERROR', COLOR_CODE_RED) + ": #{}" 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() puts(icon(:info) + ) end |
#interactive? ⇒ 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() puts(notice_format()) end |
#notice_format(message) ⇒ Object
108 109 110 |
# File 'lib/gdk/output.rb', line 108 def notice_format() "=> #{}" end |
#print(message = nil, stderr: false) ⇒ Object
87 88 89 90 91 92 |
# File 'lib/gdk/output.rb', line 87 def print( = nil, stderr: false) output = stderr ? stderr_handle : stdout_handle output.print(ensure_utf8()) 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() Kernel.print("#{}: ") $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( = nil, stderr: false) output = stderr ? stderr_handle : stdout_handle output.puts(ensure_utf8()) end |
#reset_color ⇒ Object
65 66 67 |
# File 'lib/gdk/output.rb', line 65 def reset_color ansi(0) end |
#stderr_handle ⇒ Object
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_handle ⇒ Object
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() puts(icon(:success) + ) end |
#warn(message) ⇒ Object
116 117 118 |
# File 'lib/gdk/output.rb', line 116 def warn() puts(icon(:warning) + wrap_in_color('WARNING', COLOR_CODE_YELLOW) + ": #{}", 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(, color_code) return unless colorize? "#{ansi(color_code)}#{ensure_utf8()}#{reset_color}" end |