Class: Twterm::Tab::Statuses::Base

Inherits:
Base
  • Object
show all
Includes:
FilterableList, Publisher, Subscriber, Twterm::Tab::Scrollable, Utils
Defined in:
lib/twterm/tab/statuses/base.rb

Instance Attribute Summary

Attributes included from Twterm::Tab::Scrollable

#scroller

Attributes inherited from Base

#title, #window

Instance Method Summary collapse

Methods included from Utils

check_type

Methods included from Subscriber

included, #subscribe, #unsubscribe

Methods included from Publisher

#publish

Methods included from FilterableList

#filter, #filter_query, #reset_filter

Methods inherited from Base

#==, #close, #refresh

Constructor Details

#initializeBase

Returns a new instance of Base.



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

def initialize
  super

  @status_ids = []

  subscribe(Event::Status::Delete) { |e| delete(e.status_id) }
end

Instance Method Details

#append(status) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/twterm/tab/statuses/base.rb', line 18

def append(status)
  check_type Status, 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



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

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

#destroy_statusObject



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

def destroy_status
  status = highlighted_status

  Client.current.destroy_status(status)
end

#drawable_item_countObject



41
42
43
44
45
46
47
# File 'lib/twterm/tab/statuses/base.rb', line 41

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



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

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



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

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

#itemsObject



69
70
71
# File 'lib/twterm/tab/statuses/base.rb', line 69

def items
  statuses.reverse
end


73
74
75
76
77
78
79
80
81
# File 'lib/twterm/tab/statuses/base.rb', line 73

def open_link
  return if highlighted_status.nil?

  status = highlighted_status
  urls = status.urls.map(&:expanded_url) + status.media.map(&:expanded_url)
  urls
    .map { |url| Event::OpenURI.new(url) }
    .each { |e| publish(e) }
end

#prepend(status) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/twterm/tab/statuses/base.rb', line 83

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



95
96
97
98
# File 'lib/twterm/tab/statuses/base.rb', line 95

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

#respond_to_key(key) ⇒ Object



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

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, ?L
    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



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

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

#show_conversationObject



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

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



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

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



148
149
150
151
152
153
154
155
156
157
# File 'lib/twterm/tab/statuses/base.rb', line 148

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



163
164
165
# File 'lib/twterm/tab/statuses/base.rb', line 163

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

#touch_statusesObject



159
160
161
# File 'lib/twterm/tab/statuses/base.rb', line 159

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

#updateObject



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
228
229
230
231
232
233
234
# File 'lib/twterm/tab/statuses/base.rb', line 167

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, :red) 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(:red) do
        window.addstr("#{status.favorite_count}like#{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