1
0

subscriber.rb 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. require 'active_support/subscriber'
  2. module Docs
  3. class Subscriber < ActiveSupport::Subscriber
  4. cattr_accessor :namespace
  5. def self.subscribe_to(notifier)
  6. attach_to(namespace, new, notifier)
  7. end
  8. private
  9. delegate :puts, :print, :tty?, to: :$stdout
  10. def log(msg)
  11. puts "\r" + justify(msg)
  12. end
  13. def format_url(url)
  14. url.to_s.remove %r{\Ahttps?://}
  15. end
  16. def format_path(path)
  17. path.to_s.remove File.join(File.expand_path('.'), '')
  18. end
  19. def justify(str)
  20. return str unless terminal_width
  21. max_length = if tag = str.slice!(/ \[.+\]\z/)
  22. terminal_width - tag.length
  23. else
  24. terminal_width
  25. end
  26. str.truncate(max_length).ljust(max_length) << tag.to_s
  27. end
  28. def terminal_width
  29. return @terminal_width if defined? @terminal_width
  30. @terminal_width = if !tty?
  31. nil
  32. elsif ENV['COLUMNS']
  33. ENV['COLUMNS'].to_i
  34. else
  35. `stty size`.scan(/\d+/).last.to_i
  36. end
  37. rescue
  38. @terminal_width = nil
  39. end
  40. end
  41. end