File: browser.rb

package info (click to toggle)
ruby-launchy 2.5.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 292 kB
  • sloc: ruby: 1,285; makefile: 6
file content (82 lines) | stat: -rw-r--r-- 2,292 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
class Launchy::Application
  #
  # The class handling the browser application and all of its schemes
  #
  class Browser < Launchy::Application
    def self.schemes
      %w[ http https ftp file ]
    end

    def self.handles?( uri )
      return true if schemes.include?( uri.scheme )
      return true if File.exist?( uri.path )
    end

    def windows_app_list
      [ 'start "launchy" /b' ]
    end

    def cygwin_app_list
      [ 'cmd /C start "launchy" /b' ]
    end

    # hardcode this to open?
    def darwin_app_list
      [ find_executable( "open" ) ]
    end

    def nix_app_list
      nix_de = Launchy::Detect::NixDesktopEnvironment.detect
      list   = nix_de.browsers
      list.find_all { |argv| argv.valid? }
    end

    # use a call back mechanism to get the right app_list that is decided by the 
    # host_os_family class.
    def app_list
      host_os_family.app_list( self )
    end

    def browser_env
      return [] unless ENV['BROWSER']
      browser_env = ENV['BROWSER'].split( File::PATH_SEPARATOR )
      browser_env.flatten!
      browser_env.delete_if { |b| b.nil? || (b.strip.size == 0) }
      return browser_env
    end

    # Get the full commandline of what we are going to add the uri to
    def browser_cmdline
      browser_env.each do |p|
        Launchy.log "#{self.class.name} : possibility from BROWSER environment variable : #{p}"
      end
      app_list.each do |p|
        Launchy.log "#{self.class.name} : possibility from app_list : #{p}"
      end

      possibilities = (browser_env + app_list).flatten

      if browser = possibilities.shift then
        Launchy.log "#{self.class.name} : Using browser value '#{browser}'"
        return browser
      end
      raise Launchy::CommandNotFoundError, "Unable to find a browser command. If this is unexpected, #{Launchy.bug_report_message}"
    end

    def cmd_and_args( uri, options = {} )
      cmd = browser_cmdline.to_s
      args = [ uri.to_s ]
      if cmd =~ /%s/ then
        cmd.gsub!( /%s/, args.shift )
      end
      [cmd, args]
    end

    # final assembly of the command and do %s substitution 
    # http://www.catb.org/~esr/BROWSER/index.html
    def open( uri, options = {} )
      cmd, args = cmd_and_args( uri, options )
      run( cmd, args )
    end
  end
end