Class: Hiiro::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/hiiro/shell.rb

Constant Summary collapse

ANSI_PATTERN =

Matches the full ANSI/VT100 escape sequence spec: cursor movement, erase, colors, SGR — everything a terminal interprets.

^[

catches all single-char Fe sequences (e.g. eM, eD); […] catches CSI.

x20-x2f used instead of space-to-slash to avoid ambiguity with the / regex delimiter.

/\e(?:\[[0-?]*[\x20-\x2f]*[@-~]|[^\[])/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stdout, status, stderr: nil) ⇒ Result

Returns a new instance of Result.



104
105
106
107
108
# File 'lib/hiiro/shell.rb', line 104

def initialize(stdout, status, stderr: nil)
  @stdout = stdout
  @stderr = stderr
  @status = status
end

Instance Attribute Details

#statusObject (readonly)

Returns the value of attribute status.



102
103
104
# File 'lib/hiiro/shell.rb', line 102

def status
  @status
end

#stderrObject (readonly)

Returns the value of attribute stderr.



102
103
104
# File 'lib/hiiro/shell.rb', line 102

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



102
103
104
# File 'lib/hiiro/shell.rb', line 102

def stdout
  @stdout
end

Class Method Details

.collect_chunks(io, wait_thr, tee: $stdout) ⇒ Object

Factory: reads chunks from an IO (popen handle), optionally tee-ing each chunk to ‘tee` as it arrives. Used by Shell.stream / stream_combined. Pass tee: nil to capture without printing.



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/hiiro/shell.rb', line 83

def self.collect_chunks(io, wait_thr, tee: $stdout)
  output = +""
  loop do
    chunk = io.readpartial(4096)
    tee&.write(chunk)
    puts chunk
    output << chunk
  rescue EOFError
    break
  end
  new(output, wait_thr.value)
end

Instance Method Details

#display(out: $stdout) ⇒ Object

Replay buffered output to the terminal with ANSI sequences intact —colors, cursor movement, line clears all work as they did live. Returns self so you can chain: result.display.lines



125
126
127
128
129
# File 'lib/hiiro/shell.rb', line 125

def display(out: $stdout)
  out.write(stdout)
  out.flush
  self
end

#exit_codeObject



118
119
120
# File 'lib/hiiro/shell.rb', line 118

def exit_code
  status.exitstatus
end

#failed?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/hiiro/shell.rb', line 114

def failed?
  !success?
end

#linesObject

Plain text split into non-empty lines.



138
139
140
# File 'lib/hiiro/shell.rb', line 138

def lines
  @lines ||= plain_text.lines(chomp: true)
end

#plain_textObject

Strip ANSI escape codes for text processing or filtering. Also strips bare r (carriage-return-only, used by progress bars).



133
134
135
# File 'lib/hiiro/shell.rb', line 133

def plain_text
  @plain_text ||= stdout.gsub(ANSI_PATTERN, '').gsub(/\r(?!\n)/, '')
end

#success?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/hiiro/shell.rb', line 110

def success?
  status.success?
end