Sha256: 75d0dcb66fcdc244c08324ae84dcdcc1c22ee3c632435e1a93f3d8951074fc53

Contents?: true

Size: 1.38 KB

Versions: 6

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

require 'command_kit/stdio'

module CommandKit
  #
  # Provides printing methods.
  #
  module Printing
    include Stdio

    # Platform independent new-line constant
    #
    # @return [String]
    #
    # @api public
    EOL = $/

    #
    # Prints the error message to {Stdio#stderr stderr}.
    #
    # @param [String] message
    #   The error message.
    #
    # @example
    #   print_error "error: invalid input"
    #   # error: invalid input
    #
    # @example When CommandKit::CommandName is included:
    #   print_error "invalid input"
    #   # foo: invalid input
    #
    # @api public
    #
    def print_error(message)
      if respond_to?(:command_name)
        # if #command_name is available, prefix all error messages with it
        stderr.puts "#{command_name}: #{message}"
      else
        # if #command_name is not available, just print the error message as-is
        stderr.puts message
      end
    end

    #
    # Prints an exception to {Stdio#stderr stderr}.
    #
    # @param [Exception] error
    #   The error to print.
    #
    # @example
    #   begin
    #     # ...
    #   rescue => error
    #     print_error "Error encountered"
    #     print_exception(error)
    #     exit(1)
    #   end
    #
    # @api public
    #
    def print_exception(error)
      print_error error.full_message(highlight: stderr.tty?)
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
command_kit-0.5.0 lib/command_kit/printing.rb
command_kit-0.4.1 lib/command_kit/printing.rb
command_kit-0.4.0 lib/command_kit/printing.rb
command_kit-0.3.0 lib/command_kit/printing.rb
command_kit-0.2.2 lib/command_kit/printing.rb
command_kit-0.2.1 lib/command_kit/printing.rb