Skip to content

Commit

Permalink
Growl#notify now normalizes the :icon option key [#2]
Browse files Browse the repository at this point in the history
View the readme for more information
  • Loading branch information
tj committed Apr 13, 2009
1 parent 03fc668 commit 7048d19
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
11 changes: 11 additions & 0 deletions README.rdoc
Expand Up @@ -43,6 +43,17 @@ Ruby growlnotify 'bindings'.
notify_warning 'Merge required'
notify_error 'Failed to send email', :sticky => true

== Normaized Icons

The :icon option key is automatically normalized when you use any of the Growl#notify
methods, this removes the need to explicitly use :appIcon, :image, etc.

notify 'App icon', :icon => :Safari
notify 'Jpeg icon', :icon => :jpeg
notify 'Image icon', :icon => 'path/to/image.png'
notify 'Icns icon', :icon => 'path/to/icon.icns'
notify 'Path extname icon', :icon => 'foo.rb'

== Features

* Check availability with Growl.installed?
Expand Down
17 changes: 11 additions & 6 deletions examples/growl.rb
Expand Up @@ -22,9 +22,14 @@

include Growl

notify 'Whoop', :appIcon => 'Safari' ; sleep 0.2
notify 'Image processing complete', :icon => :jpeg ; sleep 0.2
notify 'Kicks ass!', :title => 'Growl', :iconpath => iconpath ; sleep 0.2
notify_info 'New email received' ; sleep 0.2
notify_ok 'Deployment complete' ; sleep 0.2
notify_error 'Deployment failure' ; sleep 0.2
# notify 'Image processing complete', :icon => :jpeg ; sleep 0.2
# notify 'Kicks ass!', :title => 'Growl', :iconpath => iconpath ; sleep 0.2
# notify_info 'New email received' ; sleep 0.2
# notify_ok 'Deployment complete' ; sleep 0.2
# notify_error 'Deployment failure' ; sleep 0.2

notify 'Safari icon', :icon => :Safari ; sleep 0.2
notify 'Jpeg icon', :icon => :jpeg ; sleep 0.2
notify 'Image icon', :icon => imagepath ; sleep 0.2
notify 'Icns icon', :icon => iconpath ; sleep 0.2
notify 'Path extname icon', :icon => 'foo.rb' ; sleep 0.2
32 changes: 32 additions & 0 deletions lib/growl/growl.rb
Expand Up @@ -30,6 +30,7 @@ class Error < StandardError; end
def notify message = nil, options = {}, &block
return unless Growl.installed?
options.merge! :message => message if message
Growl.normalize_icon! options
Growl.new(options, &block).run
end
module_function :notify
Expand Down Expand Up @@ -76,6 +77,37 @@ def self.new *args, &block
Base.new *args, &block
end

##
# Normalize the icon option in +options+. This performs
# the following operations in order to allow for the :icon
# key to work with a variety of values:
#
# * path to an icon sets :iconpath
# * path to an image sets :image
# * capitalized word sets :appIcon
# * filename uses extname as :icon
# * otherwise treated as :icon

def self.normalize_icon! options = {}
return unless options.include? :icon
icon = options.delete(:icon).to_s
if File.exists? icon
if File.extname(icon) == '.icns'
options[:iconpath] = icon
else
options[:image] = icon
end
else
if icon.capitalize == icon
options[:appIcon] = icon
elsif !(ext = File.extname(icon)).empty?
options[:icon] = ext[1..-1]
else
options[:icon] = icon
end
end
end

#--
# Growl base
#++
Expand Down

0 comments on commit 7048d19

Please sign in to comment.