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

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/.pid. Otherwise fall back to the default.

Parameters:

  • options (Hash) —

    parsed CLI options

  • name_key (Symbol) (defaults to: :daemon_name) —

    key used for named daemons (default :daemon_name)

Returns:

  • (String) —

    path to the pid file



28
29
30
31
32
33
34
# File 'lib/ruby-progress/cli/worm_cli.rb', line 28

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

.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

  options = WormCLI::Options.parse_cli_options

  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 worm remains visible in current terminal
    PrgCLI.backgroundize

    run_daemon_mode(options)
  else
    progress = RubyProgress::Worm.new(options)

    if options[: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.

Parameters:

  • options (Hash) —

    parsed CLI options used to configure the Worm instance



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

  progress = RubyProgress::Worm.new(options)

  begin
    progress.run_daemon_mode(
      success_message: options[:success],
      show_checkmark: options[:checkmark],
      control_message_file: RubyProgress::Daemon.control_message_file(pid_file),
      icons: { success: options[:success_icon], error: options[:error_icon] }
    )
  ensure
    FileUtils.rm_f(pid_file)
  end
end