Module: WormRunner
- Included in:
- RubyProgress::Worm
- Defined in:
- lib/ruby-progress/cli/worm_runner.rb
Overview
Runtime helper methods for RubyProgress::Worm
These methods implement the interactive runtime behavior (animation loop, command execution, daemon mode, etc.) and were extracted from RubyProgress::Worm to reduce class length and improve readability.
Instance Method Summary collapse
- #animate(message: nil, success: nil, error: nil) ⇒ Object
- #animation_loop ⇒ Object
- #animation_loop_daemon_mode(stop_requested_proc: -> { false }) ⇒ Object
- #animation_loop_step ⇒ Object
- #generate_dots(ripple_position, direction) ⇒ Object
- #run_daemon_mode(success_message: nil, show_checkmark: false, control_message_file: nil, icons: {}) ⇒ Object
- #run_indefinitely ⇒ Object
- #run_with_command ⇒ Object
- #stop ⇒ Object
Instance Method Details
#animate(message: nil, success: nil, error: nil) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/ruby-progress/cli/worm_runner.rb', line 15 def animate(message: nil, success: nil, error: nil) @message = if @success_text = success if success @error_text = error if error @running = true original_int_handler = Signal.trap('INT') do @running = false RubyProgress::Utils.clear_line RubyProgress::Utils.show_cursor exit 130 end RubyProgress::Utils.hide_cursor animation_thread = Thread.new { animation_loop } begin if block_given? result = yield @running = false animation_thread.join (@success_text, true) result else animation_thread.join end rescue StandardError => e @running = false animation_thread.join (@error_text || "Error: #{e.}", false) nil ensure $stderr.print "\r\e[2K" RubyProgress::Utils.show_cursor Signal.trap('INT', original_int_handler) if original_int_handler end end |
#animation_loop ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/ruby-progress/cli/worm_runner.rb', line 206 def animation_loop position = 0 direction = 1 while @running = @message && !@message.empty? ? "#{@message} " : '' @output_capture&.redraw($stderr) $stderr.print "\r\e[2K#{@start_chars}#{}#{generate_dots(position, direction)}#{@end_chars}" $stderr.flush sleep @speed position += direction if position >= @length - 1 if @direction_mode == :forward_only position = 0 else direction = -1 end elsif position <= 0 direction = 1 end end end |
#animation_loop_daemon_mode(stop_requested_proc: -> { false }) ⇒ Object
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/ruby-progress/cli/worm_runner.rb', line 231 def animation_loop_daemon_mode(stop_requested_proc: -> { false }) position = 0 direction = 1 frame_count = 0 while @running && !stop_requested_proc.call = @message && !@message.empty? ? "#{@message} " : '' @output_capture&.redraw($stderr) $stderr.print "\r\e[2K" if (frame_count % 10).zero? # Use ANSI save/restore to clear the previous line without moving the # global cursor position. This prevents the animation from erasing # reserved output lines that we draw elsewhere. $stderr.print "\e7" # save $stderr.print "\e[1A\e[2K\r" $stderr.print "\e8" # restore end $stderr.print "#{}#{generate_dots(position, direction)}" $stderr.flush sleep @speed frame_count += 1 position += direction if position >= @length - 1 if @direction_mode == :forward_only position = 0 else direction = -1 end elsif position <= 0 direction = 1 end end end |
#animation_loop_step ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/ruby-progress/cli/worm_runner.rb', line 181 def animation_loop_step return unless @running @position ||= 0 @direction ||= 1 = @message && !@message.empty? ? "#{@message} " : '' @output_capture&.redraw($stderr) $stderr.print "\r\e[2K#{@start_chars}#{}#{generate_dots(@position, @direction)}#{@end_chars}" $stderr.flush sleep @speed @position += @direction if @position >= @length - 1 if @direction_mode == :forward_only @position = 0 else @direction = -1 end elsif @position <= 0 @direction = 1 end end |
#generate_dots(ripple_position, direction) ⇒ Object
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/ruby-progress/cli/worm_runner.rb', line 270 def generate_dots(ripple_position, direction) dots = Array.new(@length) { @style[:baseline] } (0...@length).each do |i| distance = (i - ripple_position).abs case distance when 0 dots[i] = @style[:peak] when 1 if direction == -1 dots[i] = @style[:midline] if i > ripple_position elsif i < ripple_position dots[i] = @style[:midline] end else dots[i] = @style[:baseline] end end dots.join end |
#run_daemon_mode(success_message: nil, show_checkmark: false, control_message_file: nil, icons: {}) ⇒ 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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/ruby-progress/cli/worm_runner.rb', line 129 def run_daemon_mode(success_message: nil, show_checkmark: false, control_message_file: nil, icons: {}) @running = true stop_requested = false original_int_handler = Signal.trap('INT') { stop_requested = true } Signal.trap('USR1') { stop_requested = true } Signal.trap('TERM') { stop_requested = true } Signal.trap('HUP') { stop_requested = true } RubyProgress::Utils.hide_cursor begin animation_loop_daemon_mode(stop_requested_proc: -> { stop_requested }) ensure RubyProgress::Utils.clear_line RubyProgress::Utils.show_cursor = final_checkmark = show_checkmark ? true : false final_success = true if && File.exist?() begin data = JSON.parse(File.read()) = data['message'] if data['message'] final_checkmark = data['checkmark'] if data.key?('checkmark') final_success = data['success'] if data.key?('success') rescue StandardError # ignore parse errors ensure begin File.delete() rescue StandardError nil end end end if RubyProgress::Utils.display_completion( , success: final_success, show_checkmark: final_checkmark, output_stream: :stdout, icons: icons ) end Signal.trap('INT', original_int_handler) if original_int_handler end end |
#run_indefinitely ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/ruby-progress/cli/worm_runner.rb', line 106 def run_indefinitely original_int_handler = Signal.trap('INT') do @running = false RubyProgress::Utils.clear_line RubyProgress::Utils.show_cursor exit 130 end @running = true RubyProgress::Utils.hide_cursor begin animation_loop ensure RubyProgress::Utils.show_cursor Signal.trap('INT', original_int_handler) if original_int_handler end end |
#run_with_command ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/ruby-progress/cli/worm_runner.rb', line 53 def run_with_command return unless @command exit_code = 0 stdout_content = nil begin stdout_content = if $stdout.tty? && (@output_stdout || @output_live) oc = RubyProgress::OutputCapture.new( command: @command, lines: @output_lines || 3, position: @output_position || :above, stream: @output_live || false ) oc.start @output_capture = oc animate do oc.wait end @output_capture = nil # For non-live capture, flush_to handles the output directly if @output_live oc.lines.join("\n") else oc.flush_to($stdout) if @output_stdout nil # Don't return content since it's already been flushed end else animate do Open3.popen3(@command) do |_stdin, stdout, stderr, wait_thr| captured_stdout = stdout.read stderr_content = stderr.read exit_code = wait_thr.value.exitstatus unless wait_thr.value.success? error_msg = @error_text || "Command failed with exit code #{exit_code}" error_msg += ": #{stderr_content.strip}" if stderr_content && !stderr_content.empty? raise StandardError, error_msg end captured_stdout end end end puts stdout_content if @output_stdout && stdout_content rescue StandardError exit exit_code.nonzero? || 1 rescue Interrupt exit 130 end end |
#stop ⇒ Object
125 126 127 |
# File 'lib/ruby-progress/cli/worm_runner.rb', line 125 def stop @running = false end |