jugyo-termtter 0.5.3 → 0.5.4

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest.txt CHANGED
@@ -7,14 +7,14 @@ run_termtter.rb
7
7
  example_dot_termtter
8
8
  bin/termtter
9
9
  lib/termtter.rb
10
- lib/termtter/stdout.rb
11
- lib/termtter/notify-send.rb
12
- lib/termtter/standard_commands.rb
13
- lib/termtter/uri-open.rb
14
- lib/termtter/growl.rb
15
- lib/termtter/say.rb
16
- lib/termtter/english.rb
17
- lib/termtter/completion.rb
10
+ lib/plugin/stdout.rb
11
+ lib/plugin/notify-send.rb
12
+ lib/plugin/standard_commands.rb
13
+ lib/plugin/uri-open.rb
14
+ lib/plugin/growl.rb
15
+ lib/plugin/say.rb
16
+ lib/plugin/english.rb
17
+ lib/plugin/completion.rb
18
18
  test/test_termtter.rb
19
19
  test/friends_timeline.json
20
20
  test/search.json
data/bin/termtter CHANGED
@@ -4,9 +4,9 @@ $KCODE = 'u'
4
4
 
5
5
  require 'rubygems'
6
6
  require 'termtter'
7
- require 'termtter/standard_commands'
8
- require 'termtter/stdout'
9
7
  require 'configatron'
8
+ plugin 'standard_commands'
9
+ plugin 'stdout'
10
10
 
11
11
  conf_file = File.expand_path('~/.termtter')
12
12
  if File.exist? conf_file
data/example_dot_termtter CHANGED
@@ -1,9 +1,9 @@
1
- #require 'termtter/english'
2
- #require 'termtter/say'
3
- #require 'termtter/notify-send'
4
- #require 'termtter/uri-open'
5
- #require 'termtter/growl'
6
- #require 'termtter/completion'
1
+ #require 'plugin/english'
2
+ #require 'plugin/say'
3
+ #require 'plugin/notify-send'
4
+ #require 'plugin/uri-open'
5
+ #require 'plugin/growl'
6
+ #require 'plugin/completion'
7
7
 
8
8
  configatron.user_name = 'USERNAME'
9
9
  configatron.password = 'PASSWORD'
data/lib/termtter.rb CHANGED
@@ -12,8 +12,12 @@ $:.unshift(File.dirname(__FILE__)) unless
12
12
 
13
13
  configatron.set_default(:update_interval, 300)
14
14
 
15
+ def plugin(s)
16
+ require "plugin/#{s}"
17
+ end
18
+
15
19
  module Termtter
16
- VERSION = '0.5.3'
20
+ VERSION = '0.5.4'
17
21
 
18
22
  class Twitter
19
23
 
@@ -65,20 +69,15 @@ module Termtter
65
69
 
66
70
  def get_timeline(uri)
67
71
  data = JSON.parse(open(uri, :http_basic_authentication => [@user_name, @password]).read)
68
- data = if data.instance_of? Array
69
- data
70
- else
71
- [data]
72
- end
72
+ data = [data] unless data.instance_of? Array
73
73
  return data.map do |s|
74
- u = s["user"]
75
74
  status = Status.new
76
75
  status.created_at = Time.utc(*ParseDate::parsedate(s["created_at"])).localtime
77
76
  %w(id text truncated in_reply_to_status_id in_reply_to_user_id).each do |key|
78
77
  status.__send__("#{key}=".to_sym, s[key])
79
78
  end
80
79
  %w(id name screen_name url profile_image_url).each do |key|
81
- status.__send__("user_#{key}=".to_sym, u[key])
80
+ status.__send__("user_#{key}=".to_sym, s["user"][key])
82
81
  end
83
82
  status
84
83
  end
@@ -90,47 +89,32 @@ module Termtter
90
89
  @@hooks = []
91
90
  @@commands = {}
92
91
 
93
- def self.add_hook(&hook)
94
- @@hooks << hook
95
- end
96
-
97
- def self.clear_hooks
98
- @@hooks.clear
99
- end
100
-
101
- def self.add_command(regex, &block)
102
- @@commands[regex] = block
103
- end
92
+ class << self
93
+ def add_hook(&hook)
94
+ @@hooks << hook
95
+ end
104
96
 
105
- def self.clear_commands
106
- @@commands.clear
107
- end
97
+ def clear_hooks
98
+ @@hooks.clear
99
+ end
108
100
 
