lib/cutter/stamper.rb in cutter-0.8.7 vs lib/cutter/stamper.rb in cutter-0.8.8

- old
+ new

@@ -1,50 +1,66 @@ -class Object - def time_now - Time.now.strftime("%s%L").to_i +class Time + def ms_since time + ((Time.now - time) * 1000).to_i end +end - def stamper name = nil, &block - +class Object + def stamper *args, &block return if stamper_class.off? + + options = args.extract_options! + + name = args.first + + capture = options.delete :capture + + scope = stamper_class[name] || stamper_class[:default] scope.indent = stamper_class.last ? stamper_class.last.indent + 1 : 0 stamper_class.push scope msg = 'no msg' if scope message = scope.label.values.first end + + print "\n" + spaces = " " * scope.indent - puts "\n" + log_coloured spaces, "#{message}", __color__(:message_name) log_coloured spaces, "#{'-'*message.length}", __color__(:message_line) - scope.time_initial = time_now + scope.time_initial = Time.now - self.class.send :define_method, :stamp do |*args| + (class << self; self end).send :define_method, :stamp do |*args| scope.stamp args.first end - self.class.send :alias_method, :stamp!, :stamp - - yield scope - instance_eval do - undef :stamp if respond_to? :stamp - undef :stamp! if respond_to? :stamp! + (class << self; self end).send :define_method, :stamp! do |*args| + scope.stamp args.first end + capture ? capture_stdout { yield scope } : yield(scope) + + (class << self; self end).send :remove_method, :stamp if respond_to? :stamp + (class << self; self end).send :remove_method, :stamp! if respond_to? :stamp! + scope.indent -= 1 if scope.indent > 0 stamper_class.pop - time_passed = time_now - scope.time_initial + time_passed = Time.now.ms_since scope.time_initial + tps = "#{time_passed}ms" offset = message.length - tps.length offset = 0 if offset < 0 log_coloured spaces, "#{'-'*message.length}", __color__(:total_line) log_coloured spaces + "#{' ' * (offset)}", tps, __color__(:total_count) - puts "\n" + print "\n" + + tps end private def __color__ type @@ -113,12 +129,12 @@ end def stamp lbl = nil return if Stamper.off? message = messages[lbl] || lbl.to_s - time_passed = time_now - time_initial + time_passed = Time.now.ms_since time_initial print " " * nindent - printf("stamp: %7d ms #{message}\n", time_passed) + printf("stamp: %7dms #{message}\n", time_passed) end alias_method :stamp!, :stamp module ClassMethods