Module: RippleCLI

Defined in:
lib/ruby-progress/cli/ripple_cli.rb,
lib/ruby-progress/cli/ripple_options.rb

Overview

Enhanced Ripple CLI with unified flags (extracted from bin/prg)

Defined Under Namespace

Modules: Options

Class Method Summary collapse

Class Method Details

.resolve_pid_file(options, name_key = :daemon_name) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/ruby-progress/cli/ripple_cli.rb', line 11

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

  return "/tmp/ruby-progress/#{options[name_key]}.pid" if options[name_key]

  RubyProgress::Daemon.default_pid_file
end

.runObject



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
# File 'lib/ruby-progress/cli/ripple_cli.rb', line 19

def self.run
  trap('INT') do
    RubyProgress::Utils.show_cursor
    exit
  end

  options = RippleCLI::Options.parse_cli_options

  # Daemon/status/stop handling (process these without requiring text)
  if options[:status]
    pid_file = resolve_pid_file(options, :status_name)
    RubyProgress::Daemon.show_status(pid_file)
    exit
  elsif options[:stop]
    pid_file = resolve_pid_file(options, :stop_name)
    stop_msg = options[:stop_error] || options[:stop_success]
    is_error = !options[:stop_error].nil?
    RubyProgress::Daemon.stop_daemon_by_pid_file(
      pid_file,
      message: stop_msg,
      checkmark: options[:stop_checkmark],
      error: is_error
    )
    exit
  elsif options[:daemon]
    # Background without detaching so ripple remains visible in current terminal
    PrgCLI.backgroundize

    # For daemon mode, default message if none provided
    text = options[:message] || ARGV.join(' ')
    text = 'Processing' if text.nil? || text.empty?
    run_daemon_mode(text, options)
  else
    # Non-daemon path requires text
    text = options[:message] || ARGV.join(' ')
    if text.empty?
      puts 'Error: Please provide text to animate via argument or --message flag'
      puts "Example: prg ripple 'Loading...' or prg ripple --message 'Loading...'"
      exit 1
    end

    # Convert styles array to individual flags for backward compatibility
    options[:rainbow] = options[:styles].include?(:rainbow)
    options[:inverse] = options[:styles].include?(:inverse)
    options[:caps] = options[:styles].include?(:caps)

    if options[:command]
      run_with_command(text, options)
    else
      run_indefinitely(text, options)
    end
  end
end

.run_daemon_mode(text, options) ⇒ Object



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
180
181
182
183
184
185
186
187
188
189
# File 'lib/ruby-progress/cli/ripple_cli.rb', line 130

def self.run_daemon_mode(text, options)
  pid_file = resolve_pid_file(options, :daemon_name)
  FileUtils.mkdir_p(File.dirname(pid_file))
  File.write(pid_file, Process.pid.to_s)
  begin
    # For Ripple, re-use the existing animation loop via a simple loop
    RubyProgress::Utils.hide_cursor
    rippler = RubyProgress::Ripple.new(text, 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 }

    rippler.advance until stop_requested
  ensure
    RubyProgress::Utils.clear_line
    RubyProgress::Utils.show_cursor

    # If a control message file exists, output its message with optional checkmark
    cmf = RubyProgress::Daemon.control_message_file(pid_file)
    if File.exist?(cmf)
      begin
        data = JSON.parse(File.read(cmf))
        message = data['message']
        check = if data.key?('checkmark')
                  data['checkmark'] ? true : false
                else
                  false
                end
        success_val = if data.key?('success')
                        data['success'] ? true : false
                      else
                        true
                      end
        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

    # stop job thread and cleanup
    FileUtils.rm_f(pid_file)
  end
end

.run_indefinitely(text, options) ⇒ Object



119
120
121
122
123
124
125
126
127
128
# File 'lib/ruby-progress/cli/ripple_cli.rb', line 119

def self.run_indefinitely(text, options)
  rippler = RubyProgress::Ripple.new(text, options)
  RubyProgress::Utils.hide_cursor
  begin
    loop { rippler.advance }
  ensure
    RubyProgress::Utils.show_cursor
    RubyProgress::Ripple.complete(text, options[:success_message], options[:complete_checkmark], true)
  end
end

.run_with_command(text, options) ⇒ Object



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
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/ruby-progress/cli/ripple_cli.rb', line 73

def self.run_with_command(text, options)
  if $stdout.tty?
    # Interactive TTY: use PTY-based capture so the animation can run while the
    # command executes. We only print captured stdout if options[:output] == :stdout.
    oc = RubyProgress::OutputCapture.new(
      command: options[:command],
      lines: options[:output_lines] || 3,
      position: options[:output_position] || :above,
      stream: options[:output] == :stdout || options[:stdout_live]
    )
    oc.start

    # Create rippler. Attach output capture only when the user requested
    # live stdout display via --stdout; otherwise start the PTY reader so
    # we can collect the child's exit status but do not call redraw.
    rippler = RubyProgress::Ripple.new(text, options)
    rippler.instance_variable_set(:@output_capture, oc) if options[:output] == :stdout

    thread = Thread.new { loop { rippler.advance } }
    oc.wait
    thread.kill

    captured_lines = oc.lines
    captured_output = captured_lines.join("\n")
    success = oc.exit_status.nil? || oc.exit_status.zero?
  else
    # Non-interactive / CI: fallback to legacy synchronous capture
    captured_output = `#{options[:command]} 2>&1`
    success = $CHILD_STATUS.success?
  end

  puts captured_output if options[:output] == :stdout

  if options[:success_message] || options[:complete_checkmark]
    message = success ? options[:success_message] : options[:fail_message] || options[:success_message]
    RubyProgress::Ripple.complete(
      text,
      message,
      options[:complete_checkmark],
      success,
      icons: { success: options[:success_icon], error: options[:error_icon] }
    )
  end
  exit success ? 0 : 1
end