Module: Launchy

Defined in:
lib/launchy.rb,
lib/launchy/cli.rb,
lib/launchy/argv.rb,
lib/launchy/error.rb,
lib/launchy/detect.rb,
lib/launchy/runner.rb,
lib/launchy/version.rb,
lib/launchy/os_family.rb,
lib/launchy/application.rb,
lib/launchy/detect/host_os.rb,
lib/launchy/descendant_tracker.rb,
lib/launchy/applications/browser.rb,
lib/launchy/detect/host_os_family.rb,
lib/launchy/detect/nix_desktop_environment.rb

Overview

The entry point into Launchy. This is the sole supported public API.

Launchy.open( uri, options = {} )

The currently defined global options are:

:debug        Turn on debugging output
:application  Explicitly state what application class is going to be used.
              This must be a child class of Launchy::Application
:host_os      Explicitly state what host operating system to pretend to be
:dry_run      Do nothing and print the command that would be executed on $stdout

Other options may be used, and those will be passed directly to the application class

Defined Under Namespace

Modules: DescendantTracker, Detect, Version Classes: Application, ApplicationNotFoundError, ArgumentError, Argv, Cli, CommandNotFoundError, Error, OSFamily, Runner

Constant Summary collapse

VERSION =
"3.0.1"

Class Method Summary collapse

Class Method Details

.app_for_name(name) ⇒ Object



60
61
62
63
64
# File 'lib/launchy.rb', line 60

def app_for_name(name)
  Launchy::Application.for_name(name)
rescue Launchy::ApplicationNotFoundError
  nil
end

.app_for_uri(uri) ⇒ Object



56
57
58
# File 'lib/launchy.rb', line 56

def app_for_uri(uri)
  Launchy::Application.handling(uri)
end

.app_for_uri_string(str) ⇒ Object



66
67
68
# File 'lib/launchy.rb', line 66

def app_for_uri_string(str)
  app_for_uri(string_to_uri(str))
end

.applicationObject



113
114
115
# File 'lib/launchy.rb', line 113

def application
  @application || ENV.fetch("LAUNCHY_APPLICATION", nil)
end

.application=(app) ⇒ Object



109
110
111
# File 'lib/launchy.rb', line 109

def application=(app)
  @application = app
end

.bug_report_messageObject



133
134
135
# File 'lib/launchy.rb', line 133

def bug_report_message
  "Please rerun with environment variable LAUNCHY_DEBUG=true or the '-d' commandline option and file a bug at https://github.com/copiousfreetime/launchy/issues/new"
end

.debug=(enabled) ⇒ Object



99
100
101
# File 'lib/launchy.rb', line 99

def debug=(enabled)
  @debug = to_bool(enabled)
end

.debug?Boolean

we may do logging before a call to ‘open’, hence the need to check LAUNCHY_DEBUG here

Returns:

  • (Boolean)


105
106
107
# File 'lib/launchy.rb', line 105

def debug?
  @debug || to_bool(ENV.fetch("LAUNCHY_DEBUG", nil))
end

.dry_run=(dry_run) ⇒ Object



125
126
127
# File 'lib/launchy.rb', line 125

def dry_run=(dry_run)
  @dry_run = to_bool(dry_run)
end

.dry_run?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/launchy.rb', line 129

def dry_run?
  @dry_run || to_bool(ENV.fetch("LAUNCHY_DRY_RUN", nil))
end

.extract_global_options(options) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/launchy.rb', line 91

def extract_global_options(options)
  leftover = options.dup
  Launchy.debug        = leftover.delete(:debug) || ENV.fetch("LAUNCHY_DEBUG", nil)
  Launchy.application  = leftover.delete(:application) || ENV.fetch("LAUNCHY_APPLICATION", nil)
  Launchy.host_os      = leftover.delete(:host_os) || ENV.fetch("LAUNCHY_HOST_OS", nil)
  Launchy.dry_run      = leftover.delete(:dry_run) || ENV.fetch("LAUNCHY_DRY_RUN", nil)
end

.host_osObject



121
122
123
# File 'lib/launchy.rb', line 121

def host_os
  @host_os || ENV.fetch("LAUNCHY_HOST_OS", nil)
end

.host_os=(host_os) ⇒ Object



117
118
119
# File 'lib/launchy.rb', line 117

def host_os=(host_os)
  @host_os = host_os
end

.log(msg) ⇒ Object



137
138
139
# File 'lib/launchy.rb', line 137

def log(msg)
  $stderr.puts "LAUNCHY_DEBUG: #{msg}" if Launchy.debug?
end

.open(uri_s, options = {}) ⇒ Object

Launch an application for the given uri string



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
# File 'lib/launchy.rb', line 29

def open(uri_s, options = {})
  leftover = extract_global_options(options)
  uri = string_to_uri(uri_s)
  if (name = options[:application])
    app = app_for_name(name)
  end

  app = app_for_uri(uri) if app.nil?

  app.new.open(uri, leftover)
rescue Launchy::Error => e
  raise e
rescue StandardError => e
  msg = "Failure in opening uri #{uri_s.inspect} with options #{options.inspect}: #{e}"
  raise Launchy::Error, msg
ensure
  if $ERROR_INFO && block_given?
    yield $ERROR_INFO

    # explicitly return here to swallow the errors if there was an error
    # and we yielded to the block
    # rubocop:disable Lint/EnsureReturn
    return
    # rubocop:enable Lint/EnsureReturn
  end
end

.pathObject



141
142
143
# File 'lib/launchy.rb', line 141

def path
  @path
end

.path=(path) ⇒ Object



145
146
147
# File 'lib/launchy.rb', line 145

def path=(path)
  @path = path
end

.reset_global_optionsObject



83
84
85
86
87
88
89
# File 'lib/launchy.rb', line 83

def reset_global_options
  Launchy.debug       = false
  Launchy.application = nil
  Launchy.host_os     = nil
  Launchy.dry_run     = false
  Launchy.path        = ENV.fetch("PATH", nil)
end

.string_to_uri(str) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/launchy.rb', line 70

def string_to_uri(str)
  str = str.to_s
  uri = Addressable::URI.parse(str)
  Launchy.log "URI parsing pass 1 : #{str} -> #{uri.to_hash}"
  unless uri.scheme
    uri = Addressable::URI.heuristic_parse(str)
    Launchy.log "URI parsing pass 2 : #{str} -> #{uri.to_hash}"
  end
  raise Launchy::ArgumentError, "Invalid URI given: #{str.inspect}" unless uri

  uri
end