class Spectator::UI

Attributes

interrupted_status[RW]
status[RW]

Public Class Methods

new(config) click to toggle source
# File lib/spectator/ui.rb, line 7
def initialize(config)
  @mutex = Mutex.new
  @status = nil
  @status_callbacks = {}
  @callbacks = {}
  @queue = Queue.new
end

Public Instance Methods

<<(event) click to toggle source
# File lib/spectator/ui.rb, line 15
def << event
  @queue << event
  p event
end
ask(question) click to toggle source
# File lib/spectator/ui.rb, line 68
def ask question
  print question.yellow
  $stdout.flush
  STDIN.gets.chomp.strip.downcase
end
ask_what_to_do() click to toggle source
# File lib/spectator/ui.rb, line 45
def ask_what_to_do
  self.status = :wait_for_input
  answer = ask('--- What to do now? (q=quit, a=all-specs): ')
  case answer
  when 'q' then exit
  when 'a' then run_all
  else
    puts '--- Bad input, ignored.'.yellow
    wait_for_changes
  end
end
can_run_specs?() click to toggle source
# File lib/spectator/ui.rb, line 91
def can_run_specs?
  p [:can_run_specs?, status]
  status == :waiting_for_changes
end
exit() click to toggle source
Calls superclass method
# File lib/spectator/ui.rb, line 74
def exit
  self.status = :exiting
  puts '--- Exiting...'.white
  super
end
interrupt!() click to toggle source
# File lib/spectator/ui.rb, line 34
def interrupt!
  self.interrupted_status = status
  self << :interrupt
end
noop() click to toggle source
# File lib/spectator/ui.rb, line 30
def noop
  @noop ||= lambda {}
end
on(event, &block) click to toggle source
# File lib/spectator/ui.rb, line 41
def on event, &block
  @callbacks[event] = block
end
run(cmd) click to toggle source
# File lib/spectator/ui.rb, line 80
def run cmd
  $running = true
  start = Time.now
  puts "=== running: #{cmd} ".ljust(terminal_columns, '=').cyan
  success = system cmd
  puts "=== time: #{(Time.now - start).to_i} seconds ".ljust(terminal_columns, '=').cyan
  success
ensure
  $running = false
end
run_all() click to toggle source
# File lib/spectator/ui.rb, line 57
def run_all
  self.status = :waiting_for_changes
  self << :run_all
end
start() click to toggle source
# File lib/spectator/ui.rb, line 20
def start
  wait_for_changes
  thread = Thread.new { event_loop }
  p thread
  Thread.pass
  sleep
end
wait_for_changes() click to toggle source
# File lib/spectator/ui.rb, line 62
def wait_for_changes
  return if status == :waiting_for_changes
  self.status = :waiting_for_changes
  puts '--- Waiting for changes...'.cyan
end

Private Instance Methods

event_loop() click to toggle source
# File lib/spectator/ui.rb, line 104
def event_loop
  loop do
    event = @queue.pop
    p [:queue, event]
    @mutex.synchronize { @callbacks[event].call }
  end
end
terminal_columns() click to toggle source
# File lib/spectator/ui.rb, line 99
def terminal_columns
  cols = `tput cols 2> /dev/tty`.strip.to_i
  ($?.success? && cols.nonzero?) ? cols : 80
end