Module: T::Printable

Includes:
ActionView::Helpers::NumberHelper
Included in:
CLI, List, Search, Stream
Defined in:
lib/t/printable.rb

Constant Summary collapse

MAX_SCREEN_NAME_SIZE =
20

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/t/printable.rb', line 14

def self.included(base)

private

  def build_long_list(list)
    created_at = list.created_at > 6.months.ago ? list.created_at.strftime("%b %e %H:%M") : list.created_at.strftime("%b %e  %Y")
    [list.id, created_at, list.full_name, number_with_delimiter(list.member_count), number_with_delimiter(list.subscriber_count), list.mode, list.description]
  end

  def build_long_status(status)
    created_at = status.created_at > 6.months.ago ? status.created_at.strftime("%b %e %H:%M") : status.created_at.strftime("%b %e  %Y")
    [status.id, created_at, "@#{status.user.screen_name}", HTMLEntities.new.decode(status.text).gsub(/\n+/, ' ')]
  end

  def build_long_user(user)
    created_at = user.created_at > 6.months.ago ? user.created_at.strftime("%b %e %H:%M") : user.created_at.strftime("%b %e  %Y")
    [user.id, created_at, number_with_delimiter(user.statuses_count), number_with_delimiter(user.favourites_count), number_with_delimiter(user.listed_count), number_with_delimiter(user.friends_count), number_with_delimiter(user.followers_count), "@#{user.screen_name}", user.name]
  end

  def print_csv_list(list)
    created_at = list.created_at.utc.strftime("%Y-%m-%d %H:%M:%S %z")
    say [list.id, created_at, list.user.screen_name, list.slug, list.member_count, list.subscriber_count, list.mode, list.description].to_csv
  end

  def print_csv_status(status)
    created_at = status.created_at.utc.strftime("%Y-%m-%d %H:%M:%S %z")
    say [status.id, created_at, status.user.screen_name, HTMLEntities.new.decode(status.text)].to_csv
  end

  def print_csv_user(user)
    created_at = user.created_at.utc.strftime("%Y-%m-%d %H:%M:%S %z")
    say [user.id, created_at, user.statuses_count, user.favourites_count, user.listed_count, user.friends_count, user.followers_count, user.screen_name, user.name].to_csv
  end

  def print_in_columns(array)
    cols = HighLine::SystemExtensions.terminal_size[0]
    width = (array.map{|el| el.to_s.size}.max || 0) + 2
    array.each_with_index do |value, index|
      puts if (((index) % (cols / width))).zero? && !index.zero?
      printf("%-#{width}s", value)
    end
    puts
  end

  def print_lists(lists)
    lists = lists.sort_by{|list| list.slug.downcase} unless options['unsorted']
    if options['posted']
      lists = lists.sort_by{|user| user.created_at}
    elsif options['members']
      lists = lists.sort_by{|user| user.member_count}
    elsif options['mode']
      lists = lists.sort_by{|user| user.mode}
    elsif options['subscribers']
      lists = lists.sort_by{|user| user.subscriber_count}
    end
    lists.reverse! if options['reverse']
    if options['csv']
      say ["ID", "Created at", "Screen name", "Slug", "Members", "Subscribers", "Mode", "Description"].to_csv unless lists.empty?
      lists.each do |list|
        print_csv_list(list)
      end
    elsif options['long']
      array = lists.map do |list|
        build_long_list(list)
      end
      if STDOUT.tty?
        headings = ["ID", "Created at", "Slug", "Members", "Subscribers", "Mode", "Description"]
        array.unshift(headings) unless lists.empty?
        print_table(array, :truncate => true)
      else
        print_table(array)
      end
    else
      if STDOUT.tty?
        print_in_columns(lists.map(&:full_name))
      else
        lists.each do |list|
          say list.full_name
        end
      end
    end
  end

  def print_status(status)
    if STDOUT.tty? && !options['no-color']
      say("   #{Thor::Shell::Color::BOLD}@#{status.user.screen_name}", :yellow)
      print_wrapped(HTMLEntities.new.decode(status.text), :indent => 3)
    else
      say("   @#{status.user.screen_name}")
      print_wrapped(HTMLEntities.new.decode(status.text), :indent => 3)
    end
    say
  end

  def print_statuses(statuses)
    statuses.reverse! if options['reverse'] || options['stream']
    if options['csv']
      say ["ID", "Posted at", "Screen name", "Text"].to_csv unless statuses.empty?
      statuses.each do |status|
        print_csv_status(status)
      end
    elsif options['long']
      array = statuses.map do |status|
        build_long_status(status)
      end
      if STDOUT.tty?
        headings = ["ID", "Posted at", "Screen name", "Text"]
        array.unshift(headings) unless statuses.empty?
        print_table(array, :truncate => true)
      else
        print_table(array)
      end
    else
      statuses.each do |status|
        print_status(status)
      end
    end
  end

  def print_users(users)
    users = users.sort_by{|user| user.screen_name.downcase} unless options['unsorted']
    if options['posted']
      users = users.sort_by{|user| user.created_at}
    elsif options['favorites']
      users = users.sort_by{|user| user.favourites_count}
    elsif options['followers']
      users = users.sort_by{|user| user.followers_count}
    elsif options['friends']
      users = users.sort_by{|user| user.friends_count}
    elsif options['listed']
      users = users.sort_by{|user| user.listed_count}
    elsif options['tweets']
      users = users.sort_by{|user| user.statuses_count}
    end
    users.reverse! if options['reverse']
    if options['csv']
      say ["ID", "Since", "Tweets", "Favorites", "Listed", "Following", "Followers", "Screen name", "Name"].to_csv unless users.empty?
      users.each do |user|
        print_csv_user(user)
      end
    elsif options['long']
      array = users.map do |user|
        build_long_user(user)
      end
      if STDOUT.tty?
        headings = ["ID", "Since", "Tweets", "Favorites", "Listed", "Following", "Followers", "Screen name", "Name"]
        array.unshift(headings) unless users.empty?
        print_table(array, :truncate => true)
      else
        print_table(array)
      end
    else
      if STDOUT.tty?
        print_in_columns(users.map{|user| "@#{user.screen_name}"})
      else
        users.each do |user|
          say "@#{user.screen_name}"
        end
      end
    end
  end

end

Instance Method Details

#build_long_list(list) ⇒ Object



18
19
20
21
# File 'lib/t/printable.rb', line 18

def build_long_list(list)
  created_at = list.created_at > 6.months.ago ? list.created_at.strftime("%b %e %H:%M") : list.created_at.strftime("%b %e  %Y")
  [list.id, created_at, list.full_name, number_with_delimiter(list.member_count), number_with_delimiter(list.subscriber_count), list.mode, list.description]
end

#build_long_status(status) ⇒ Object



23
24
25
26
# File 'lib/t/printable.rb', line 23

def build_long_status(status)
  created_at = status.created_at > 6.months.ago ? status.created_at.strftime("%b %e %H:%M") : status.created_at.strftime("%b %e  %Y")
  [status.id, created_at, "@#{status.user.screen_name}", HTMLEntities.new.decode(status.text).gsub(/\n+/, ' ')]
end

#build_long_user(user) ⇒ Object



28
29
30
31
# File 'lib/t/printable.rb', line 28

def build_long_user(user)
  created_at = user.created_at > 6.months.ago ? user.created_at.strftime("%b %e %H:%M") : user.created_at.strftime("%b %e  %Y")
  [user.id, created_at, number_with_delimiter(user.statuses_count), number_with_delimiter(user.favourites_count), number_with_delimiter(user.listed_count), number_with_delimiter(user.friends_count), number_with_delimiter(user.followers_count), "@#{user.screen_name}", user.name]
end


33
34
35
36
# File 'lib/t/printable.rb', line 33

def print_csv_list(list)
  created_at = list.created_at.utc.strftime("%Y-%m-%d %H:%M:%S %z")
  say [list.id, created_at, list.user.screen_name, list.slug, list.member_count, list.subscriber_count, list.mode, list.description].to_csv
end


38
39
40
41
# File 'lib/t/printable.rb', line 38

def print_csv_status(status)
  created_at = status.created_at.utc.strftime("%Y-%m-%d %H:%M:%S %z")
  say [status.id, created_at, status.user.screen_name, HTMLEntities.new.decode(status.text)].to_csv
end


43
44
45
46
# File 'lib/t/printable.rb', line 43

def print_csv_user(user)
  created_at = user.created_at.utc.strftime("%Y-%m-%d %H:%M:%S %z")
  say [user.id, created_at, user.statuses_count, user.favourites_count, user.listed_count, user.friends_count, user.followers_count, user.screen_name, user.name].to_csv
