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
- .resolve_pid_file(options, name_key = :daemon_name) ⇒ Object
- .run ⇒ Object
- .run_daemon_mode(text, options) ⇒ Object
- .run_indefinitely(text, options) ⇒ Object
- .run_with_command(text, options) ⇒ Object
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(, name_key = :daemon_name) return [:pid_file] if [:pid_file] return "/tmp/ruby-progress/#{[name_key]}.pid" if [name_key] RubyProgress::Daemon.default_pid_file end |
.run ⇒ Object
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 = RippleCLI::Options. # Daemon/status/stop handling (process these without requiring text) if [:status] pid_file = resolve_pid_file(, :status_name) RubyProgress::Daemon.show_status(pid_file) exit elsif [:stop] pid_file = resolve_pid_file(, :stop_name) stop_msg = [:stop_error] || [:stop_success] is_error = ![:stop_error].nil? RubyProgress::Daemon.stop_daemon_by_pid_file( pid_file, message: stop_msg, checkmark: [:stop_checkmark], error: is_error ) exit elsif [:daemon] # Background without detaching so ripple remains visible in current terminal PrgCLI.backgroundize # For daemon mode, default message if none provided text = [:message] || ARGV.join(' ') text = 'Processing' if text.nil? || text.empty? run_daemon_mode(text, ) else # Non-daemon path requires text text = [: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 [:rainbow] = [:styles].include?(:rainbow) [:inverse] = [:styles].include?(:inverse) [:caps] = [:styles].include?(:caps) if [:command] run_with_command(text, ) else run_indefinitely(text, ) 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, ) pid_file = resolve_pid_file(, :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, ) 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.(pid_file) if File.exist?(cmf) begin data = JSON.parse(File.read(cmf)) = 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 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 # 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, ) rippler = RubyProgress::Ripple.new(text, ) RubyProgress::Utils.hide_cursor begin loop { rippler.advance } ensure RubyProgress::Utils.show_cursor RubyProgress::Ripple.complete(text, [:success_message], [: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, ) 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: [:command], lines: [:output_lines] || 3, position: [:output_position] || :above, stream: [:output] == :stdout || [: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, ) rippler.instance_variable_set(:@output_capture, oc) if [: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 = `#{[:command]} 2>&1` success = $CHILD_STATUS.success? end puts captured_output if [:output] == :stdout if [:success_message] || [:complete_checkmark] = success ? [:success_message] : [:fail_message] || [:success_message] RubyProgress::Ripple.complete( text, , [:complete_checkmark], success, icons: { success: [:success_icon], error: [:error_icon] } ) end exit success ? 0 : 1 end |