class Epuber::UI

Constants

Location

Attributes

current_command[RW]

@return [Epuber::Command]

Public Class Methods

error(message, location: nil) click to toggle source

Fatal error, prints message and exit with return code 1

@param [String] message message of the error @param [Thread::Backtrace::Location] location location of the error @param [Bool] backtrace output backtrace locations, nil == automatic, true == always and false == never

# File lib/epuber/user_interface.rb, line 35
def self.error(message, location: nil)
  _clear_processing_line_for_new_output do
    $stdout.puts(_format_message(:error, message, location: location))
    _print_backtrace(location.try(:backtrace_locations) || message.try(:backtrace_locations) || caller_locations, location: location) if current_command && current_command.verbose?
  end
end
error!(message, location: nil) click to toggle source

Fatal error, prints message and exit with return code 1

@param [Exception, String] message message of the error @param [Thread::Backtrace::Location] location location of the error

# File lib/epuber/user_interface.rb, line 24
def self.error!(message, location: nil)
  error(message, location: location)
  exit(1)
end
print_processing_debug_info(info_text) click to toggle source

@param [String] info_text

print_processing_file(file, index, count) click to toggle source

@param [Compiler::FileTypes::AbstractFile] file

@return nil

print_processing_problem(problem) click to toggle source

@param [#to_s] problem some problem, object just have to know to convert self into string with method to_s

print_step_processing_time(step_name, time = nil) { || ... } click to toggle source

@param [Compiler::FileTypes::AbstractFile] file @param [String] step_name @param [Fixnum] time

processing_files_done() click to toggle source
# File lib/epuber/user_interface.rb, line 104
def self.processing_files_done
  remove_processing_file_line

  @current_file = nil
end
remove_processing_file_line() click to toggle source
# File lib/epuber/user_interface.rb, line 92
def self.remove_processing_file_line
  last_line = @last_processing_file_line

  unless @last_processing_file_line.nil?
    $stdout.print("\033[2K") # remove line, but without moving cursor
    $stdout.print("\r") # go to beginning of line
    @last_processing_file_line = nil
  end

  last_line
end
warning(message, location: nil) click to toggle source

@param [String] message message of the error @param [Thread::Backtrace::Location] location location of the error

# File lib/epuber/user_interface.rb, line 45
def self.warning(message, location: nil)
  _clear_processing_line_for_new_output do
    $stdout.puts(_format_message(:warning, message, location: location))
  end
end

Private Class Methods

_clear_processing_line_for_new_output() { || ... } click to toggle source
# File lib/epuber/user_interface.rb, line 142
def self._clear_processing_line_for_new_output
  last_line = remove_processing_file_line

  yield

  @last_processing_file_line = last_line
  $stdout.print(last_line)
end
_color_from_level(level) click to toggle source

@param [Symbol] level color of the output

@return [Symbol] color

# File lib/epuber/user_interface.rb, line 155
def self._color_from_level(level)
  case level
    when :error;   :red
    when :warning; :yellow
    when :normal;  :white
    when :debug;   :blue
    else
      raise "Unknown output level #{level}"
  end
end
_format_backtrace(locations, location: nil) click to toggle source

@param [Array<Thread::Backtrace::Location>] locations locations of the error (only for verbose output) @param [Thread::Backtrace::Location] location location of the error

@return [String] formatted message

# File lib/epuber/user_interface.rb, line 210
def self._format_backtrace(locations, location: nil)
  index = locations.index(location) || 0
  locations[index, locations.size].map { |loc| loc.to_s }
end
_format_message(level, message, location: nil) click to toggle source

@param [Symbol] level color of the output @param [String] message message of the error @param [Thread::Backtrace::Location] location location of the error

@return [String] formatted message

# File lib/epuber/user_interface.rb, line 189
def self._format_message(level, message, location: nil)
  location = _location_from_obj(location)

  comps = []
  comps << message.to_s
  if !location.nil? && !(message.is_a?(Epuber::Compiler::Problem) || message.is_a?(Epuber::Checker::TextChecker::MatchProblem))
    if location.lineno
      comps << "  (in file #{location.path} line #{location.lineno})"
    else
      comps << "  (in file #{location.path})"
    end
  end

  comps.join("\n").ansi.send(_color_from_level(level))
end
_location_from_obj(obj) click to toggle source

@param [Thread::Backtrace::Location, Nokogiri::XML::Node] obj

@return [Location]

# File lib/epuber/user_interface.rb, line 170
def self._location_from_obj(obj)
  case obj
  when ::Thread::Backtrace::Location
    Location.new(obj.path, obj.lineno)
  when ::Nokogiri::XML::Node
    Location.new(obj.document.file_path, obj.line)
  when Location
    obj
  when Epuber::Compiler::FileTypes::AbstractFile
    Location.new(obj.source_path, nil)
  end
end
_print_backtrace(locations, location: nil) click to toggle source

@param [Thread::Backtrace::Location] location location of the error

# File lib/epuber/user_interface.rb, line 217
def self._print_backtrace(locations, location: nil)
  puts(_format_backtrace(locations, location: location)) if current_command.verbose?
end