module Spectator::Emacs

This module contains all the functions used to interact with the Enotify emacs mode-line notification system.

Constants

VERSION

spectator-emacs version

Public Instance Methods

blank_string?(string) click to toggle source

Checks whether the string is made by whitespace characters.

@param [String] string the string to be checked @return [Boolean] non nil if the string is blank, nil otherwise.

# File lib/spectator/emacs.rb, line 353
def blank_string?(string)
  string =~ /\A\s*\n?\z/
end
enotify_connect() click to toggle source

Creates a connection to the Enotify host.

# File lib/spectator/emacs.rb, line 372
def enotify_connect
  begin
    print "=== Connecting to emacs... ".cyan
    @sock = TCPSocket.new(@enotify_host, @enotify_port)
    enotify_register
    puts "Success!".green
  rescue SocketError, Errno::ECONNREFUSED => e
    puts "Failed!".red
    rescue_sock_error
  end
end
enotify_notify(stdout, stats) click to toggle source

Sends a notification to the enotify host with the RSpec results.

@param [String] stdout the rspec command output. @param [Hash] stats the extracted summary of the results. For

details, see the return value of
{Spectator::Specs#extract_rspec_summary} for details.
# File lib/spectator/emacs.rb, line 329
def enotify_notify(stdout, stats)
  #stats = extract_rspec_stats stdout
  status = stats[:status]
  minibuffer_message = "#{stats[:examples]} examples, #{stats[:failures]} failures" +
    ((stats[:pending] > 0) ? ", #{stats[:pending]} pending.\n" : ".\n")

  message = {
    :id => @enotify_slot_id,
    :notification => {
      :text => @notification_messages[status],
      :face => @notification_face[status],
      :help => format_tooltip(stats),
      :mouse_1 => "tdd"
    },
    :data => { :mode => @report_buffer_mode, :report_text => stdout, :message => minibuffer_message }
  }

  enotify_send message
end
enotify_register() click to toggle source

Registers the slot named `@enotify_slot_id` with Enotify.

# File lib/spectator/emacs.rb, line 319
def enotify_register
  enotify_send :register => @enotify_slot_id, :handler_fn => "tdd"
end
enotify_send(object) click to toggle source

Sends a message to the Enotify host.

@param [Object] object the object to be serialized as a lisp

object (with the {Object#to_lisp} method) and sent as a message.
# File lib/spectator/emacs.rb, line 313
def enotify_send(object)
  sexp = object.to_lisp
  @sock.puts "|#{sexp.length}|#{sexp}"
end
format_tooltip(stats) click to toggle source

Formats the text that will be used as a tooltip for the modeline *“icon”*.

@param [Hash] stats the extracted summary of the results. For

details, see the return value of
{Spectator::Specs#extract_rspec_summary} for details.
# File lib/spectator/emacs.rb, line 390
def format_tooltip(stats)
  t = Time.now
  "#{t.year}-#{t.month}-#{t.day} -- #{t.hour}:#{t.min}:#{t.sec}\n" +
    "#{stats[:examples]} examples, #{stats[:failures]} failures" +
    ((stats[:pending] > 0) ? ", #{stats[:pending]} pending.\n" : ".\n") +
    "\nmouse-1: switch to rspec output buffer"
end
rescue_sock_error() click to toggle source

Interactively retries to connect to the Enotify host, asking a new host:port value.

# File lib/spectator/emacs.rb, line 359
def rescue_sock_error
  print "--- Enter Enotify host [localhost:5000]: ".yellow
  host_and_port = STDIN.gets.strip
  if blank_string?(host_and_port)
    @enotify_host, @enotify_port = ['localhost', @default_options[:enotify_port]]
  else
    @enotify_host, @enotify_port = host_and_port.split(/\s:\s/)
    @enotify_port = @enotify_port.to_i
  end
  enotify_connect
end