Class: Epuber::UserInterface

Inherits:
Object
  • Object
show all
Defined in:
lib/epuber/user_interface.rb

Defined Under Namespace

Classes: Location

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.current_commandEpuber::Command

Returns:



16
17
18
# File 'lib/epuber/user_interface.rb', line 16

def current_command
  @current_command
end

Class Method Details

.error(message, location: nil) ⇒ Object

Fatal error, prints message and exit with return code 1

Parameters:

  • message (String)

    message of the error

  • location (Thread::Backtrace::Location) (defaults to: nil)

    location of the error

  • backtrace (Bool)

    output backtrace locations, nil == automatic, true == always and false == never



35
36
37
38
39
40
# File 'lib/epuber/user_interface.rb', line 35

def self.error(message, location: nil)
  $stdout.puts unless @last_processing_file_line.nil?

  $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.verbose?
end

.error!(message, location: nil) ⇒ Object

Fatal error, prints message and exit with return code 1

Parameters:

  • message (Exception, String)

    message of the error

  • location (Thread::Backtrace::Location) (defaults to: nil)

    location of the error



24
25
26
27
# File 'lib/epuber/user_interface.rb', line 24

def self.error!(message, location: nil)
  error(message, location: location)
  exit(1)
end

Parameters:

  • info_text (String)


65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/epuber/user_interface.rb', line 65

def self.print_processing_debug_info(info_text)
  if current_command && current_command.verbose?
    _clear_processing_line_for_new_output do
      message = if @current_file.nil?
                  "#{info_text}"
                else
                  "#{@current_file.source_path}: #{info_text}"
                end

      $stdout.puts(message.ansi.send(_color_from_level(:debug)))
    end
  end
end

Returns nil.

Parameters:

Returns:

  • nil



83
84
85
86
87
88
89
90
# File 'lib/epuber/user_interface.rb', line 83

def self.print_processing_file(file, index, count)
  remove_processing_file_line

  @current_file = file

  @last_processing_file_line = "▸ Processing #{file.source_path} (#{index + 1} of #{count})"
  $stdout.print(@last_processing_file_line)
end

Parameters:

  • problem (#to_s)

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



57
58
59
60
61
# File 'lib/epuber/user_interface.rb', line 57

def self.print_processing_problem(problem)
  _clear_processing_line_for_new_output do
    $stdout.puts(problem.to_s.ansi.send(_color_from_level(:warning)))
  end
end

.processing_files_doneObject



104
105
106
107
108
# File 'lib/epuber/user_interface.rb', line 104

def self.processing_files_done
  remove_processing_file_line

  @current_file = nil
end

.remove_processing_file_lineObject



92
93
94
95
96
97
98
99
100
101
102
# 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) ⇒ Object

Parameters:



45
46
47
48
49
# 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