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.



88
89
90
91
92
# File 'lib/hiiro/shell.rb', line 88

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

Instance Attribute Details

#statusObject (readonly)

Returns the value of attribute status.



86
87
88
# File 'lib/hiiro/shell.rb', line 86

def status
  @status
end

#stderrObject (readonly)

Returns the value of attribute stderr.



86
87
88
# File 'lib/hiiro/shell.rb', line 86

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



86
87
88
# File 'lib/hiiro/shell.rb', line 86

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.



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/hiiro/shell.rb', line 129

def self.collect_chunks(io, wait_thr, tee: $stdout)
  output = +""
  loop do
    chunk = io.readpartial(4096)
    tee&.write(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



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

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

#exit_codeObject



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

def exit_code
  status.exitstatus
end

#failed?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/hiiro/shell.rb', line 98

def failed?
  !success?
end

#linesObject

Plain text split into non-empty lines.



122
123
124
# File 'lib/hiiro/shell.rb', line 122

def lines
  plain_text.split("\n").reject(&:empty?)
end

#plain_textObject

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



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

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

#success?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/hiiro/shell.rb', line 94

def success?
  status.success?
end