Skip to content

Commit

Permalink
[Ruby] Add TerminalNotifier.remove(group).
Browse files Browse the repository at this point in the history
  • Loading branch information
alloy committed Jul 30, 2012
1 parent 9039a4a commit 0edbb67
Showing 1 changed file with 30 additions and 9 deletions.
39 changes: 30 additions & 9 deletions Ruby/lib/terminal-notifier.rb
Expand Up @@ -9,16 +9,22 @@ def self.available?
@available
end

# Executes the `terminal-notifier` tool through Kernel::system while
# redirecting output to `/dev/null`.
def self.silent_execute(options)
def self.silence_stdout
stdout = STDOUT.clone
STDOUT.reopen(File.new('/dev/null', 'w'))
system(BIN_PATH, *options)
yield
ensure
STDOUT.reopen(stdout)
end

def self.execute(options)
if TerminalNotifier.available?
system(BIN_PATH, *options.map { |k,v| ["-#{k}", v.to_s] }.flatten)
else
raise "terminal-notifier is only supported on Mac OS X 10.8, or higher."
end
end

# Sends a User Notification and returns wether or not it was a success.
#
# The available options are `:title`, `:group`, `:activate`, `:open`, and
Expand All @@ -37,11 +43,26 @@ def self.silent_execute(options)
#
# Raises if not supported on the current platform.
def notify(message, options = {})
if TerminalNotifier.available?
TerminalNotifier.silent_execute(options.merge(:message => message).map { |k,v| ["-#{k}", v.to_s] }.flatten)
else
raise "terminal-notifier is only supported on Mac OS X 10.8, or higher."
end
TerminalNotifier.silence_stdout { TerminalNotifier.verbose_notify(message, options) }
end
module_function :notify

# The same as `verbose`, but sends the output from the tool to STDOUT.
def verbose_notify(message, options = {})
TerminalNotifier.execute(options.merge(:message => message))
end
module_function :verbose_notify

# Removes a notification that was previously sent with the specified
# ‘group’ ID, if one exists.
def remove(group)
TerminalNotifier.silence_stdout { TerminalNotifier.verbose_remove(group) }
end
module_function :remove

# The same as `remove`, but sends the output from the tool to STDOUT.
def verbose_remove(group)
TerminalNotifier.execute(:remove => group)
end
module_function :verbose_remove
end

0 comments on commit 0edbb67

Please sign in to comment.