twat 0.5.2 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/twat.rb +1 -1
  2. data/lib/twat/actions.rb +76 -4
  3. metadata +6 -7
@@ -45,7 +45,7 @@ end
45
45
  module Twat
46
46
  VERSION_MAJOR = 0
47
47
  VERSION_MINOR = 5
48
- VERSION_PATCH = 2
48
+ VERSION_PATCH = 3
49
49
 
50
50
  VERSION = "#{VERSION_MAJOR}.#{VERSION_MINOR}.#{VERSION_PATCH}"
51
51
  class Twat
@@ -2,6 +2,45 @@ module Twat
2
2
  OAUTH_TOKENS = [ :oauth_token, :oauth_token_secret ]
3
3
  CONSUMER_TOKENS = [ :consumer_key, :consumer_secret ]
4
4
 
5
+ class NoSuchTweet < Exception; end
6
+
7
+ class TweetStack
8
+ # A circularly linked list representing all of the tweets printed thus far,
9
+ # for the purposes of retrieving them after being printed
10
+ def initialize
11
+ @stack = {}
12
+ @_next = 0
13
+ end
14
+
15
+ def [] k
16
+ @stack[k]
17
+ end
18
+
19
+ def << v
20
+ @stack[nxt] = v
21
+ end
22
+
23
+ def include? k
24
+ @stack.keys.include?(k)
25
+ end
26
+
27
+ def last
28
+ # I see the irony
29
+ @_next
30
+ end
31
+
32
+ private
33
+
34
+ def nxt
35
+ if @_next == 99
36
+ @_next = 0
37
+ else
38
+ @_next += 1
39
+ end
40
+ end
41
+
42
+ end
43
+
5
44
  class Actions
6
45
 
7
46
  attr_accessor :config, :opts, :failcount
@@ -82,18 +121,39 @@ module Twat
82
121
 
83
122
  private
84
123
 
124
+ # The handling of the shared variable in the follow code makes a shitton
125
+ # more sense in the context of a class (ala the subcommands branch). For
126
+ # now the kludginess must be tolerated
127
+
85
128
  # @return last_id
86
129
  def process_followed(tweets)
87
130
  last_id = nil
88
131
  tweets.reverse.each do |tweet|
132
+ id = @tweetstack << tweet
89
133
  beep if config.beep? && tweet.text.mentions?(config.account_name)
90
- format(tweet)
134
+ format(tweet, @tweetstack.last)
91
135
  last_id = tweet.id
92
136
  end
93
137
 
94
138
  return last_id
95
139
  end
96
140
 
141
+ def handle_input(inp)
142
+ case inp
143
+ when /[rR][tT] ([0-9]{1,2})/
144
+ begin
145
+ retweet($1.to_i)
146
+ rescue NoSuchTweet
147
+ puts "No such tweet".red
148
+ end
149
+ end
150
+ end
151
+
152
+ def retweet(idx)
153
+ raise NoSuchTweet unless @tweetstack.include?(idx)
154
+ Twitter.retweet(@tweetstack[idx].id)
155
+ end
156
+
97
157
  public
98
158
 
99
159
  def follow
@@ -102,17 +162,22 @@ module Twat
102
162
  # occasionally :/
103
163
  twitter_auth
104
164
  failcount = 0
165
+ @tweetstack = TweetStack.new
105
166
 
106
167
  # Get 5 tweets
107
168
  tweets = Twitter.home_timeline(:count => 5)
108
169
  while true do
109
170
  begin
110
171
  last_id = process_followed(tweets) if tweets.any?
111
- sleep config.polling_interval
172
+ config.polling_interval.times do
173
+ handle_input(STDIN.read_nonblock(128).chop) rescue nil
174
+ sleep 1
175
+ end
112
176
  tweets = Twitter.home_timeline(:since_id => last_id)
113
177
  failcount = 0
114
- rescue Twitter::ServiceUnavailable
115
178
  rescue Interrupt
179
+ break
180
+ rescue Twitter::ServiceUnavailable
116
181
  if failcount > 2
117
182
  puts "3 consecutive failures, giving up"
118
183
  else
@@ -155,10 +220,16 @@ module Twat
155
220
 
156
221
  private
157
222
 
223
+ def pad(n)
224
+ "%02d" % n
225
+ end
226
+
158
227
  # Format a tweet all pretty like
159
- def format(twt)
228
+ def format(twt, idx = nil)
229
+ idx = pad(idx) if idx
160
230
  text = deentitize(twt.text)
161
231
  if config.colors?
232
+ print "#{idx.cyan}:" if idx
162
233
  if twt.user.screen_name == config.account_name.to_s
163
234
  puts "#{twt.user.screen_name.bold.blue}: #{text}"
164
235
  elsif text.mentions?(config.account_name)
@@ -167,6 +238,7 @@ module Twat
167
238
  puts "#{twt.user.screen_name.bold.cyan}: #{text}"
168
239
  end
169
240
  else
241
+ print "#{idx}:" if idx
170
242
  puts "#{twt.user.screen_name}: #{text}"
171
243
  end
172
244
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.2
4
+ version: 0.5.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-25 00:00:00.000000000Z
12
+ date: 2012-02-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: twitter
16
- requirement: &26440540 !ruby/object:Gem::Requirement
16
+ requirement: &15845540 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *26440540
24
+ version_requirements: *15845540
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: oauth
27
- requirement: &26439880 !ruby/object:Gem::Requirement
27
+ requirement: &15858800 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *26439880
35
+ version_requirements: *15858800
36
36
  description: Command line tool for tweeting and whatnot
37
37
  email:
38
38
  - richo@psych0tik.net
@@ -86,4 +86,3 @@ signing_key:
86
86
  specification_version: 3
87
87
  summary: Command line tool for tweeting and whatnot
88
88
  test_files: []
89
- has_rdoc: