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



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

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

  @status == other.to_i && to_ary == other.to_ary
end

#each(separator = nil, &block) ⇒ Object

Enumerate over output lines

Parameters:

  • separator (String) (defaults to: nil)


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

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

#exit_statusObject Also known as: exitstatus, status

Information on how the process exited



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

def exit_status
  @status
end

#exited?Boolean Also known as: complete?

Returns:

  • (Boolean)


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

def exited?
  @status != nil
end

#failure?Boolean Also known as: failed?

Returns:

  • (Boolean)


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

def failure?
  !success?
end

#success?Boolean

Returns:

  • (Boolean)


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

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

#to_aryObject



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

def to_ary
  [@out, @err]
end

#to_iObject



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

def to_i
  @status
end

#to_sObject



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

def to_s
  @status.to_s
end