lib/fontcustom/watcher.rb in fontcustom-1.1.0.pre2 vs lib/fontcustom/watcher.rb in fontcustom-1.1.0
- old
+ new
@@ -1,10 +1,12 @@
require "fontcustom"
require "listen"
module Fontcustom
class Watcher
+ include Util
+
def initialize(opts)
@opts = opts
@vector_listener = Listen.to(@opts.input[:vectors]).relative_paths(true).filter(/\.(eps|svg)$/).change(&callback)
templates = @opts.templates.dup
@@ -25,58 +27,58 @@
@template_listener = @template_listener.polling_fallback_message(false) if @template_listener
end
end
def watch
- puts "Font Custom is watching your icons at #{@opts.input[:vectors]}. Press Ctrl + C to stop."
compile unless @opts.skip_first
+ start
+ rescue SignalException # Catches Ctrl + C
+ stop
+ end
- # Non-blocking if test
- if @is_test
+ private
+
+ def start
+ if @is_test # Non-blocking listener
@vector_listener.start
@template_listener.start if @template_listener
else
@vector_listener.start!
@template_listener.start! if @template_listener
end
-
- rescue Fontcustom::Error => e
- show_error e
-
- # Catches Ctrl + C
- rescue SignalException
- stop
end
def stop
@vector_listener.stop
@template_listener.stop if @template_listener
- puts "\nFont Custom is signing off. Good night and good luck."
+ say "\nFont Custom is signing off. Good night and good luck.", :yellow
end
- private
-
def callback
Proc.new do |modified, added, removed|
begin
- puts " >> Changed: " + modified.join(", ") unless modified.empty?
- puts " >> Added: " + added.join(", ") unless added.empty?
- puts " >> Removed: " + removed.join(", ") unless removed.empty?
-
+ say_message :changed, modified.join(", ") unless modified.empty?
+ say_message :added, added.join(", ") unless added.empty?
+ say_message :removed, removed.join(", ") unless removed.empty?
changed = modified + added + removed
compile unless changed.empty?
rescue Fontcustom::Error => e
- show_error e
+ say_message :error, e.message, :red
end
end
end
def compile
- Fontcustom::Generator::Font.start [@opts]
- Fontcustom::Generator::Template.start [@opts]
+ Generator::Font.start [@opts]
+ Generator::Template.start [@opts]
end
- def show_error(err)
- puts "ERROR: #{err.message}"
+ def say(*args)
+ return if @opts.quiet
+ @opts.instance_variable_get(:@shell).say *args
+ end
+
+ def say_message(*args)
+ @opts.say_message *args
end
end
end