Module: WormCLI
- Defined in:
- lib/ruby-progress/cli/worm_cli.rb,
lib/ruby-progress/cli/worm_options.rb
Overview
Enhanced Worm CLI (extracted from bin/prg)
Defined Under Namespace
Modules: Options
Class Method Summary collapse
-
.resolve_pid_file(options, name_key = :daemon_name) ⇒ String
Determine the pid file path from options.
-
.run ⇒ void
Entrypoint for the Worm CLI.
-
.run_daemon_mode(options) ⇒ void
Launch the worm indicator in daemon mode writing a pid file and monitoring for control messages.
Class Method Details
.resolve_pid_file(options, name_key = :daemon_name) ⇒ String
Determine the pid file path from options. If options specify a custom :pid_file, return it. If a named daemon key is present, use /tmp/ruby-progress/<name>.pid. Otherwise fall back to the default.
28 29 30 31 32 33 34 |
# File 'lib/ruby-progress/cli/worm_cli.rb', line 28 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 ⇒ void
This method returns an undefined value.
Entrypoint for the Worm CLI. Parses options and dispatches to status, stop, daemon, or runtime modes. Ensures the cursor is restored on Ctrl+C.
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 |
# File 'lib/ruby-progress/cli/worm_cli.rb', line 40 def self.run trap('INT') do RubyProgress::Utils.show_cursor exit end = WormCLI::Options. 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 worm remains visible in current terminal PrgCLI.backgroundize run_daemon_mode() else progress = RubyProgress::Worm.new() if [:command] progress.run_with_command else progress.run_indefinitely end end end |
.run_daemon_mode(options) ⇒ void
This method returns an undefined value.
Launch the worm indicator in daemon mode writing a pid file and monitoring for control messages. Ensures pid file is removed on exit.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/ruby-progress/cli/worm_cli.rb', line 84 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) progress = RubyProgress::Worm.new() begin progress.run_daemon_mode( success_message: [:success], show_checkmark: [:checkmark], control_message_file: RubyProgress::Daemon.(pid_file), icons: { success: [:success_icon], error: [:error_icon] } ) ensure FileUtils.rm_f(pid_file) end end |