109
- def self.public_storage
110
- @@public_storage ||= {}
111
- return @@public_storage
112
- end
101
+ def add_command(regex, &block)
102
+ @@commands[regex] = block
103
+ end
113
104
 
114
- def self.call_hooks(statuses, event, tw)
115
- @@hooks.each do |h|
116
- begin
117
- h.call(statuses.dup, event, tw)
118
- rescue => e
119
- puts "Error: #{e}"
120
- puts e.backtrace.join("\n")
121
- end
105
+ def clear_commands
106
+ @@commands.clear
122
107
  end
123
- end
124
108
 
125
- def self.call_commands(text, tw)
126
- return if text.empty?
109
+ def public_storage
110
+ @@public_storage ||= {}
111
+ return @@public_storage
112
+ end
127
113
 
128
- command_found = false
129
- @@commands.each do |key, command|
130
- if key =~ text
131
- command_found = true
114
+ def call_hooks(statuses, event, tw)
115
+ @@hooks.each do |h|
132
116
  begin
133
- command.call($~, tw)
117
+ h.call(statuses.dup, event, tw)
134
118
  rescue => e
135
119
  puts "Error: #{e}"
136
120
  puts e.backtrace.join("\n")
@@ -138,68 +122,84 @@ module Termtter
138
122
  end
139
123
  end
140
124
 
141
- raise CommandNotFound unless command_found
142
- end
125
+ def call_commands(text, tw)
126
+ return if text.empty?
127
+
128
+ command_found = false
129
+ @@commands.each do |key, command|
130
+ if key =~ text
131
+ command_found = true
132
+ begin
133
+ command.call($~, tw)
134
+ rescue => e
135
+ puts "Error: #{e}"
136
+ puts e.backtrace.join("\n")
137
+ end
138
+ end
139
+ end
143
140
 
144
- def self.pause
145
- @@pause = true
146
- end
141
+ raise CommandNotFound unless command_found
142
+ end
147
143
 
148
- def self.resume
149
- @@pause = false
150
- @@update_thread.run
151
- end
144
+ def pause
145
+ @@pause = true
146
+ end
152
147
 
153
- def self.exit
154
- @@update_thread.kill
155
- @@input_thread.kill
156
- end
148
+ def resume
149
+ @@pause = false
150
+ @@update_thread.run
151
+ end
157
152
 
158
- def self.run
159
- @@pause = false
160
- tw = Termtter::Twitter.new(configatron.user_name, configatron.password)
153
+ def exit
154
+ @@update_thread.kill
155
+ @@input_thread.kill
156
+ end
161
157
 
162
- @@update_thread = Thread.new do
163
- since_id = nil
164
- loop do
165
- begin
166
- Thread.stop if @@pause
158
+ def run
159
+ @@pause = false
160
+ tw = Termtter::Twitter.new(configatron.user_name, configatron.password)
167
161
 
168
- statuses = tw.get_friends_timeline(since_id)
169
- unless statuses.empty?
170
- since_id = statuses[0].id
171
- end
172
- call_hooks(statuses, :update_friends_timeline, tw)
162
+ @@update_thread = Thread.new do
163
+ since_id = nil
164
+ loop do
165
+ begin
166
+ Thread.stop if @@pause
173
167
 
174
- rescue => e
175
- puts "Error: #{e}"
176
- puts e.backtrace.join("\n")
177
- ensure
178
- sleep configatron.update_interval
168
+ statuses = tw.get_friends_timeline(since_id)
169
+ unless statuses.empty?
170
+ since_id = statuses[0].id
171
+ end
172
+ call_hooks(statuses, :update_friends_timeline, tw)
173
+
174
+ rescue => e
175
+ puts "Error: #{e}"
176
+ puts e.backtrace.join("\n")
177
+ ensure
178
+ sleep configatron.update_interval
179
+ end
179
180
  end
180
181
  end
181
- end
182
182
 
183
- @@input_thread = Thread.new do
184
- while buf = Readline.readline("", true)
185
- begin
186
- call_commands(buf, tw)
187
- rescue CommandNotFound => e
188
- puts "Unknown command \"#{buf}\""
189
- puts 'Enter "help" for instructions'
190
- rescue => e
191
- puts "Error: #{e}"
192
- puts e.backtrace.join("\n")
183
+ @@input_thread = Thread.new do
184
+ while buf = Readline.readline("", true)
185
+ begin
186
+ call_commands(buf, tw)
187
+ rescue CommandNotFound => e
188
+ puts "Unknown command \"#{buf}\""
189
+ puts 'Enter "help" for instructions'
190
+ rescue => e
191
+ puts "Error: #{e}"
192
+ puts e.backtrace.join("\n")
193
+ end
193
194
  end
