Class: CLI::UI::StdoutRouter::Capture
- Inherits:
-
Object
- Object
- CLI::UI::StdoutRouter::Capture
- Defined in:
- lib/cli/ui/stdout_router.rb
Defined Under Namespace
Classes: BlockingInput
Instance Attribute Summary collapse
-
#print_captured_output ⇒ Object
: bool.
Class Method Summary collapse
-
.current_capture ⇒ Object
: -> Capture?.
-
.current_capture! ⇒ Object
: -> Capture.
-
.in_alternate_screen(&block) ⇒ Object
: [T] { -> T } -> T.
-
.stdin_synchronize(&block) ⇒ Object
: [T] { -> T } -> T.
-
.with_stdin_masked(&block) ⇒ Object
: [T] { -> T } -> T.
Instance Method Summary collapse
-
#initialize(with_frame_inset: true, merged_output: false, duplicate_output_to: File.open(File::NULL, 'w'), &block) ⇒ Capture
constructor
: (?with_frame_inset: bool, ?merged_output: bool, ?duplicate_output_to: IO) { -> void } -> void.
-
#run ⇒ Object
: -> untyped.
-
#stderr ⇒ Object
: -> String.
-
#stdout ⇒ Object
: -> String.
Constructor Details
#initialize(with_frame_inset: true, merged_output: false, duplicate_output_to: File.open(File::NULL, 'w'), &block) ⇒ Capture
: (?with_frame_inset: bool, ?merged_output: bool, ?duplicate_output_to: IO) { -> void } -> void
184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/cli/ui/stdout_router.rb', line 184 def initialize( with_frame_inset: true, merged_output: false, duplicate_output_to: File.open(File::NULL, 'w'), &block ) @with_frame_inset = with_frame_inset @merged_output = merged_output @duplicate_output_to = duplicate_output_to @block = block @print_captured_output = false @out = StringIO.new @err = StringIO.new end |
Instance Attribute Details
#print_captured_output ⇒ Object
: bool
200 201 202 |
# File 'lib/cli/ui/stdout_router.rb', line 200 def print_captured_output @print_captured_output end |
Class Method Details
.current_capture ⇒ Object
: -> Capture?
101 102 103 |
# File 'lib/cli/ui/stdout_router.rb', line 101 def current_capture Thread.current[:cliui_current_capture] end |
.current_capture! ⇒ Object
: -> Capture
106 107 108 |
# File 'lib/cli/ui/stdout_router.rb', line 106 def current_capture! current_capture #: as !nil end |
.in_alternate_screen(&block) ⇒ Object
: [T] { -> T } -> T
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/cli/ui/stdout_router.rb', line 111 def in_alternate_screen(&block) stdin_synchronize do previous_print_captured_output = current_capture&.print_captured_output current_capture&.print_captured_output = true Spinner::SpinGroup.pause_spinners do if outermost_uncaptured? begin prev_hook = Thread.current[:cliui_output_hook] Thread.current[:cliui_output_hook] = nil replay = current_capture!.stdout.gsub(ANSI.match_alternate_screen, '') CLI::UI.raw do print("#{ANSI.enter_alternate_screen}#{replay}") end ensure Thread.current[:cliui_output_hook] = prev_hook end end block.call ensure print(ANSI.exit_alternate_screen) if outermost_uncaptured? end ensure current_capture&.print_captured_output = !!previous_print_captured_output end end |
.stdin_synchronize(&block) ⇒ Object
: [T] { -> T } -> T
138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/cli/ui/stdout_router.rb', line 138 def stdin_synchronize(&block) @stdin_mutex.synchronize do case $stdin when BlockingInput $stdin.synchronize do block.call end else block.call end end end |
.with_stdin_masked(&block) ⇒ Object
: [T] { -> T } -> T
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/cli/ui/stdout_router.rb', line 152 def with_stdin_masked(&block) @capture_mutex.synchronize do if @active_captures.zero? @stdin_mutex.synchronize do @saved_stdin = $stdin $stdin = BlockingInput.new(@saved_stdin) end end @active_captures += 1 end yield ensure @capture_mutex.synchronize do @active_captures -= 1 if @active_captures.zero? @stdin_mutex.synchronize do $stdin = @saved_stdin end end end end |
Instance Method Details
#run ⇒ Object
: -> untyped
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/cli/ui/stdout_router.rb', line 203 def run require 'stringio' StdoutRouter.assert_enabled! Thread.current[:cliui_current_capture] = self 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 stream = :stdout if @merged_output case stream when :stdout @out.write(data) @duplicate_output_to.write(data) when :stderr @err.write(data) else raise end print_captured_output # suppress writing to terminal by default end @block.call end ensure Thread.current[:cliui_output_hook] = prev_hook Thread.current[:no_cliui_frame_inset] = prev_frame_inset Thread.current[:cliui_current_capture] = nil end |
#stderr ⇒ Object
: -> String
246 247 248 |
# File 'lib/cli/ui/stdout_router.rb', line 246 def stderr @err.string end |
#stdout ⇒ Object
: -> String
241 242 243 |
# File 'lib/cli/ui/stdout_router.rb', line 241 def stdout @out.string end |