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

Class Method Details

.resolve_pid_file(options, name_key) ⇒ String

Resolve pid file helper used by run_daemon_mode and CLI.

Parameters:

  • options (Hash)
  • name_key (Symbol)

Returns:



173
174
175
176
177
178
179
180
181
# File 'lib/ruby-progress/cli/twirl_runner.rb', line 173

def self.resolve_pid_file(options, name_key)
  return options[:pid_file] if options[:pid_file]

  if options[name_key]
    "/tmp/ruby-progress/#{options[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.

Parameters:

  • options (Hash)


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(options)
  pid_file = resolve_pid_file(options, :daemon_name)
  FileUtils.mkdir_p(File.dirname(pid_file))
  File.write(pid_file, Process.pid.to_s)

  message = options[:message]
  spinner = TwirlSpinner.new(message, options)
  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.control_message_file(pid_file)
    if File.exist?(cmf)
      begin
        data = JSON.parse(File.read(cmf))
        message = data['message']
        check = data.key?('checkmark') ? data['checkmark'] : false
        success_val = data.key?('success') ? data['success'] : true
        if message
          RubyProgress::Utils.display_completion(
            message,
            success: success_val,
            show_checkmark: check,
            output_stream: :stdout,
            icons: { success: options[:success_icon], error: options[: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).

Parameters:

  • options (Hash)

    CLI options used to configure the spinner



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(options)
  message = options[:message]
  spinner = TwirlSpinner.new(message, options)

  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 options[:success] || options[:checkmark]
      RubyProgress::Utils.display_completion(
        options[:success] || 'Complete',
        success: true,
        show_checkmark: options[:checkmark],
        icons: { success: options[:success_icon], error: options[: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.

Parameters:

  • options (Hash)

    CLI options parsed from TwirlCLI::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(options)
  message = options[:message]
  captured_output = nil

  spinner = TwirlSpinner.new(message, options)
  success = false

  begin
    RubyProgress::Utils.hide_cursor
    spinner_thread = Thread.new { loop { spinner.animate } }

    if $stdout.tty? && (options[:stdout] || options[:stdout_live])
      oc = RubyProgress::OutputCapture.new(
        command: options[:command],
        lines: options[:output_lines] || 3,
        position: options[:output_position] || :above,
        stream: options[:stdout] || options[: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 = `#{options[: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 options[:stdout]

  if options[:success] || options[:error] || options[:checkmark]
    final_msg = success ? options[:success] : options[:error]
    final_msg ||= success ? 'Success' : 'Failed'

    RubyProgress::Utils.display_completion(
      final_msg,
      success: success,
      show_checkmark: options[:checkmark],
      icons: { success: options[:success_icon], error: options[:error_icon] }
    )
  end

  exit success ? 0 : 1
end