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) ⇒ 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(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) ⇒ 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(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) ⇒ 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(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) ⇒ 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(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