launchy 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,84 +2,97 @@ require 'launchy/application'
2
2
  require 'uri'
3
3
 
4
4
  module Launchy
5
- class Browser < Application
6
-
7
- DESKTOP_ENVIRONMENT_BROWSER_LAUNCHERS = {
8
- :kde => "kfmclient",
9
- :gnome => "gnome-open",
10
- :xfce => "exo-open",
11
- :generic => "htmlview"
5
+ class Browser < Application
6
+
7
+
8
+ class << self
9
+ def desktop_environment_browser_launchers
10
+ @desktop_environment_browser_launchers ||= {
11
+ :kde => "kfmclient",
12
+ :gnome => "gnome-open",
13
+ :xfce => "exo-open",
14
+ :generic => "htmlview"
12
15
  }
13
-
14
- FALLBACK_BROWSERS = %w[ firefox seamonkey opera mozilla netscape galeon ]
15
-
16
- class << self
17
- def run(*args)
18
- Browser.new.visit(args[0])
19
- end
20
-
21
- # return true if this class can handle the given parameter(s)
22
- def handle?(*args)
23
- begin
24
- Launchy.log "#{self.name} : testing if [#{args[0]}] (#{args[0].class}) is a url."
25
- uri = URI.parse(args[0])
26
- result = [URI::HTTP, URI::HTTPS, URI::FTP].include?(uri.class)
27
- rescue Exception => e
28
- # hmm... why does rcov not see that this is executed ?
29
- Launchy.log "#{self.name} : not a url, #{e}"
30
- return false
31
- end
32
- end
33
- end
34
-
35
- def initialize
36
- raise "Unable to find browser to launch for os family '#{my_os_family}'." unless browser
37
- end
38
-
39
- # Find a list of potential browser applications to run on *nix machines.
40
- # The order is:
41
- # 1) What is in ENV['LAUNCHY_BROWSER'] or ENV['BROWSER']
42
- # 2) xdg-open
43
- # 3) desktop environment launcher program
44
- # 4) a list of fallback browsers
45
- def nix_app_list
46
- if not @nix_app_list then
47
- browser_cmds = ['xdg-open']
48
- browser_cmds << DESKTOP_ENVIRONMENT_BROWSER_LAUNCHERS[nix_desktop_environment]
49
- browser_cmds << FALLBACK_BROWSERS
50
- browser_cmds.flatten!
51
- browser_cmds.delete_if { |b| b.nil? || (b.strip.size == 0) }
52
- Launchy.log "#{self.class.name} : Initial *Nix Browser List: #{browser_cmds.join(', ')}"
53
- @nix_app_list = browser_cmds.collect { |bin| find_executable(bin) }.find_all { |x| not x.nil? }
54
- Launchy.log "#{self.class.name} : Filtered *Nix Browser List: #{@nix_app_list.join(', ')}"
55
- end
56
- @nix_app_list
57
- end
58
-
59
- # return the full command line path to the browser or nil
60
- def browser
61
- if not @browser then
62
- if ENV['LAUNCHY_BROWSER'] and File.exists?(ENV['LAUNCHY_BROWSER']) then
63
- Launchy.log "#{self.class.name} : Using LAUNCHY_BROWSER environment variable : #{ENV['LAUNCHY_BROWSER']}"
64
- @browser = ENV['LAUNCHY_BROWSER']
65
- elsif ENV['BROWSER'] and File.exists?(ENV['BROWSER']) then
66
- Launchy.log "#{self.class.name} : Using BROWSER environment variable : #{ENV['BROWSER']}"
67
- @browser = ENV['BROWSER']
68
- elsif app_list.size > 0 then
69
- @browser = app_list.first
70
- Launchy.log "#{self.class.name} : Using application list : #{@browser}"
71
- else
72
- msg = "Unable to launch. No Browser application found."
73
- Launchy.log "#{self.class.name} : #{msg}"
74
- $stderr.puts msg
75
- end
76
- end
77
- return @browser
16
+ end
17
+ def fallback_browsers
18
+ @fallback_browsers ||= %w[ firefox seamonkey opera mozilla netscape galeon ]
19
+ end
20
+ def run(*args)
21
+ Browser.new.visit(args[0])
22
+ end
23
+
24
+ # return true if this class can handle the given parameter(s)
25
+ def handle?(*args)
26
+ begin
27
+ Launchy.log "#{self.name} : testing if [#{args[0]}] (#{args[0].class}) is a url."
28
+ uri = URI.parse(args[0])
29
+ result = [URI::HTTP, URI::HTTPS, URI::FTP].include?(uri.class)
30
+ rescue Exception => e
31
+ # hmm... why does rcov not see that this is executed ?
32
+ Launchy.log "#{self.name} : not a url, #{e}"
33
+ return false
78
34
  end
79
-
80
- # launch the browser at the appointed url
81
- def visit(url)
82
- run(browser,url)
35
+ end
36
+ end
37
+
38
+ def initialize
39
+ @browser = nil
40
+ @nix_app_list = nil
41
+ raise "Unable to find browser to launch for os family '#{my_os_family}'." unless browser
42
+ end
43
+
44
+ def desktop_environment_browser_launchers
45
+ self.class.desktop_environment_browser_launchers
46
+ end
47
+
48
+ def fallback_browsers
49
+ self.class.fallback_browsers
50
+ end
51
+
52
+ # Find a list of potential browser applications to run on *nix machines.
53
+ # The order is:
54
+ # 1) What is in ENV['LAUNCHY_BROWSER'] or ENV['BROWSER']
55
+ # 2) xdg-open
56
+ # 3) desktop environment launcher program
57
+ # 4) a list of fallback browsers
58
+ def nix_app_list
59
+ if not @nix_app_list then
60
+ browser_cmds = ['xdg-open']
61
+ browser_cmds << desktop_environment_browser_launchers[nix_desktop_environment]
62
+ browser_cmds << fallback_browsers
63
+ browser_cmds.flatten!
64
+ browser_cmds.delete_if { |b| b.nil? || (b.strip.size == 0) }
65
+ Launchy.log "#{self.class.name} : Initial *Nix Browser List: #{browser_cmds.join(', ')}"
66
+ @nix_app_list = browser_cmds.collect { |bin| find_executable(bin) }.find_all { |x| not x.nil? }
67
+ Launchy.log "#{self.class.name} : Filtered *Nix Browser List: #{@nix_app_list.join(', ')}"
68
+ end
69
+ @nix_app_list
70
+ end
71
+
72
+ # return the full command line path to the browser or nil
73
+ def browser
74
+ if not @browser then
75
+ if ENV['LAUNCHY_BROWSER'] and File.exists?(ENV['LAUNCHY_BROWSER']) then
76
+ Launchy.log "#{self.class.name} : Using LAUNCHY_BROWSER environment variable : #{ENV['LAUNCHY_BROWSER']}"
77
+ @browser = ENV['LAUNCHY_BROWSER']
78
+ elsif ENV['BROWSER'] and File.exists?(ENV['BROWSER']) then
79
+ Launchy.log "#{self.class.name} : Using BROWSER environment variable : #{ENV['BROWSER']}"
80
+ @browser = ENV['BROWSER']
81
+ elsif app_list.size > 0 then
82
+ @browser = app_list.first
83
+ Launchy.log "#{self.class.name} : Using application list : #{@browser}"
84
+ else
85
+ msg = "Unable to launch. No Browser application found."
86
+ Launchy.log "#{self.class.name} : #{msg}"
87
+ $stderr.puts msg
83
88
  end
89
+ end
90
+ return @browser
91
+ end
92
+
93
+ # launch the browser at the appointed url
94
+ def visit(url)
95
+ run(browser,url)
84
96
  end
97
+ end
85
98
  end
@@ -1,48 +1,48 @@
1
1
  require 'optparse'
2
2
 
3
3
  module Launchy
4
- class CommandLine
4
+ class CommandLine
5
5
 
6
- def parser
7
- @parser ||= OptionParser.new do |op|
8
- op.banner = "Usage: launchy [options] url"
9
- op.separator ""
10
- op.on("-d", "--debug", "Force debug, output lots of information.",
11
- "This sets the LAUNCHY_DEBUG environment variable to 'true'.") do |d|
12
- ENV["LAUNCHY_DEBUG"] = 'true'
13
- end
14
-
15
- op.on("-h", "--help", "Print this message") do |h|
16
- puts op.to_s
17
- exit 0
18
- end
6
+ def parser
7
+ @parser ||= OptionParser.new do |op|
8
+ op.banner = "Usage: launchy [options] url"
9
+ op.separator ""
10
+ op.on("-d", "--debug", "Force debug, output lots of information.",
11
+ "This sets the LAUNCHY_DEBUG environment variable to 'true'.") do |d|
12
+ ENV["LAUNCHY_DEBUG"] = 'true'
13
+ end
14
+
15
+ op.on("-h", "--help", "Print this message") do |h|
16
+ puts op.to_s
17
+ exit 0
18
+ end
19
+
20
+ op.on("-v", "--version", "Output the version of Launchy") do |v|
21
+ puts "Launchy version #{Launchy::VERSION}"
22
+ exit 0
23
+ end
19
24
 
20
- op.on("-v", "--version", "Output the version of Launchy") do |v|
21
- puts "Launchy version #{Launchy::VERSION}"
22
- exit 0
23
- end
24
-
25
- op.on("-o", "--host-os HOST_OS","Force the behavior of a particular host os.",
25
+ op.on("-o", "--host-os HOST_OS","Force the behavior of a particular host os.",
26
26
  "This sets the LAUNCHY_HOST_OS environment variable.") do |os|
27
- ENV["LAUNCHY_HOST_OS"] = os
28
- end
29
-
30
- op.on("-b", "--browser BROWSER", "Force launchy to use a particular browser.",
31
- "This sets the LAUNCHY_BROWSER environment variable.") do |browser|
32
- ENV["LAUNCHY_BROWSER"] = browser
33
- end
34
- end
27
+ ENV["LAUNCHY_HOST_OS"] = os
35
28
  end
36
-
37
- def run(argv = ARGV)
38
- begin
39
- parser.parse!(argv)
40
- Launchy.open(*argv)
41
- rescue ::OptionParser::ParseError => pe
42
- $stderr.puts "#{parser.programn_name}: #{pe}"
43
- $stderr.puts "Try `#{parser.program_name} --help' for more information."
44
- exit 1
45
- end
29
+
30
+ op.on("-b", "--browser BROWSER", "Force launchy to use a particular browser.",
31
+ "This sets the LAUNCHY_BROWSER environment variable.") do |browser|
32
+ ENV["LAUNCHY_BROWSER"] = browser
46
33
  end
34
+ end
35
+ end
36
+
37
+ def run(argv = ARGV)
38
+ begin
39
+ parser.parse!(argv)
40
+ Launchy.open(*argv)
41
+ rescue ::OptionParser::ParseError => pe
42
+ $stderr.puts "#{parser.programn_name}: #{pe}"
43
+ $stderr.puts "Try `#{parser.program_name} --help' for more information."
44
+ exit 1
45
+ end
47
46
  end
47
+ end
48
48
  end
@@ -0,0 +1,53 @@
1
+ #--
2
+ # Copyright (c) 2007 Jeremy Hinegardner
3
+ # All rights reserved. See LICENSE and/or COPYING for details.
4
+ #++
5
+ #
6
+ module Launchy
7
+ #
8
+ # Access to various paths inside the project programatically
9
+ #
10
+ module Paths
11
+ #
12
+ # :call-seq:
13
+ # Launchy::Paths.root_dir -> String
14
+ #
15
+ # Returns The full expanded path of the parent directory of +lib+
16
+ # going up the path from the current file. A trailing File::SEPARATOR⋅
17
+ # is guaranteed
18
+ #
19
+ def self.root_dir
20
+ @root_dir ||=(
21
+ path_parts = ::File.expand_path( __FILE__ ).split( ::File::SEPARATOR )
22
+ lib_index = path_parts.rindex( "lib" )
23
+ @root_dir = path_parts[0...lib_index].join( ::File::SEPARATOR ) + ::File::SEPARATOR
24
+ )
25
+ end
26
+
27
+ #
28
+ # :call-seq:
29
+ # Launchy::Paths.lib_path( *args ) -> String
30
+ #
31
+ # Returns The full expanded path of the +lib+ directory below
32
+ # _root_dir_. All parameters passed in are joined onto the⋅
33
+ # result. A trailing File::SEPARATOR is guaranteed if⋅
34
+ # _args_ are *not* present.
35
+ #
36
+ def self.lib_path( *args )
37
+ self.sub_path( "lib", *args )
38
+ end
39
+
40
+ #
41
+ # :call-seq:
42
+ # Launchy::Paths.sub_path( sub, *args ) -> String
43
+ #
44
+ # Returns the full expanded path of the +sub+ directory below _root_dir. All
45
+ # _arg_ parameters passed in are joined onto the result. A trailing
46
+ # File::SEPARATOR is guaranteed if _args_ are *not* present.
47
+ #
48
+ def self.sub_path( sub, *args )
49
+ sp = ::File.join( root_dir, sub ) + File::SEPARATOR
50
+ sp = ::File.join( sp, *args ) if args
51
+ end
52
+ end
53
+ end
@@ -1,18 +1,17 @@
1
1
  module Launchy
2
- class Version
3
- MAJOR = 0
4
- MINOR = 3
5
- BUILD = 2
2
+ module Version
3
+ MAJOR = 0
4
+ MINOR = 3
5
+ BUILD = 3
6
6
 
7
- class << self
8
- def to_a
9
- [MAJOR, MINOR, BUILD]
10
- end
7
+ def self.to_a
8
+ [MAJOR, MINOR, BUILD]
9
+ end
11
10
 
12
- def to_s
13
- to_a.join(".")
14
- end
15
- end
11
+ def self.to_s
12
+ to_a.join(".")
16
13
  end
17
- VERSION = Version.to_s
14
+ STRING = Version.to_s.freeze
15
+ end
16
+ VERSION = Version.to_s.freeze
18
17
  end
@@ -2,58 +2,57 @@ require File.join(File.dirname(__FILE__),"spec_helper.rb")
2
2
  require 'yaml'
3
3
 
4
4
  describe Launchy::Application do
5
- before(:each) do
6
- yml = YAML::load(IO.read(File.join(File.dirname(__FILE__),"tattle-host-os.yml")))
7
- @host_os = yml['host_os']
8
- @app = Launchy::Application.new
9
- end
5
+ before(:each) do
6
+ yml = YAML::load(IO.read(File.join(File.dirname(__FILE__),"tattle-host-os.yml")))
7
+ @host_os = yml['host_os']
8
+ @app = Launchy::Application.new
9
+ end
10
10
 
11
- YAML::load(IO.read(File.join(File.dirname(__FILE__), "tattle-host-os.yml")))['host_os'].keys.sort.each do |os|
12
- it "#{os} should be a found os" do
13
- Launchy::Application::KNOWN_OS_FAMILIES.should include(@app.my_os_family(os))
14
- end
15
- end
16
-
17
- it "should not find os of 'dos'" do
18
- @app.my_os_family('dos').should == :unknown
19
- end
20
-
21
- it "my os should have a value" do
22
- @app.my_os.should_not == ''
23
- @app.my_os.should_not == nil
24
- end
25
-
26
- it "should find open" do
27
- @app.find_executable('open').should == "/usr/bin/open"
28
- end
29
-
30
- it "should not find app xyzzy" do
31
- @app.find_executable('xyzzy').should == nil
32
- end
33
-
34
- it "should find the correct class to launch an ftp url" do
35
- Launchy::Application.find_application_class_for("ftp://ftp.ruby-lang.org/pub/ruby/").should == Launchy::Browser
36
- end
37
-
38
- it "knows when it cannot find an application class" do
39
- Launchy::Application.find_application_class_for("xyzzy:stuff,things").should == nil
40
- end
41
-
42
- it "allows for environmental override of host_os" do
43
- ENV["LAUNCHY_HOST_OS"] = "hal-9000"
44
- Launchy::Application.my_os.should == "hal-9000"
45
- ENV["LAUNCHY_HOST_OS"] = nil
46
- end
47
-
48
- it "can detect the desktop environment of a *nix machien" do
49
- @app.nix_desktop_environment.should == :generic
50
-
51
- { "KDE_FULL_SESSION" => :kde,
52
- "KDE_SESSION_UID" => :kde,
53
- "GNOME_DESKTOP_SESSION_ID" => :gnome }.each_pair do |k,v|
54
- ENV[k] = "launchy-test"
55
- Launchy::Application.new.nix_desktop_environment.should == v
56
- ENV[k] = nil
57
- end
11
+ YAML::load(IO.read(File.join(File.dirname(__FILE__), "tattle-host-os.yml")))['host_os'].keys.sort.each do |os|
12
+ it "#{os} should be a found os" do
13
+ Launchy::Application::known_os_families.should include(@app.my_os_family(os))
58
14
  end
15
+ end
16
+
17
+ it "should not find os of 'dos'" do
18
+ @app.my_os_family('dos').should eql(:unknown)
19
+ end
20
+
21
+ it "my os should have a value" do
22
+ @app.my_os.should_not eql('')
23
+ @app.my_os.should_not eql(nil)
24
+ end
25
+
26
+ it "should find open" do
27
+ @app.find_executable('open').should eql("/usr/bin/open")
28
+ end
29
+
30
+ it "should not find app xyzzy" do
31
+ @app.find_executable('xyzzy').should eql(nil)
32
+ end
33
+
34
+ it "should find the correct class to launch an ftp url" do
35
+ Launchy::Application.find_application_class_for("ftp://ftp.ruby-lang.org/pub/ruby/").should == Launchy::Browser
36
+ end
37
+
38
+ it "knows when it cannot find an application class" do
39
+ Launchy::Application.find_application_class_for("xyzzy:stuff,things").should == nil
40
+ end
41
+
42
+ it "allows for environmental override of host_os" do
43
+ ENV["LAUNCHY_HOST_OS"] = "hal-9000"
44
+ Launchy::Application.my_os.should eql("hal-9000")
45
+ ENV["LAUNCHY_HOST_OS"] = nil
46
+ end
47
+
48
+ { "KDE_FULL_SESSION" => :kde,
49
+ "KDE_SESSION_UID" => :kde,
50
+ "GNOME_DESKTOP_SESSION_ID" => :gnome }.each_pair do |k,v|
51
+ it "can detect the desktop environment of a *nix machine using #{k}" do
52
+ @app.nix_desktop_environment.should eql(:generic)
53
+ ENV[k] = "launchy-test"
54
+ Launchy::Application.new.nix_desktop_environment.should eql(v)
55
+ ENV[k] = nil
56
+ end
57
+ end
59
58
  end