Module: Twterm::Tab::Statuses::Base

Includes:
FilterableList, Base, Twterm::Tab::Scrollable
Included in:
Conversation, Favorites, Home, ListTimeline, Mentions, Search, UserTimeline
Defined in:
lib/twterm/tab/statuses/base.rb

Instance Attribute Summary

Attributes included from Twterm::Tab::Scrollable

#scroller

Attributes included from Base

#title, #window

Instance Method Summary collapse

Methods included from FilterableList

#filter, #filter_query, #reset_filter

Methods included from Base

#==, #close, #refresh, #resize

Instance Method Details

#append(status) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/twterm/tab/statuses/base.rb', line 9

def append(status)
  fail ArgumentError,
    'argument must be an instance of Status class' unless status.is_a? Status

  return if @status_ids.include?(status.id)

  @status_ids.unshift(status.id)
  status.split(window.maxx - 4)
  status.touch!
  scroller.item_appended!
  refresh
end

#delete(status_id) ⇒ Object



22
23
24
25
# File 'lib/twterm/tab/statuses/base.rb', line 22

def delete(status_id)
  Status.delete(status_id)
  refresh
end

#destroy_statusObject



27
28
29
30
31
32
33
34
# File 'lib/twterm/tab/statuses/base.rb', line 27

def destroy_status
  status = highlighted_status

  Client.current.destroy_status(status).then do
    delete(status.id)
    refresh
  end
end

#drawable_item_countObject



36
37
38
39
40
41
42
# File 'lib/twterm/tab/statuses/base.rb', line 36

def drawable_item_count
  statuses.reverse.drop(scroller.offset).lazy
  .map { |s| s.split(window.maxx - 4).count + 2 }
  .scan(0, :+)
  .select { |l| l < window.maxy }
  .count
end

#favoriteObject



44
45
46
47
48
49
50
# File 'lib/twterm/tab/statuses/base.rb', line 44

def favorite
  return if highlighted_status.nil?

  method_name = highlighted_status.favorited ? :unfavorite : :favorite
  Client.current.method(method_name).call(highlighted_status)
    .then { refresh }
end

#fetchObject



52
53
54
# File 'lib/twterm/tab/statuses/base.rb', line 52

def fetch
  fail NotImplementedError, 'fetch method must be implemented'
end

#initializeObject



56
57
58
59
60
# File 'lib/twterm/tab/statuses/base.rb', line 56

def initialize
  super

  @status_ids = []
end

#itemsObject



62
63
64
# File 'lib/twterm/tab/statuses/base.rb', line 62

def items
  statuses.reverse
end


66
67
68
69
70
71
72
73
74
# File 'lib/twterm/tab/statuses/base.rb', line 66

def open_link
  return if highlighted_status.nil?

  status = highlighted_status
  urls = status.urls.map(&:expanded_url) + status.media.map(&:expanded_url)
  urls.each(&Launchy.method(:open))
rescue Launchy::CommandNotFoundError
  Notifier.instance.show_error 'Cannot find web browser'
end

#prepend(status) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/twterm/tab/statuses/base.rb', line 76

def prepend(status)
  fail unless status.is_a? Status

  return if @status_ids.include?(status.id)

  @status_ids << status.id
  status.split(window.maxx - 4)
  status.touch!
  scroller.item_prepended!
  refresh
end

#replyObject



88
89
90
91
# File 'lib/twterm/tab/statuses/base.rb', line 88

def reply
  return if highlighted_status.nil?
  Tweetbox.instance.compose(highlighted_status)
end

#respond_to_key(key) ⇒ Object



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
# File 'lib/twterm/tab/statuses/base.rb', line 93

def respond_to_key(key)
  return true if scroller.respond_to_key(key)

  case key
  when ?c
    show_conversation
  when ?D
    destroy_status
  when ?F
    favorite
  when ?o
    open_link
  when ?r
    reply
  when ?R
    retweet
  when 18
    fetch
  when ?U
    show_user
  when ?/
    filter
  when ?q
    reset_filter
  else
    return false
  end
  true
end

#retweetObject



123
124
125
126
# File 'lib/twterm/tab/statuses/base.rb', line 123

def retweet
  return if highlighted_status.nil?
  Client.current.retweet(highlighted_status).then { refresh }
end

#show_conversationObject



128
129
130
131
132
# File 'lib/twterm/tab/statuses/base.rb', line 128

def show_conversation
  return if highlighted_status.nil?
  tab = Tab::Statuses::Conversation.new(highlighted_status.id)
  TabManager.instance.add_and_show(tab)
end

#show_userObject



134
135
136
137
138
139
# File 'lib/twterm/tab/statuses/base.rb', line 134

def show_user
  return if highlighted_status.nil?
  user = highlighted_status.user
  user_tab = Tab::UserTab.new(user.id)
  TabManager.instance.add_and_show(user_tab)
end

#statusesObject



141
142
143
144
145
146
147
148
149
150
# File 'lib/twterm/tab/statuses/base.rb', line 141

def statuses
  statuses = @status_ids.map { |id| Status.find(id) }.reject(&:nil?)
  @status_ids = statuses.map(&:id)

  if filter_query.empty?
    statuses
  else
    statuses.select { |s| s.matches?(filter_query) }
  end
end

#total_item_countObject



156
157
158
# File 'lib/twterm/tab/statuses/base.rb', line 156

def total_item_count
  filter_query.empty? ? @status_ids.count : statuses.count
end

#touch_statusesObject



152
153
154
# File 'lib/twterm/tab/statuses/base.rb', line 152

def touch_statuses
  statuses.reverse.take(100).each(&:touch!)
end

#updateObject



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/twterm/tab/statuses/base.rb', line 160

def update
  line = 0

  scroller.drawable_items.each.with_index(0) do |status, i|
    formatted_lines = status.split(window.maxx - 4).count
    window.with_color(:black, :magenta) do
      (formatted_lines + 1).times do |j|
        window.setpos(line + j, 0)
        window.addch(' ')
      end
    end if scroller.current_item?(i)

    window.setpos(line, 2)

    window.bold do
      window.with_color(status.user.color) do
        window.addstr(status.user.name)
      end
    end

    window.addstr(" (@#{status.user.screen_name}) [#{status.date}] ")

    unless status.retweeted_by.nil?
      window.addstr('(retweeted by ')
      window.bold do
        window.addstr("@#{status.retweeted_by.screen_name}")
      end
      window.addstr(') ')
    end

    if status.favorited?
      window.with_color(:black, :yellow) do
        window.addch(' ')
      end

      window.addch(' ')
    end

    if status.retweeted?
      window.with_color(:black, :green) do
        window.addch(' ')
      end
      window.addch(' ')
    end

    if status.favorite_count > 0
      window.with_color(:yellow) do
        window.addstr("#{status.favorite_count}fav#{status.favorite_count > 1 ? 's' : ''}")
      end
      window.addch(' ')
    end

    if status.retweet_count > 0
      window.with_color(:green) do
        window.addstr("#{status.retweet_count}RT#{status.retweet_count > 1 ? 's' : ''}")
      end
      window.addch(' ')
    end

    status.split(window.maxx - 4).each do |str|
      line += 1
      window.setpos(line, 2)
      window.addstr(str)
    end

    line += 2
  end
end