end


48
49
50
51
52
53
54
55
56
# File 'lib/t/printable.rb', line 48

def print_in_columns(array)
  cols = HighLine::SystemExtensions.terminal_size[0]
  width = (array.map{|el| el.to_s.size}.max || 0) + 2
  array.each_with_index do |value, index|
    puts if (((index) % (cols / width))).zero? && !index.zero?
    printf("%-#{width}s", value)
  end
  puts
end


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/t/printable.rb', line 58

def print_lists(lists)
  lists = lists.sort_by{|list| list.slug.downcase} unless options['unsorted']
  if options['posted']
    lists = lists.sort_by{|user| user.created_at}
  elsif options['members']
    lists = lists.sort_by{|user| user.member_count}
  elsif options['mode']
    lists = lists.sort_by{|user| user.mode}
  elsif options['subscribers']
    lists = lists.sort_by{|user| user.subscriber_count}
  end
  lists.reverse! if options['reverse']
  if options['csv']
    say ["ID", "Created at", "Screen name", "Slug", "Members", "Subscribers", "Mode", "Description"].to_csv unless lists.empty?
    lists.each do |list|
      print_csv_list(list)
    end
  elsif options['long']
    array = lists.map do |list|
      build_long_list(list)
    end
    if STDOUT.tty?
      headings = ["ID", "Created at", "Slug", "Members", "Subscribers", "Mode", "Description"]
      array.unshift(headings) unless lists.empty?
      print_table(array, :truncate => true)
    else
      print_table(array)
    end
  else
    if STDOUT.tty?
      print_in_columns(lists.map(&:full_name))
    else
      lists.each do |list|
        say list.full_name
      end
    end
  end
end


97
98
99
100
101
102
103
104
105
106
# File 'lib/t/printable.rb', line 97

def print_status(status)
  if STDOUT.tty? && !options['no-color']
    say("   #{Thor::Shell::Color::BOLD}@#{status.user.screen_name}", :yellow)
    print_wrapped(HTMLEntities.new.decode(status.text), :indent => 3)
  else
    say("   @#{status.user.screen_name}")
    print_wrapped(HTMLEntities.new.decode(status.text), :indent => 3)
  end
  say
end


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/t/printable.rb', line 108

def print_statuses(statuses)
  statuses.reverse! if options['reverse'] || options['stream']
  if options['csv']
    say ["ID", "Posted at", "Screen name", "Text"].to_csv unless statuses.empty?
    statuses.each do |status|
      print_csv_status(status)
    end
  elsif options['long']
    array = statuses.map do |status|
      build_long_status(status)
    end
    if STDOUT.tty?
      headings = ["ID", "Posted at", "Screen name", "Text"]
      array.unshift(headings) unless statuses.empty?
      print_table(array, :truncate => true)
    else
      print_table(array)
    end
  else
    statuses.each do |status|
      print_status(status)
    end
  end
end


133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/t/printable.rb', line 133

def print_users(users)
  users = users.sort_by{|user| user.screen_name.downcase} unless options['unsorted']
  if options['posted']
    users = users.sort_by{|user| user.created_at}
  elsif options['favorites']
    users = users.sort_by{|user| user.favourites_count}
  elsif options['followers']
    users = users.sort_by{|user| user.followers_count}
  elsif options['friends']
    users = users.sort_by{|user| user.friends_count}
  elsif options['listed']
    users = users.sort_by{|user| user.listed_count}
  elsif options['tweets']
    users = users.sort_by{|user| user.statuses_count}
  end
  users.reverse! if options['reverse']
  if options['csv']
    say ["ID", "Since", "Tweets", "Favorites", "Listed", "Following", "Followers", "Screen name", "Name"].to_csv unless users.empty?
    users.each do |user|
      print_csv_user(user)
    end
  elsif options['long']
    array = users.map do |user|
      build_long_user(user)
    end
    if STDOUT.tty?
      headings = ["ID", "Since", "Tweets", "Favorites", "Listed", "Following", "Followers", "Screen name", "Name"]
      array.unshift(headings) unless users.empty?
      print_table(array, :truncate => true)
    else
      print_table(array)
    end
  else
    if STDOUT.tty?
      print_in_columns(users.map{|user| "@#{user.screen_name}"})
    else
      users.each do |user|
        say "@#{user.screen_name}"
      end
    end
  end
end