Module: Guard::Notifier

Extended by:
Notifier
Included in:
Notifier
Defined in:
lib/guard/notifier.rb,
lib/guard/notifiers/gntp.rb,
lib/guard/notifiers/tmux.rb,
lib/guard/notifiers/base.rb,
lib/guard/notifiers/emacs.rb,
lib/guard/notifiers/growl.rb,
lib/guard/notifiers/libnotify.rb,
lib/guard/notifiers/rb_notifu.rb,
lib/guard/notifiers/notifysend.rb,
lib/guard/notifiers/growl_notify.rb,
lib/guard/notifiers/file_notifier.rb,
lib/guard/notifiers/terminal_title.rb,
lib/guard/notifiers/terminal_notifier.rb

Overview

The notifier handles sending messages to different notifiers. Currently the following libraries are supported:

  • Ruby GNTP

  • Growl

  • GrowlNotify

  • Libnotify

  • rb-notifu

  • emacs

  • Terminal Notifier

  • Terminal Title

  • Tmux

Please see the documentation of each notifier for more information about the requirements and configuration possibilities.

Guard knows four different notification types:

  • success

  • pending

  • failed

  • notify

The notification type selection is based on the image option that is sent to #notify. Each image type has its own notification type, and notifications with custom images goes all sent as type `notify`. The `gntp` and `growl_notify` notifiers are able to register these types at Growl and allows customization of each notification type.

Guard can be configured to make use of more than one notifier at once.

See Also:

Defined Under Namespace

Classes: Base, Emacs, FileNotifier, GNTP, Growl, GrowlNotify, Libnotify, Notifu, NotifySend, TerminalNotifier, TerminalTitle, Tmux

Constant Summary

NOTIFIERS =

List of available notifiers, grouped by functionality

[
  {
    gntp: GNTP,
    growl: Growl,
    growl_notify: GrowlNotify,
    terminal_notifier: TerminalNotifier,
    libnotify: Libnotify,
    notifysend: NotifySend,
    notifu: Notifu
  },
  { emacs: Emacs },
  { tmux: Tmux },
  { terminal_title: TerminalTitle },
  { file: FileNotifier }
]

Instance Method Summary collapse

Instance Method Details

#add_notifier(name, opts = {}) ⇒ Boolean

Add a notification library to be used.



151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/guard/notifier.rb', line 151

def add_notifier(name, opts = {})
  return turn_off if name == :off

  notifier_class = _get_notifier_module(name)

  if notifier_class && notifier_class.available?(opts)
    self.notifiers = notifiers << { name: name, options: opts }
    true
  else
    false
  end
end

#clear_notifiersObject

Clear available notifications.



86
87
88
# File 'lib/guard/notifier.rb', line 86

def clear_notifiers
  ENV['GUARD_NOTIFIERS'] = nil
end

#enabled?Boolean

Test if the notifications are on.



140
141
142
# File 'lib/guard/notifier.rb', line 140

def enabled?
  ENV['GUARD_NOTIFY'] == 'true'
end

#notifiersObject



76
77
78
# File 'lib/guard/notifier.rb', line 76

def notifiers
  ENV['GUARD_NOTIFIERS'] ? YAML::load(ENV['GUARD_NOTIFIERS']) : []
end

#notifiers=(notifiers) ⇒ Object



80
81
82
# File 'lib/guard/notifier.rb', line 80

def notifiers=(notifiers)
  ENV['GUARD_NOTIFIERS'] = YAML::dump(notifiers)
end

#notify(message, opts = {}) ⇒ Object

Show a system notification with all configured notifiers.

Options Hash (opts):

  • image (Symbol, String)

    the image symbol or path to an image

  • title (String)

    the notification title



170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/guard/notifier.rb', line 170

def notify(message, opts = {})
  return unless enabled?

  notifiers.each do |notifier|
    notifier = _get_notifier_module(notifier[:name]).new(notifier[:options])

    begin
      notifier.notify(message, opts.dup)
    rescue Exception => e
      ::Guard::UI.error "Error sending notification with #{ notifier.name }: #{ e.message }"
      ::Guard::UI.debug e.backtrace.join("\n")
    end
  end
end

#toggleObject

Toggle the system notifications on/off



127
128
129
130
131
132
133
134
# File 'lib/guard/notifier.rb', line 127

def toggle
  if enabled?
    ::Guard::UI.info 'Turn off notifications'
    turn_off
  else
    turn_on
  end
end

#turn_offObject

Turn notifications off.



115
116
117
118
119
120
121
122
123
# File 'lib/guard/notifier.rb', line 115

def turn_off
  notifiers.each do |notifier|
    notifier_class = _get_notifier_module(notifier[:name])

    notifier_class.turn_off if notifier_class.respond_to?(:turn_off)
  end

  ENV['GUARD_NOTIFY'] = 'false'
end

#turn_on(opts = {}) ⇒ Object

Turn notifications on. If no notifications are defined in the `Guardfile` Guard auto detects the first available library.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/guard/notifier.rb', line 96

def turn_on(opts = {})
  _auto_detect_notification if notifiers.empty? && (!::Guard.options || ::Guard.options[:notify])

  if notifiers.empty?
    turn_off
  else
    notifiers.each do |notifier|
      notifier_class = _get_notifier_module(notifier[:name])
      ::Guard::UI.info "Guard is using #{ notifier_class.title } to send notifications." unless opts[:silent]

      notifier_class.turn_on if notifier_class.respond_to?(:turn_on)
    end

    ENV['GUARD_NOTIFY'] = 'true'
  end
end