Class: Rooibos::Command::Open

Inherits:
Object
  • Object
show all
Includes:
Custom
Defined in:
lib/rooibos/command/open.rb

Overview

Opens a file or URL with the system’s default application.

Terminal applications often need to hand off to external programs. Opening a PDF, launching a URL, or viewing an image requires platform-specific commands.

This command detects the platform and runs the appropriate opener: open on macOS, xdg-open on Linux, start on Windows.

On success (exit 0), sends Message::Open. On failure (non-zero), sends Message::Error.

Prefer the Command.open factory method for convenience.

Example

# Using the factory method (recommended)
Command.open(model.selected_file)
Command.open("https://rooibos.run")

# Using the class directly
Open.new(path: model.selected_file, envelope: model.selected_file)

# Pattern-match on the response
def update(msg, model)
  case msg
  in { type: :open, envelope: path }
    model.with(status: "Opened #{path}")
  in { type: :error, envelope: path }
    model.with(error: "Could not open #{path}")
  end
end

Instance Method Summary collapse

Methods included from Custom

#deconstruct_keys, #rooibos_command?

Instance Method Details

#call(out, token) ⇒ Object

Executes the open command and sends the result message.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rooibos/command/open.rb', line 51

def call(out, token)
  return if token.canceled?

  require "open3"
  cmd = self.class.__send__(:system_command, path)
  _stdout, stderr, status = Open3.capture3(cmd)

  message = if status.exitstatus == 0
    Message::Open.new(envelope:)
  else
    error_msg = stderr.empty? ? "Failed to open: #{path}" : stderr.strip
    Message::Error.new(
      command: envelope,
      exception: RuntimeError.new(error_msg.freeze).freeze
    )
  end

  out.put(Ractor.make_shareable(message))
rescue => e
  out.put(Ractor.make_shareable(Message::Error.new(
    command: envelope,
    exception: RuntimeError.new(e.message.freeze).freeze
  )))
end

#rooibos_cancellation_grace_periodObject

System commands are generally fast; no grace period needed.



48
# File 'lib/rooibos/command/open.rb', line 48

def rooibos_cancellation_grace_period = 0