Module: Spectator::Emacs

Included in:
ERunner
Defined in:
lib/spectator/emacs.rb,
lib/spectator/emacs/version.rb

Overview

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

Constant Summary collapse

VERSION =

spectator-emacs version

"0.2.4"

Instance Method Summary collapse

Instance Method Details

#blank_string?(string) ⇒ Boolean

Checks whether the string is made by whitespace characters.

Parameters:

  • string (String)

    the string to be checked

Returns:

  • (Boolean)

    non nil if the string is blank, nil otherwise.



353
354
355
# File 'lib/spectator/emacs.rb', line 353

def blank_string?(string)
  string =~ /\A\s*\n?\z/
end

#enotify_connectObject

Creates a connection to the Enotify host.



372
373
374
375
376
377
378
379
380
381
382
# 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) ⇒ Object

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

Parameters:



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# 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_registerObject

Registers the slot named @enotify_slot_id with Enotify.



319
320
321
# File 'lib/spectator/emacs.rb', line 319

def enotify_register
  enotify_send :register => @enotify_slot_id, :handler_fn => "tdd"
end

#enotify_send(object) ⇒ Object

Sends a message to the Enotify host.

Parameters:

  • object (Object)

    the object to be serialized as a lisp object (with the Object#to_lisp method) and sent as a message.



313
314
315
316
# File 'lib/spectator/emacs.rb', line 313

def enotify_send(object)
  sexp = object.to_lisp
  @sock.puts "|#{sexp.length}|#{sexp}"
end

#format_tooltip(stats) ⇒ Object

Formats the text that will be used as a tooltip for the modeline "icon".

Parameters:



390
391
392
393
394
395
396
# 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_errorObject

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



359
360
361
362
363
364
365
366
367
368
369
# 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