Class: CLI::UI::StdoutRouter::Capture
- Inherits:
-
Object
- Object
- CLI::UI::StdoutRouter::Capture
- Extended by:
- T::Sig
- Defined in:
- lib/cli/ui/stdout_router.rb
Instance Attribute Summary collapse
-
#stderr ⇒ Object
readonly
Returns the value of attribute stderr.
-
#stdout ⇒ Object
readonly
Returns the value of attribute stdout.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(with_frame_inset: true, &block) ⇒ Capture
constructor
A new instance of Capture.
- #run ⇒ Object
Methods included from T::Sig
Constructor Details
#initialize(with_frame_inset: true, &block) ⇒ Capture
Returns a new instance of Capture.
118 119 120 121 122 123 |
# File 'lib/cli/ui/stdout_router.rb', line 118 def initialize(with_frame_inset: true, &block) @with_frame_inset = with_frame_inset @block = block @stdout = '' @stderr = '' end |
Instance Attribute Details
#stderr ⇒ Object (readonly)
Returns the value of attribute stderr.
126 127 128 |
# File 'lib/cli/ui/stdout_router.rb', line 126 def stderr @stderr end |
#stdout ⇒ Object (readonly)
Returns the value of attribute stdout.
126 127 128 |
# File 'lib/cli/ui/stdout_router.rb', line 126 def stdout @stdout end |
Class Method Details
.with_stdin_masked(&block) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/cli/ui/stdout_router.rb', line 94 def self.with_stdin_masked(&block) @m.synchronize do if @active_captures.zero? @saved_stdin = $stdin $stdin, w = IO.pipe $stdin.close w.close end @active_captures += 1 end yield ensure @m.synchronize do @active_captures -= 1 if @active_captures.zero? $stdin = @saved_stdin end end end |
Instance Method Details
#run ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/cli/ui/stdout_router.rb', line 129 def run require 'stringio' StdoutRouter.assert_enabled! out = StringIO.new err = StringIO.new prev_frame_inset = Thread.current[:no_cliui_frame_inset] prev_hook = Thread.current[:cliui_output_hook] if Thread.current.respond_to?(:report_on_exception) Thread.current.report_on_exception = false end self.class.with_stdin_masked do Thread.current[:no_cliui_frame_inset] = !@with_frame_inset Thread.current[:cliui_output_hook] = ->(data, stream) do case stream when :stdout then out.write(data) when :stderr then err.write(data) else raise end false # suppress writing to terminal end begin @block.call ensure @stdout = out.string @stderr = err.string end end ensure Thread.current[:cliui_output_hook] = prev_hook Thread.current[:no_cliui_frame_inset] = prev_frame_inset end |