Skip to content

Commit

Permalink
Browser launching improvements
Browse files Browse the repository at this point in the history
Uses open(1) if on OS X, xdg-open(1) or cygstart(1) if either is available,
and prefers $BROWSER above all else.
  • Loading branch information
defunkt committed Apr 21, 2010
1 parent 07ae034 commit ad58368
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions lib/hub/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,7 @@ def browse(args)
exit(1)
end

args.executable = ENV['BROWSER'] || 'open'
args.push "#{protocol}://github.com/#{user}/#{repo}"
browser args, "#{protocol}://github.com/#{user}/#{repo}"
end

# $ hub compare 1.0...fix
Expand Down Expand Up @@ -257,8 +256,7 @@ def compare(args)
puts url
exit
else
args.executable = ENV['BROWSER'] || 'open'
args.push url
browser args, url
end
end

Expand Down Expand Up @@ -397,6 +395,40 @@ def improved_help_text
# from the command line.
#

# Checks whether a command exists on this system in the $PATH.
#
# name - The String name of the command to check for.
#
# Returns a Boolean.
def command?(name)
system "type #{name} > /dev/null 2>&1"
end

# Launches a given url in the user's browser, attempting to find
# one of a few known launcher commands before falling back to
# `$BROWSER`.

This comment has been minimized.

Copy link
@nogweii

nogweii Apr 21, 2010

It seems that the documentation & code mis-match?

The documenation says that hub tries finding the 'magic' commands first, and failing that, use $BROWSER.

The code checks $BROWSER first, then looks for the 'magic' commands.

Personally, I'd like to to try searching for open/xdg-open/cygstart first (rearranging the if-stack)

This comment has been minimized.

Copy link
@defunkt

defunkt Apr 21, 2010

Author Contributor

Whoops, fixed: d918ca2

#
# args - The args for this command. We modify them in place.
# url - The String URL to open in a browser.
#
# Returns nothing.
def browser(args, url)
if ENV['BROWSER']
cmd = ENV['BROWSER']
elsif RUBY_PLATFORM.include?('darwin')
cmd = "open"
elsif command?("xdg-open")
cmd = "xdg-open"
elsif command?("cygstart")
cmd = "cygstart"
else
abort "Please set $BROWSER to a web launcher to use this command."
end

args.executable = cmd
args.push url
end

# Either returns the GitHub user as set by git-config(1) or aborts
# with an error message.
def github_user
Expand Down

0 comments on commit ad58368

Please sign in to comment.