forked from freeCodeCamp/devdocs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubscriber.rb
More file actions
53 lines (41 loc) · 1.04 KB
/
subscriber.rb
File metadata and controls
53 lines (41 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
require 'active_support/subscriber'
module Docs
class Subscriber < ActiveSupport::Subscriber
cattr_accessor :namespace
def self.subscribe_to(notifier)
attach_to(namespace, new, notifier)
end
private
delegate :puts, :print, :tty?, to: :$stdout
def log(msg)
puts "\r" + justify(msg)
end
def format_url(url)
url.to_s.remove %r{\Ahttps?://}
end
def format_path(path)
path.to_s.remove File.join(File.expand_path('.'), '')
end
def justify(str)
return str unless terminal_width
max_length = if tag = str.slice!(/ \[.+\]\z/)
terminal_width - tag.length
else
terminal_width
end
str.truncate(max_length).ljust(max_length) << tag.to_s
end
def terminal_width
return @terminal_width if defined? @terminal_width
@terminal_width = if !tty?
nil
elsif ENV['COLUMNS']
ENV['COLUMNS'].to_i
else
`stty size`.scan(/\d+/).last.to_i
end
rescue
@terminal_width = nil
end
end
end