Module: Twterm::Tab::StatusesTab

Includes:
AutoReloadable, Base, Scrollable
Included in:
ConversationTab, ListTab, MentionsTab, SearchTab, TimelineTab, UserTab
Defined in:
lib/twterm/tab/statuses_tab.rb

Instance Attribute Summary

Attributes included from Base

#title

Instance Method Summary collapse

Methods included from AutoReloadable

#auto_reload

Methods included from Scrollable

#draw_scroll_bar, #index, #item_appended, #item_prepended, #last, #move_down, #move_to_bottom, #move_to_top, #move_up, #offset, #update_scrollbar_length

Methods included from Base

#==, #close, #refresh

Instance Method Details

#append(status) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/twterm/tab/statuses_tab.rb', line 25

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

  return if @statuses.any? { |s| s == status }

  @statuses.unshift(status)
  status.split(@window.maxx - 4)
  item_appended
  refresh
end

#delete_status(status_id) ⇒ Object



61
62
63
64
65
# File 'lib/twterm/tab/statuses_tab.rb', line 61

def delete_status(status_id)
  detector = -> (status) { status.id == status_id }
  @statuses.delete_if(&detector)
  refresh
end

#favoriteObject



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/twterm/tab/statuses_tab.rb', line 41

def favorite
  return if highlighted_status.nil?
  if highlighted_status.favorited?
    Client.current.unfavorite(highlighted_status) do
      refresh
    end
  else
    Client.current.favorite(highlighted_status) do
      refresh
    end
  end
end

#fetchObject



87
88
89
# File 'lib/twterm/tab/statuses_tab.rb', line 87

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

#initializeObject



8
9
10
11
12
# File 'lib/twterm/tab/statuses_tab.rb', line 8

def initialize
  super

  @statuses = []
end


74
75
76
77
78
79
# File 'lib/twterm/tab/statuses_tab.rb', line 74

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))
end

#prepend(status) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/twterm/tab/statuses_tab.rb', line 14

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

  return if @statuses.any? { |s| s.id == status.id }

  @statuses << status
  status.split(@window.maxx - 4)
  item_prepended
  refresh
end

#replyObject



36
37
38
39
# File 'lib/twterm/tab/statuses_tab.rb', line 36

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

#respond_to_key(key) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/twterm/tab/statuses_tab.rb', line 178

def respond_to_key(key)
  return true if super

  case key
  when 'c'
    show_conversation
  when 'F'
    favorite
  when 'o'
    open_link
  when 'r'
    reply
  when 'R'
    retweet
  when 18
    fetch
  when 'U'
    show_user
  else
    return false
  end
  true
end

#retweetObject



54
55
56
57
58
59
# File 'lib/twterm/tab/statuses_tab.rb', line 54

def retweet
  return if highlighted_status.nil?
  Client.current.retweet(highlighted_status) do
    refresh
  end
end

#show_conversationObject



81
82
83
84
85
# File 'lib/twterm/tab/statuses_tab.rb', line 81

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

#show_userObject



67
68
69
70
71
72
# File 'lib/twterm/tab/statuses_tab.rb', line 67

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

#updateObject



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/twterm/tab/statuses_tab.rb', line 91

def update
  current_line = 0

  @window.clear

  @statuses.reverse.drop(offset).each.with_index(offset) do |status, i|
    formatted_lines = status.split(@window.maxx - 4).count
    if current_line + formatted_lines + 2 > @window.maxy
      @scrollable_last = i
      break
    end

    posy = current_line

    if index == i
      @window.with_color(:black, :magenta) do
        (formatted_lines + 1).times do |j|
          @window.setpos(posy + j, 0)
          @window.addch(' ')
        end
      end
    end

    @window.setpos(current_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 |line|
      current_line += 1
      @window.setpos(current_line, 2)
      @window.addstr(line)
    end

    current_line += 2
  end

  draw_scroll_bar

  @window.refresh

  UserWindow.instance.update(highlighted_status.user) unless highlighted_status.nil?
  show_help
end