Class: Pakyow::Error::CLIFormatter Private

Inherits:
Object
  • Object
show all
Defined in:
lib/pakyow/error.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Constant Summary collapse

HIGHLIGHT_REGEX =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

/`([^']*)'/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(error) ⇒ CLIFormatter

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of CLIFormatter.



191
192
193
# File 'lib/pakyow/error.rb', line 191

def initialize(error)
  @error = error
end

Class Method Details

.format(message) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



263
264
265
266
267
268
269
# File 'lib/pakyow/error.rb', line 263

def format(message)
  message.dup.tap do |message_to_format|
    message.scan(HIGHLIGHT_REGEX).each do |match|
      message_to_format.gsub!("`#{match[0]}'", Support::CLI.style.italic.blue(match[0]))
    end
  end
end

.indent(message) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



253
254
255
256
257
# File 'lib/pakyow/error.rb', line 253

def indent(message)
  message.split("\n").map { |line|
    "  #{line}"
  }.join("\n")
end

Instance Method Details

#backtraceObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



245
246
247
# File 'lib/pakyow/error.rb', line 245

def backtrace
  @error.condensed_backtrace.map(&:to_s).join("\n")
end

#detailsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



241
242
243
# File 'lib/pakyow/error.rb', line 241

def details
  Support::CLI.style.bright_black(self.class.indent(self.class.format(@error.details)))
end

#headerObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



209
210
211
212
213
214
215
216
217
218
219
# File 'lib/pakyow/error.rb', line 209

def header
  start = " #{@error.name.upcase} "

  finish = if @error.project?
    File.basename(@error.path)
  else
    ""
  end

  "#{start}#{" " * (80 - (start.length + finish.length + 2))} #{finish} "
end

#messageObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/pakyow/error.rb', line 221

def message
  message = Support::CLI.style.white.on_red.bold(header)
  message_lines = @error.message.split("\n")
  if message_lines.any?
    message << "\n\n#{self.class.indent(Support::CLI.style.red("") + Support::CLI.style.bright_black(" #{self.class.format(message_lines.shift)}"))}\n"
    message_lines.each do |line|
      message << Support::CLI.style.bright_black("\n#{line}")
    end
  end

  if @error.respond_to?(:contextual_message)
    message = <<~MESSAGE
      #{message}
      #{Support::CLI.style.bright_black(self.class.indent(self.class.format(@error.contextual_message)))}
    MESSAGE
  end

  message.rstrip
end

#to_sObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/pakyow/error.rb', line 195

def to_s
  <<~MESSAGE
    #{message}

    #{Support::CLI.style.black.on_white.bold(" DETAILS                                                                        ")}

    #{details}

    #{Support::CLI.style.black.on_white.bold(" BACKTRACE                                                                      ")}

    #{backtrace}
  MESSAGE
end