Class: TTY::Command::Result

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/tty/command/result.rb

Overview

Encapsulates the information on the command executed

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status, out, err, runtime = 0.0) ⇒ Result

Create a result


25
26
27
28
29
30
# File 'lib/tty/command/result.rb', line 25

def initialize(status, out, err, runtime = 0.0)
  @status = status
  @out    = out
  @err    = err
  @runtime = runtime
end

Instance Attribute Details

#errObject (readonly) Also known as: stderr

All data written out to process's stdin stream


16
17
18
# File 'lib/tty/command/result.rb', line 16

def err
  @err
end

#outObject (readonly) Also known as: stdout

All data written out to process's stdout stream


12
13
14
# File 'lib/tty/command/result.rb', line 12

def out
  @out
end

#runtimeObject (readonly)

Total command execution time


20
21
22
# File 'lib/tty/command/result.rb', line 20

def runtime
  @runtime
end

Instance Method Details

#==(other) ⇒ Object


83
84
85
86
# File 'lib/tty/command/result.rb', line 83

def ==(other)
  return false unless other.is_a?(TTY::Command::Result)
  @status == other.to_i && to_ary == other.to_ary
end

#each(separator = nil) ⇒ Object

Enumerate over output lines

Parameters:

  • separator (String) (defaults to: nil)

37
38
39
40
41
42
43
44
45
46
# File 'lib/tty/command/result.rb', line 37

def each(separator = nil)
  sep = separator || TTY::Command.record_separator
  return unless @out
  elements = @out.split(sep)
  if block_given?
    elements.each { |line| yield(line) }
  else
    elements.to_enum
  end
end

#exit_statusObject Also known as: exitstatus, status

Information on how the process exited


51
52
53
# File 'lib/tty/command/result.rb', line 51

def exit_status
  @status
end

#exited?Boolean Also known as: complete?

Returns:

  • (Boolean)

69
70
71
# File 'lib/tty/command/result.rb', line 69

def exited?
  @status != nil
end

#failure?Boolean Also known as: failed?

Returns:

  • (Boolean)

78
79
80
# File 'lib/tty/command/result.rb', line 78

def failure?
  !success?
end

#success?Boolean

Returns:

  • (Boolean)

74
75
76
# File 'lib/tty/command/result.rb', line 74

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

#to_aryObject


65
66
67
# File 'lib/tty/command/result.rb', line 65

def to_ary
  [@out, @err]
end

#to_iObject


57
58
59
# File 'lib/tty/command/result.rb', line 57

def to_i
  @status
end

#to_sObject


61
62
63
# File 'lib/tty/command/result.rb', line 61

def to_s
  @status.to_s
end