Module: TwirlRunner
- Defined in:
- lib/ruby-progress/cli/twirl_runner.rb
Overview
Top-level runtime helper module for the Twirl CLI.
Contains helper methods used by the TwirlCLI dispatcher. These methods implement the runtime behavior (running a command, running indefinitely, or launching in daemon mode) and were extracted to reduce module size and improve testability.
Class Method Summary collapse
-
.resolve_pid_file(options, name_key) ⇒ String
Resolve pid file helper used by run_daemon_mode and CLI.
-
.run_daemon_mode(options) ⇒ void
Run the spinner in daemon mode.
-
.run_indefinitely(options) ⇒ void
Run the spinner indefinitely until interrupted (SIGINT).
-
.run_with_command(options) ⇒ void
Runtime helper: run the provided command while showing a twirl spinner.
Class Method Details
.resolve_pid_file(options, name_key) ⇒ String
Resolve pid file helper used by run_daemon_mode and CLI.
173 174 175 176 177 178 179 180 181 |
# File 'lib/ruby-progress/cli/twirl_runner.rb', line 173 def self.resolve_pid_file(, name_key) return [:pid_file] if [:pid_file] if [name_key] "/tmp/ruby-progress/#{[name_key]}.pid" else RubyProgress::Daemon.default_pid_file end end |
.run_daemon_mode(options) ⇒ void
This method returns an undefined value.
Run the spinner in daemon mode. Writes a pid file and listens for control messages via the daemon control message file.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 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 |
# File 'lib/ruby-progress/cli/twirl_runner.rb', line 114 def self.run_daemon_mode() pid_file = resolve_pid_file(, :daemon_name) FileUtils.mkdir_p(File.dirname(pid_file)) File.write(pid_file, Process.pid.to_s) = [:message] spinner = TwirlSpinner.new(, ) stop_requested = false Signal.trap('INT') { stop_requested = true } Signal.trap('USR1') { stop_requested = true } Signal.trap('TERM') { stop_requested = true } Signal.trap('HUP') { stop_requested = true } begin RubyProgress::Utils.hide_cursor spinner.animate until stop_requested ensure RubyProgress::Utils.clear_line RubyProgress::Utils.show_cursor # Check for control message cmf = RubyProgress::Daemon.(pid_file) if File.exist?(cmf) begin data = JSON.parse(File.read(cmf)) = data['message'] check = data.key?('checkmark') ? data['checkmark'] : false success_val = data.key?('success') ? data['success'] : true if RubyProgress::Utils.display_completion( , success: success_val, show_checkmark: check, output_stream: :stdout, icons: { success: [:success_icon], error: [:error_icon] } ) end rescue StandardError # ignore ensure begin File.delete(cmf) rescue StandardError nil end end end FileUtils.rm_f(pid_file) end end |
.run_indefinitely(options) ⇒ void
This method returns an undefined value.
Run the spinner indefinitely until interrupted (SIGINT).
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/ruby-progress/cli/twirl_runner.rb', line 85 def self.run_indefinitely() = [:message] spinner = TwirlSpinner.new(, ) begin RubyProgress::Utils.hide_cursor loop { spinner.animate } rescue Interrupt RubyProgress::Utils.clear_line RubyProgress::Utils.show_cursor exit 130 ensure RubyProgress::Utils.show_cursor if [:success] || [:checkmark] RubyProgress::Utils.display_completion( [:success] || 'Complete', success: true, show_checkmark: [:checkmark], icons: { success: [:success_icon], error: [:error_icon] } ) end end end |
.run_with_command(options) ⇒ void
This method returns an undefined value.
Runtime helper: run the provided command while showing a twirl spinner. Captures output via RubyProgress::OutputCapture when appropriate and prints final completion messages according to options.
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 52 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 |
# File 'lib/ruby-progress/cli/twirl_runner.rb', line 21 def self.run_with_command() = [:message] captured_output = nil spinner = TwirlSpinner.new(, ) success = false begin RubyProgress::Utils.hide_cursor spinner_thread = Thread.new { loop { spinner.animate } } if $stdout.tty? && ([:stdout] || [:stdout_live]) oc = RubyProgress::OutputCapture.new( command: [:command], lines: [:output_lines] || 3, position: [:output_position] || :above, stream: [:stdout] || [:stdout_live] ) oc.start spinner.instance_variable_set(:@output_capture, oc) # wait for command while spinner thread runs oc.wait captured_lines = oc.lines captured_output = captured_lines.join("\n") success = true else captured_output = `#{[:command]} 2>&1` success = $CHILD_STATUS.success? end spinner_thread.kill RubyProgress::Utils.clear_line rescue Interrupt spinner_thread&.kill RubyProgress::Utils.clear_line RubyProgress::Utils.show_cursor exit 130 ensure RubyProgress::Utils.show_cursor end puts captured_output if [:stdout] if [:success] || [:error] || [:checkmark] final_msg = success ? [:success] : [:error] final_msg ||= success ? 'Success' : 'Failed' RubyProgress::Utils.display_completion( final_msg, success: success, show_checkmark: [:checkmark], icons: { success: [:success_icon], error: [:error_icon] } ) end exit success ? 0 : 1 end |