Skip to content

Loading…

Configurable path to growlnotify bin #2

Merged
merged 2 commits into from

2 participants

@lukaszkorecki
Collaborator

Hi

I've added the ability to configure the path to growlnotify exec so that it can be bundled with the application using growl lib.

Example can be found in the README

@tj
Owner
tj commented

sounds good. i dont maintain my ruby stuff anymore, i can add you as a contributor if you want

@lukaszkorecki
Collaborator

Oh cool, sounds great.

@tj
Owner
tj commented

done :)

@tj tj merged commit a91899b into tj:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Showing with 71 additions and 42 deletions.
  1. +14 −6 README.rdoc
  2. +6 −1 examples/growl.rb
  3. +51 −35 lib/growl/growl.rb
View
20 README.rdoc
@@ -43,30 +43,38 @@ Ruby growlnotify 'bindings'.
notify_warning 'Merge required'
notify_error 'Failed to send email', :sticky => true
+== Custom path to growlnotify
+
+If needed you can set a path to the binary with:
+
+ Growl.bin_path = '~/my/path/to/growlnotify'
+
+right after you require the growl library.
+
== 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 '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'
+ notify 'Path extname icon', :icon => 'foo.rb'
== Features
* Check availability with Growl.installed?
* Check dependencies with Growl.version
* Use images, icon paths, application icons, or file format icons
-* Sticky a notification making it appear until explicitly closed
-* Set notification priority
+* Sticky a notification making it appear until explicitly closed
+* Set notification priority
* Convenience methods such as notify_ok, notify_warning, notify_error, and notify_info
* Etc (consult growlnotify --help)
== Installing growlnotify
-Visit http://growl.info/documentation.php for installation help.
+Visit http://growl.info/documentation.php for installation help.
== Known Issues
View
7 examples/growl.rb
@@ -2,6 +2,10 @@
$:.unshift File.dirname(__FILE__) + '/../lib'
require 'growl'
+Growl.bin_path = ARGV[0] unless ARGV.empty?
+
+puts "using growlnotify located at #{Growl.bin_path}" unless ARGV.empty?
+
puts "growlnotify version #{Growl.version} installed"
imagepath = File.dirname(__FILE__) + '/../spec/fixtures/image.png'
@@ -9,6 +13,7 @@
include Growl
+
notify_info 'New email received', :sticky => true, :title => 'Some app'; sleep 0.2
notify_ok 'Deployment complete' ; sleep 0.2
notify_error 'Deployment failure' ; sleep 0.2
@@ -16,4 +21,4 @@
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
+notify 'Path extname icon', :icon => 'foo.rb' ; sleep 0.2
View
86 lib/growl/growl.rb
@@ -1,16 +1,16 @@
module Growl
-
- BIN = 'growlnotify'
-
+
+ @@path = 'growlnotify'
+
#--
# Exceptions
#++
-
+
class Error < StandardError; end
-
+
##
- # Display a growl notification +message+, with +options+
+ # Display a growl notification +message+, with +options+
# documented below. Alternatively a +block+ may be passed
# which is then instance evaluated or yielded to the block.
#
@@ -20,13 +20,13 @@ class Error < StandardError; end
# and your user.
#
# === Examples
- #
+ #
# Growl.notify 'Hello'
# Growl.notify 'Hello', :title => 'TJ Says:', :sticky => true
# Growl.notify { |n| n.message = 'Hello'; n.sticky! }
# Growl.notify { self.message = 'Hello'; sticky! }
#
-
+
def notify message = nil, options = {}, &block
return unless Growl.installed?
options.merge! :message => message if message
@@ -34,11 +34,11 @@ def notify message = nil, options = {}, &block
Growl.new(options, &block).run
end
module_function :notify
-
+
#--
# Generate notify_STATUS methods.
#++
-
+
%w( ok info warning error ).each do |type|
define_method :"notify_#{type}" do |message, *args|
options = args.first || {}
@@ -47,36 +47,52 @@ def notify message = nil, options = {}, &block
end
module_function :"notify_#{type}"
end
-
+
##
# Execute +args+ against the binary.
-
+
def self.exec *args
- Kernel.system BIN, *args
+ Kernel.system @@path, *args
end
-
+
+ ##
+ # Specify +path+ to the growlnotify binary
+ # useful if you're bundling it with your application
+ # and don't want to force your user to install
+ # growlnotify package
+
+ def bin_path= path
+ @@path = File.expand_path path
+ end
+ module_function :bin_path=
+
+ def bin_path
+ @@path
+ end
+ module_function :bin_path
+
##
# Return the version triple of the binary.
-
+
def self.version
- @version ||= `#{BIN} --version`.split[1]
+ @version ||= `#{@@path} --version`.split[1]
end
-
+
##
# Check if the binary is installed and accessable.
-
+
def self.installed?
version rescue false
end
-
+
##
# Return an instance of Growl::Base or nil when not installed.
-
+
def self.new *args, &block
return unless installed?
Base.new *args, &block
end
-
+
##
# Normalize the icon option in +options+. This performs
# the following operations in order to allow for the :icon
@@ -87,7 +103,7 @@ def self.new *args, &block
# * 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
@@ -107,18 +123,18 @@ def self.normalize_icon! options = {}
end
end
end
-
+
#--
# Growl base
#++
-
+
class Base
attr_reader :args
-
+
##
# Initialize with optional +block+, which is then
# instance evaled or yielded depending on the blocks arity.
-
+
def initialize options = {}, &block
@args = []
if block_given?
@@ -133,10 +149,10 @@ def initialize options = {}, &block
end
end
end
-
+
##
# Run the notification, only --message is required.
-
+
def run
raise Error, 'message required' unless message
self.class.switches.each do |switch|
@@ -147,7 +163,7 @@ def run
end
Growl.exec *args
end
-
+
##
# Define a switch +name+.
#
@@ -160,7 +176,7 @@ def run
# @growl.sticky = false # => false
# @growl.sticky? # => false
#
-
+
def self.switch name
ivar = :"@#{name}"
(@switches ||= []) << name
@@ -168,18 +184,18 @@ def self.switch name
define_method(:"#{name}?") { instance_variable_get(ivar) }
define_method(:"#{name}!") { instance_variable_set(ivar, true) }
end
-
+
##
# Return array of available switch symbols.
-
+
def self.switches
@switches
end
-
+
#--
# Switches
#++
-
+
switch :title
switch :message
switch :sticky
@@ -199,4 +215,4 @@ def self.switches
end
-end
+end
Something went wrong with that request. Please try again.