lib/arborist/command/watch.rb in arborist-0.2.0.pre20170519125456 vs lib/arborist/command/watch.rb in arborist-0.2.0

- old
+ new

@@ -1,8 +1,9 @@ # -*- ruby -*- #encoding: utf-8 +require 'tty/spinner' require 'msgpack' require 'arborist/cli' unless defined?( Arborist::CLI ) require 'arborist/client' require 'arborist/event_api' @@ -25,10 +26,13 @@ # Watch for system events as well prompt.say "Subscribing to manager heartbeat events." sock.subscribe( 'sys.heartbeat' ) + spinner = TTY::Spinner.new( "[ :spinner ] waiting for events...\r", + frames: HEARTBEAT_CHARACTERS, hide_cursor: true ) + begin last_runid = nil prompt.say "Watching for events on manager at %s" % [ client.event_api_url ] loop do msg = sock.receive @@ -45,26 +49,26 @@ prompt.say "New subscription %p" % [ subid ] else self.log.debug "Manager is alive (runid: %s)" % [ this_runid ] end - $stderr.print( heartbeat() ) + spinner.spin last_runid = this_runid when 'sys.node_added' prompt.say "[%s] «Node added» %s\n" % [ - hl(Time.now.strftime('%Y-%m-%d %H:%M:%S %Z')).color(:dark, :white), - hl(event['node']).color( :bold, :cyan ) + hl.dark.white( Time.now.strftime('%Y-%m-%d %H:%M:%S %Z') ), + hl.bold.cyan( event['node'] ) ] when 'sys.node_removed' prompt.say "[%s] »Node removed« %s\n" % [ - hl(Time.now.strftime('%Y-%m-%d %H:%M:%S %Z')).color(:dark, :white), - hl(event['node']).color( :dark, :cyan ) + hl.dark.white( Time.now.strftime('%Y-%m-%d %H:%M:%S %Z') ), + hl.dark.cyan( event['node'] ) ] else prompt.say "[%s] %s\n" % [ - hl(Time.now.strftime('%Y-%m-%d %H:%M:%S %Z')).color(:dark, :white), - hl(dump_event( event )).color( :dark, :white ) + hl.dark.white( Time.now.strftime('%Y-%m-%d %H:%M:%S %Z') ), + hl.dark.white( dump_event( event ) ) ] end end ensure self.log.info "Unsubscribing from subscription %p" % [ subid ] @@ -95,44 +99,40 @@ case event_type when 'node.update' type, status, errors = event['data'].values_at( *%w'type status errors' ) return "%s updated: %s is %s%s" % [ - hl( id ).color( :cyan ), + hl.cyan( id ), type, - hl( status ).color( status.to_sym ), + hl.decorate( status, status.to_sym ), errors ? " (#{errors})" : '' ] when 'node.delta' pairs = diff_pairs( event['data'] ) - return "%s delta, changes: %s" % [ hl( id ).color( :cyan ), pairs ] + return "%s delta, changes: %s" % [ hl.cyan( id ), pairs ] else - return "%s event: %p" % [ hl(event_type).color(:dark, :white), event ] + return "%s event: %p" % [ hl.dark.white( event_type ), event ] end end ### Return a string showing the differences in a delta event's change +data+. def diff_pairs( data ) - return data.collect do |key, pairs| + diff = data.collect do |key, pairs| if pairs.is_a?( Hash ) diff_pairs( pairs ) else val1, val2 = *pairs "%s: %s -> %s" % [ - hl( key ).color( :dark, :white ), - hl( val1 ).color( :yellow ), - hl( val2 ).color( :bold, :yellow ) + hl.dark.white( key ), + hl.yellow( val1 ), + hl.bold.yellow( val2 ) ] end - end.join( hl(", ").color(:dark, :white) ) - end + end - - ### Return a heartbeat string for the current time. - def heartbeat - idx = (Time.now.to_i % HEARTBEAT_CHARACTERS.length) - 1 - return " " + HEARTBEAT_CHARACTERS[ idx ] + "\x08\x08" + return hl.dark.white( diff.join(', ') ) end + end # module Arborist::CLI::Watch