Class: Guard::Notifier::NotifySend

Inherits:
Base
  • Object
show all
Defined in:
lib/guard/notifiers/notifysend.rb

Overview

System notifications using notify-send, a binary that ships with the libnotify-bin package on many Debian-based distributions.

Examples:

Add the `:notifysend` notifier to your `Guardfile`

notification :notifysend

Constant Summary

DEFAULTS =

Default options for the notify-send notifications.

{
  t: 3000, # Default timeout is 3000ms
  h: 'int:transient:1' # Automatically close the notification
}
SUPPORTED =

Full list of options supported by notify-send.

[:u, :t, :i, :c, :h]

Constants inherited from Base

Base::HOSTS

Instance Attribute Summary

Attributes inherited from Base

#options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

_supported_host?, gem_name, #images_path, #initialize, name, #name, #normalize_standard_options!, require_gem_safely, title, #title

Constructor Details

This class inherits a constructor from Guard::Notifier::Base

Class Method Details

._notifysend_binary_available?Boolean



35
36
37
# File 'lib/guard/notifiers/notifysend.rb', line 35

def self._notifysend_binary_available?
  !`which notify-send`.empty?
end

._register!(opts) ⇒ Boolean

Detects if the notify-send binary is available and if not, displays an error message unless `opts` is true.



46
47
48
49
50
51
52
53
54
55
# File 'lib/guard/notifiers/notifysend.rb', line 46

def self._register!(opts)
  if _notifysend_binary_available?
    true
  else
    unless opts[:silent]
      ::Guard::UI.error 'The :notifysend notifier runs only on Linux, FreeBSD, OpenBSD and Solaris with the libnotify-bin package installed.'
    end
    false
  end
end

.available?(opts = {}) ⇒ Boolean



27
28
29
# File 'lib/guard/notifiers/notifysend.rb', line 27

def self.available?(opts = {})
  super and _register!(opts)
end

.supported_hostsObject



23
24
25
# File 'lib/guard/notifiers/notifysend.rb', line 23

def self.supported_hosts
  %w[linux freebsd openbsd sunos solaris]
end

Instance Method Details

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

Shows a system notification.

Options Hash (opts):

  • type (String)

    the notification type. Either 'success', 'pending', 'failed' or 'notify'

  • title (String)

    the notification title

  • image (String)

    the path to the notification image

  • c (String)

    the notification category

  • t (Number)

    the number of milliseconds to display (1000, 3000)



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/guard/notifiers/notifysend.rb', line 69

def notify(message, opts = {})
  super

  command = [opts[:title], message]
  opts = DEFAULTS.merge(
    i: opts.delete(:image),
    u: _notifysend_urgency(opts.delete(:type))
  ).merge(opts)

  system('notify-send', *_to_arguments(command, SUPPORTED, opts))
end