194
195
  end
195
- end
196
196
 
197
- stty_save = `stty -g`.chomp
198
- trap("INT") { system "stty", stty_save; exit }
197
+ stty_save = `stty -g`.chomp
198
+ trap("INT") { system "stty", stty_save; exit }
199
199
 
200
- @@input_thread.join
200
+ @@input_thread.join
201
+ end
201
202
  end
202
-
203
203
  end
204
204
 
205
205
  class CommandNotFound < StandardError; end
data/run_termtter.rb CHANGED
@@ -11,8 +11,8 @@ self_file =
11
11
  $:.unshift(File.dirname(self_file) + "/lib")
12
12
 
13
13
  require 'termtter'
14
- require 'termtter/standard_commands'
15
- require 'termtter/stdout'
14
+ plugin 'standard_commands'
15
+ plugin 'stdout'
16
16
 
17
17
  configatron.update_interval
18
18
 
@@ -26,4 +26,4 @@ end
26
26
 
27
27
  Termtter::Client.run
28
28
 
29
- # Startup scripts for development
29
+ # Startup scripts for development
metadata CHANGED
@@ -1,10 +1,11 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jugyo-termtter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.3
4
+ version: 0.5.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - jugyo
8
+ - ujihisa
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
@@ -1,58 +0,0 @@
1
- require 'set'
2
-
3
- module Termtter::Client
4
-
5
- public_storage[:users] ||= Set.new
6
-
7
- add_hook do |statuses, event, t|
8
- if !statuses.empty?
9
- case event
10
- when :update_friends_timeline, :replies, :list_user_timeline
11
- statuses.each do |s|
12
- public_storage[:users].add(s.user_screen_name)
13
- s.text.scan(/@[a-zA-Z_0-9]*/).each do |u| # reply
14
- public_storage[:users].add(u.gsub("@","")) unless u == "@"
15
- end
16
- end
17
- end
18
- end
19
- end
20
-
21
- end
22
-
23
- module Termtter
24
- module InputCompletor
25
-
26
- Commands = %w[exit help list pause update resume replies search show uri-open]
27
-
28
- def self.find_candidates(a, b)
29
- if a.empty?
30
- Termtter::Client.public_storage[:users].to_a
31
- else
32
- Termtter::Client.public_storage[:users].
33
- grep(/^#{Regexp.quote a}/i).map {|u| b % u }
34
- end
35
- end
36
-
37
- CompletionProc = proc {|input|
38
- case input
39
- when /^l(ist)? +(.*)/
40
- find_candidates $2, "list %s"
41
- when /^(update|u)\s+(.*)@([^\s]*)$/
42
- find_candidates $3, "#{$1} #{$2}@%s"
43
- when /^uri-open +(.*)/
44
- find_candidates $1, "uri-open %s"
45
- else
46
- Commands.grep(/^#{Regexp.quote input}/)
47
- end
48
- }
49
-
50
- end
51
- end
52
-
53
- Readline.basic_word_break_characters= "\t\n\"\\'`><=;|&{("
54
- Readline.completion_proc = Termtter::InputCompletor::CompletionProc
55
-
56
- # author: bubbles
57
- #
58
- # see also: http://d.hatena.ne.jp/bubbles/20090105/1231145823
@@ -1,51 +0,0 @@
1
- Termtter::Client.clear_hooks # FIXME: not to clear all but to clear just stdout.rb
2
-
3
- # english? :: String -> Boolean
4
- def english?(message)
5
- /[一-龠]+|[ぁ-ん]+|[ァ-ヴー]+|[a-zA-Z0-9]+/ !~ message
6
- end
7
-
8
- # FIXME: The code below is a copy from stdout.rb so it's not DRY. DRY it.
9
- Termtter::Client.add_hook do |statuses, event|
10
- colors = %w(0 31 32 33 34 35 36 91 92 93 94 95 96)
11
-
12
- case event
13
- when :update_friends_timeline, :list_friends_timeline, :list_user_timeline, :show, :replies
14
- unless statuses.empty?
15
- if event == :update_friends_timeline then statuses = statuses.reverse end
16
- statuses.each do |s|
17
- text = s.text.gsub("\n", '')
18
- next unless english?(text) # if you substitute "if" for "unless", this script will be "japanese.rb"
19
- color_num = colors[s.user_screen_name.hash % colors.size]
20
- status = "#{s.user_screen_name}: #{text}"
21
- if s.in_reply_to_status_id
22
- status += " (reply to #{s.in_reply_to_status_id})"
23
- end
24
-
25
- case event
26
- when :update_friends_timeline, :list_friends_timeline
27
- time_format = '%H:%M:%S'
28
- else
29
- time_format = '%m-%d %H:%d'
30
- end
31
- time_str = "(#{s.created_at.strftime(time_format)})"
32
-
33
- puts "#{color(time_str, 90)} #{color(status, color_num)}"
34
- end
35
- end
36
- when :search
37
- statuses.each do |s|
38
- text = s.text.gsub("\n", '')
39
- color_num = colors[s.user_screen_name.hash % colors.size]
40
- status = "#{s.user_screen_name}: #{text}"
41
- time_str = "(#{s.created_at.strftime('%m-%d %H:%d')})"
42
-
43
- puts "#{color(time_str, 90)} #{color(status, color_num)}"
44
- end
45
- end
46
- end
47
-
48
- # USAGE:
49
- # Write the line on the *first line* of your ~/.termtter
50
- # require 'termtter/english'
51
- # (english.rb will destroy plugins which were required before)
@@ -1,7 +0,0 @@
1
- Termtter::Client.add_hook do |statuses, event|
2
- if !statuses.empty? && event == :update_friends_timeline
3
- statuses.each do |s|
4
- system 'growlnotify', 'Termtter', '-m', "#{s.user_screen_name}: #{s.text}", '-n', 'termtter'
5
- end
6
- end
7
- end
@@ -1,16 +0,0 @@
1
- Termtter::Client.add_hook do |statuses, event|
2
- if !statuses.empty? && event == :update_friends_timeline
3
- max = 10
4
-
5
- text = statuses[0..(max - 1)].map{|s|
6
- status_text = CGI.escapeHTML(s.text)
7
- status_text.gsub!(%r{https?://[-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+},'<a href="\0">\0</a>')
8
- status_text.gsub!("\n", '')
9
- "<b>#{s.user_screen_name}:</b> <span font=\"9.0\">#{status_text}</span>"
10
- }.join("\n")
11
-
12
- text += "\n<a href=\"http://twitter.com/\">more…</a>" if statuses.size > max
13
-
14
- system 'notify-send', 'Termtter', text, '-t', '60000'
15
- end
16
- end
data/lib/termtter/say.rb DELETED
@@ -1,22 +0,0 @@
1
- raise 'say.rb runs only in OSX Leopard' if /darwin9/ !~ RUBY_PLATFORM
2
-
3
- # say :: String -> String -> IO ()
4
- def say(who, what)
5
- voices = %w(Alex Bruce Fred Junior Ralph Agnes Kathy Princess Vicki Victoria Albert Bad\ News Bahh Bells Boing Bubbles Cellos Deranged Good\ News Hysterical Pipe\ Organ Trinoids Whisper Zarvox)
6
- voice = voices[who.hash % voices.size]
7
- system 'say', '-v', voice, what
8
- end
9
-
10
- module Termtter::Client
11
- add_hook do |statuses, event, t|
12
- if !statuses.empty? && event == :update_friends_timeline
13
- statuses.reverse.each do |s|
14
- text_without_uri = s.text.gsub(%r|https?://[^\s]+|, 'U.R.I.')
15
- say s.user_screen_name, text_without_uri
16
- end
17
- end
18
- end
19
- end
20
-
21
- # KNOWN BUG:
22
- # * exit or <C-c> doen't work quickly.
@@ -1,69 +0,0 @@
1
- module Termtter::Client
2
-
3
- add_command /^(update|u)\s+(.*)/ do |m, t|
4
- text = m[2]
5
- unless text.empty?
6
- t.update_status(text)
7
- puts "=> #{text}"
8
- end
9
- end
10
-
11
- add_command /^(list|l)\s*$/ do |m, t|
12
- statuses = t.get_friends_timeline()
13
- call_hooks(statuses, :list_friends_timeline, t)
14
- end
15
-
16
- add_command /^(list|l)\s+([^\s]+)/ do |m, t|
17
- statuses = t.get_user_timeline(m[2])
18
- call_hooks(statuses, :list_user_timeline, t)
19
- end
20
-
21
- add_command /^(search|s)\s+(.+)/ do |m, t|
22
- call_hooks(t.search(m[2]), :search, t)
23
- end
24
-
25
- add_command /^(replies|r)\s*$/ do |m, t|
26
- call_hooks(t.replies(), :replies, t)
27
- end
28
-
29
- add_command /^show\s+([^\s]+)/ do |m, t|
30
- call_hooks(t.show(m[1]), :show, t)
31
- end
32
-
33
- add_command /^pause\s*$/ do |m, t|
34
- pause
35
- end
36
-
37
- add_command /^resume\s*$/ do |m, t|
38
- resume
39
- end
40
-
41
- add_command /^exit\s*$/ do |m, t|
42
- exit
43
- end
44
-
45
- add_command /^help\s*$/ do |m, t|
46
- puts <<-EOS
47
- exit Exit
48
- help Print this help message
49
- list,l List the posts in your friends timeline
50
- list,l USERNAME List the posts in the the given user's timeline
51
- pause Pause updating
52
- update,u TEXT Post a new message
53
- resume Resume updating
54
- replies,r List the most recent @replies for the authenticating user
55
- search,s TEXT Search for Twitter
56
- show ID Show a single status
57
- EOS
58
- end
59
-
60
- add_command /^eval\s+(.*)$/ do |m, t|
61
- begin
62
- result = eval(m[1]) unless m[1].empty?
63
- puts "=> #{result.inspect}"
64
- rescue SyntaxError => e
65
- puts e
66
- end
67
- end
68
-
69
- end
@@ -1,41 +0,0 @@
1
- def color(str, num)
2
- "\e[#{num}m#{str}\e[0m"
3
- end
4
-
5
- Termtter::Client.add_hook do |statuses, event|
6
- colors = %w(0 31 32 33 34 35 36 91 92 93 94 95 96)
7
-
8
- case event
9
- when :update_friends_timeline, :list_friends_timeline, :list_user_timeline, :show, :replies
10
- unless statuses.empty?
11
- if event == :update_friends_timeline then statuses = statuses.reverse end
12
- statuses.each do |s|
13
- text = s.text.gsub("\n", '')
14
- color_num = colors[s.user_screen_name.hash % colors.size]
15
- status = "#{s.user_screen_name}: #{text}"
16
- if s.in_reply_to_status_id
17
- status += " (reply to #{s.in_reply_to_status_id})"
18
- end
19
-
20
- case event
21
- when :update_friends_timeline, :list_friends_timeline
22
- time_format = '%H:%M:%S'
23
- else
24
- time_format = '%m-%d %H:%d'
25
- end
26
- time_str = "(#{s.created_at.strftime(time_format)})"
27
-
28
- puts "#{color(time_str, 90)} #{color(status, color_num)}"
29
- end
30
- end
31
- when :search
32
- statuses.each do |s|
33
- text = s.text.gsub("\n", '')
34
- color_num = colors[s.user_screen_name.hash % colors.size]
35
- status = "#{s.user_screen_name}: #{text}"
36
- time_str = "(#{s.created_at.strftime('%m-%d %H:%d')})"
37
-
38
- puts "#{color(time_str, 90)} #{color(status, color_num)}"
39
- end
40
- end
41
- end
@@ -1,39 +0,0 @@
1
- module Termtter::Client
2
- public_storage[:uris] = []
3
-
4
- add_hook do |statuses, event, t|
5
- if !statuses.empty? && event == :update_friends_timeline
6
- statuses.each do |s|
7
- public_storage[:uris] += s.text.scan(%r|https?://[^\s]+|)
8
- end
9
- end
10
- end
11
-
12
- add_command /^uri-open\s*$/ do |m, t|
13
- public_storage[:uris].each do |uri|
14
- # FIXME: works only in OSX and other *NIXs
15
- if /linux/ =~ RUBY_PLATFORM
16
- system 'firefox', uri
17
- else
18
- system 'open', uri
19
- end
20
- end
21
- public_storage[:uris].clear
22
- end
23
-
24
- add_command /^uri-open\s+list\s*$/ do |m, t|
25
- puts public_storage[:uris]
26
- end
27
-
28
- add_command /^uri-open\s+clear\s*$/ do |m, t|
29
- public_storage[:uris].clear
30
- puts "clear uris"
31
- end
32
- end
33
- # ~/.termtter
34
- # require 'termtter/uri-open'
35
- #
36
- # see also: http://ujihisa.nowa.jp/entry/c3dd00c4e0
37
- #
38
- # KNOWN BUG
39
- # * In Debian, exit or C-c in the termtter kills your firefox.