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) ⇒ Object
- .run_daemon_mode(options) ⇒ Object
- .run_indefinitely(options) ⇒ Object
- .run_with_command(options) ⇒ Object
Class Method Details
.resolve_pid_file(options, name_key) ⇒ Object
153 154 155 156 157 158 159 160 161 |
# File 'lib/ruby-progress/cli/twirl_runner.rb', line 153 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) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/ruby-progress/cli/twirl_runner.rb', line 99 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) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/ruby-progress/cli/twirl_runner.rb', line 75 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) ⇒ 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/ruby-progress/cli/twirl_runner.rb', line 15 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 |