lib/tane/helpers.rb in tane-0.0.1 vs lib/tane/helpers.rb in tane-0.0.2

- old
+ new

@@ -9,10 +9,12 @@ def self.included(mod) mod.extend(ClassMethods) end module ClassMethods + @should_throb = false + def term @hl ||= HighLine.new end def verbose_say(message) @@ -62,11 +64,11 @@ def in_rails_dir? Dir.exists?('./rails') || Dir.exists?('./script') end def bushido_app_exists? - File.exists?('.bushido/tane.yml') and File.exists?('.bushido/email.yml') + File.exists?('.bushido/tane.yml') and File.directory?('.bushido/emails') end def username begin return credentials[:username] @@ -161,8 +163,85 @@ result = JSON(RestClient.put url, data, :content_type => :json, :accept => :json) verbose_say(result.inspect) result end + + def repeat_every(interval, &block) + Thread.new do + loop do + start_time = Time.now + yield + elapsed = Time.now - start_time + sleep([interval - elapsed, 0].max) + end + end + end + + def start_throbber! + throbber_frames = ['|', '/', '-', '\\', 'X'] + frame_counter = 0 + + @should_throb = true + + thread = repeat_every(0.5) do + if @should_throb + print "\x08\x08\x08" + frame_counter += 1 + frame_counter = 0 if frame_counter == throbber_frames.length + print "#{throbber_frames[frame_counter]} " + end + end + + end + + def stop_throbber! + @should_throb = false + end + + def should_throb + @should_throb + end + + def should_throb=(value) + @should_throb = value + end + + # Takes a list of ENV vars, records their value, + # sets them to null, runs the block, then ensures + # the ENV vars are restored at the end. + def suppress_env_vars(*vars, &block) + cache = {} + vars.each do |var| + cache[var] = ENV[var] + end + + begin + vars.each do |var| + ENV[var] = nil + end + yield block + ensure + cache.each_pair do |key, value| + ENV[key] = value + end + end + end + + def try_for(options, &block) + options[:seconds] ||= 30 + options[:sleep] ||= 5 + + start = Time.now + elapsed = 0 + + while elapsed < options[:seconds] + return true if yield elapsed + sleep options[:sleep] + elapsed = Time.now - start + end + + return false + end end end end