Sha256: b066f7e6d91c84c299dc8ee0ce6b0da145581aee20aac2351766d51b93e126c6

Contents?: true

Size: 1.76 KB

Versions: 1

Compression:

Stored size: 1.76 KB

Contents

# frozen_string_literal: true

module TTY
  class Command
    # Encapsulates the information on the command executed
    #
    # @api public
    class Result
      include Enumerable

      # All data written out to process's stdout stream
      attr_reader :out
      alias stdout out

      # All data written out to process's stdin stream
      attr_reader :err
      alias stderr err

      # Total command execution time
      attr_reader :runtime

      # Create a result
      #
      # @api public
      def initialize(status, out, err, runtime = 0.0)
        @status = status
        @out    = out
        @err    = err
        @runtime = runtime
      end

      # Enumerate over output lines
      #
      # @param [String] separator
      #
      # @api public
      def each(separator = nil, &block)
        sep = separator || TTY::Command.record_separator
        return unless @out

        elements = @out.split(sep)
        if block_given?
          elements.each(&block)
        else
          elements.to_enum
        end
      end

      # Information on how the process exited
      #
      # @api public
      def exit_status
        @status
      end
      alias exitstatus exit_status
      alias status exit_status

      def to_i
        @status
      end

      def to_s
        @status.to_s
      end

      def to_ary
        [@out, @err]
      end

      def exited?
        @status != nil
      end
      alias complete? exited?

      def success?
        exited? ?  @status.zero? : false
      end

      def failure?
        !success?
      end
      alias failed? failure?

      def ==(other)
        return false unless other.is_a?(TTY::Command::Result)

        @status == other.to_i && to_ary == other.to_ary
      end
    end # Result
  end # Command
end # TTY

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tty-command-0.10.1 lib/tty/command/result.rb