Sha256: cb4513489a86a6830cd609b9dcc2cfb2732490b7aa8507b5bc8899a81d98a9ff

Contents?: true

Size: 1.79 KB

Versions: 6

Compression:

Stored size: 1.79 KB

Contents

require 'command_kit/os'
require 'command_kit/env/path'

module CommandKit
  #
  # Allows opening a file or a URI with the system's preferred application for
  # that file type or URI scheme.
  #
  # ## Examples
  #
  #     open_app_for "movie.avi"
  #     open_app_for "https://github.com/postmodern/command_kit.rb#readme"
  #
  # @since 0.2.0
  #
  module OpenApp
    include OS
    include Env::Path

    #
    # Initializes the command and determines which open command to use.
    #
    # @param [Hash{Symbol => Object}] kwargs
    #   Additional keyword arguments.
    #
    # @api public
    #
    def initialize(**kwargs)
      super(**kwargs)

      @open_command = if macos?
                        'open'
                      elsif linux? || bsd?
                        if command_installed?('xdg-open')
                          'xdg-open'
                        end
                      elsif windows?
                        if command_installed?('invoke-item')
                          'invoke-item'
                        else
                          'start'
                        end
                      end
    end

    #
    # Opens a file or URI using the system's preferred application for that
    # file type or URI scheme.
    #
    # @param [String, URI] file_or_uri
    #   The file path or URI to open.
    #
    # @return [Boolean, nil]
    #   Specifies whether the file or URI was successfully opened or not.
    #   If the open command could not be determined, `nil` is returned.
    #
    # @example Open a file:
    #   open_app_for "movie.avi"
    #
    # @example Open a URI:
    #   open_app_for "https://github.com/postmodern/command_kit.rb"
    #
    def open_app_for(file_or_uri)
      if @open_command
        system(@open_command,file_or_uri.to_s)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
command_kit-0.5.0 lib/command_kit/open_app.rb
command_kit-0.4.1 lib/command_kit/open_app.rb
command_kit-0.4.0 lib/command_kit/open_app.rb
command_kit-0.3.0 lib/command_kit/open_app.rb
command_kit-0.2.2 lib/command_kit/open_app.rb
command_kit-0.2.1 lib/command_kit/open_app.rb