Class: Guard::Notifier::Emacs

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

Overview

Send a notification to Emacs with emacsclient (www.emacswiki.org/emacs/EmacsClient).

Examples:

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

notification :emacs

Constant Summary

DEFAULTS =
{
  client:    'emacsclient',
  success:   'ForestGreen',
  failed:    'Firebrick',
  default:   'Black',
  fontcolor: 'White',
}

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, supported_hosts, title, #title

Constructor Details

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

Class Method Details

._emacs_client_available?(opts) ⇒ Boolean



29
30
31
32
# File 'lib/guard/notifiers/emacs.rb', line 29

def self._emacs_client_available?(opts)
  result = `#{opts.fetch(:client, DEFAULTS[:client])} --eval '1' 2> #{DEV_NULL} || echo 'N/A'`
  !%w(N/A 'N/A').include?(result.chomp!)
end

.available?(opts = {}) ⇒ Boolean



21
22
23
# File 'lib/guard/notifiers/emacs.rb', line 21

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

Instance Method Details

#emacs_color(type, options = {}) ⇒ String

Get the Emacs color for the notification type. You can configure your own color by overwrite the defaults.

Options Hash (options):

  • success (String)

    the color to use for success notifications (default is 'ForestGreen')

  • failed (String)

    the color to use for failure notifications (default is 'Firebrick')

  • pending (String)

    the color to use for pending notifications

  • default (String)

    the default color to use (default is 'Black')



81
82
83
84
# File 'lib/guard/notifiers/emacs.rb', line 81

def emacs_color(type, options = {})
  default = options.fetch(:default, DEFAULTS[:default])
  options.fetch(type.to_sym, default)
end

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

Shows a system notification.

Options Hash (opts):

  • success (String)

    the color to use for success notifications (default is 'ForestGreen')

  • failed (String)

    the color to use for failure notifications (default is 'Firebrick')

  • pending (String)

    the color to use for pending notifications

  • default (String)

    the default color to use (default is 'Black')

  • client (String)

    the client to use for notification (default is 'emacsclient')

  • priority (String, Integer)

    specify an int or named key (default is 0)



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/guard/notifiers/emacs.rb', line 55

def notify(message, opts = {})
  super

  opts      = DEFAULTS.merge(opts)
  color     = emacs_color(opts[:type], opts)
  fontcolor = emacs_color(:fontcolor, opts)
  elisp = "(set-face-attribute 'mode-line nil\n:background \"\#{color}\"\n:foreground \"\#{fontcolor}\")\n".gsub(/\s+/, ' ').strip

  _run_cmd(opts[:client], '--eval', elisp)
end