Module: Termtter::Client
- Defined in:
- lib/termtter.rb,
lib/termtter.rb,
lib/plugin/sl.rb,
lib/filter/fib.rb,
lib/plugin/erb.rb,
lib/plugin/fib.rb,
lib/plugin/log.rb,
lib/plugin/say.rb,
lib/plugin/bomb.rb,
lib/plugin/spam.rb,
lib/filter/yhara.rb,
lib/plugin/group.rb,
lib/plugin/shell.rb,
lib/plugin/yhara.rb,
lib/plugin/filter.rb,
lib/plugin/follow.rb,
lib/plugin/plugin.rb,
lib/plugin/reload.rb,
lib/filter/reverse.rb,
lib/plugin/confirm.rb,
lib/plugin/history.rb,
lib/plugin/favorite.rb,
lib/plugin/uri-open.rb,
lib/plugin/quicklook.rb,
lib/filter/expand-tinyurl.rb,
lib/plugin/standard_plugins.rb
Constant Summary collapse
- @@hooks =
[]
- @@commands =
{}
- @@completions =
[]
- @@filters =
[]
- @@helps =
[]
Class Method Summary collapse
- .add_command(regex, &block) ⇒ Object
- .add_completion(&completion) ⇒ Object
- .add_filter(&filter) ⇒ Object
- .add_help(name, desc) ⇒ Object
- .add_hook(&hook) ⇒ Object
- .add_macro(r, s) ⇒ Object
-
.apply_filters(statuses) ⇒ Object
memo: each filter must return Array of Status.
- .call_commands(text, tw) ⇒ Object
- .call_hooks(statuses, event, tw) ⇒ Object
- .clear_commands ⇒ Object
- .clear_completions ⇒ Object
- .clear_filters ⇒ Object
- .clear_helps ⇒ Object
- .clear_hooks ⇒ Object
- .create_input_thread(tw) ⇒ Object
- .exit ⇒ Object
- .find_filter_candidates(a, b, filters) ⇒ Object
- .find_group_candidates(a, b) ⇒ Object
- .find_plugin_candidates(a, b) ⇒ Object
- .find_status_id_candidates(a, b) ⇒ Object
- .find_user_candidates(a, b) ⇒ Object
- .formatted_help ⇒ Object
- .load_history ⇒ Object
- .open_uri(uri) ⇒ Object
- .pause ⇒ Object
- .public_storage ⇒ Object
- .resume ⇒ Object
- .run ⇒ Object
- .save_history ⇒ Object
Class Method Details
.add_command(regex, &block) ⇒ Object
250 251 252 |
# File 'lib/termtter.rb', line 250 def add_command(regex, &block) @@commands[regex] = block end |
.add_completion(&completion) ⇒ Object
264 265 266 |
# File 'lib/termtter.rb', line 264 def add_completion(&completion) @@completions << completion end |
.add_filter(&filter) ⇒ Object
280 281 282 |
# File 'lib/termtter.rb', line 280 def add_filter(&filter) @@filters << filter end |
.add_help(name, desc) ⇒ Object
272 273 274 |
# File 'lib/termtter.rb', line 272 def add_help(name, desc) @@helps << [name, desc] end |
.add_hook(&hook) ⇒ Object
242 243 244 |
# File 'lib/termtter.rb', line 242 def add_hook(&hook) @@hooks << hook end |
.add_macro(r, s) ⇒ Object
254 255 256 257 258 |
# File 'lib/termtter.rb', line 254 def add_macro(r, s) add_command(r) do |m, t| call_commands(s % m, t) end end |
.apply_filters(statuses) ⇒ Object
memo: each filter must return Array of Status
289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/termtter.rb', line 289 def apply_filters(statuses) filtered = statuses @@filters.each do |f| filtered = f.call(filtered) end filtered rescue => e puts "Error: #{e}" puts e.backtrace.join("\n") statuses end |
.call_commands(text, tw) ⇒ Object
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/termtter.rb', line 320 def call_commands(text, tw) return if text.empty? command_found = false @@commands.each do |key, command| if key =~ text command_found = true begin command.call($~, tw) rescue => e puts "Error: #{e}" puts e.backtrace.join("\n") end end end raise CommandNotFound unless command_found end |
.call_hooks(statuses, event, tw) ⇒ Object
308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/termtter.rb', line 308 def call_hooks(statuses, event, tw) statuses = apply_filters(statuses) @@hooks.each do |h| begin h.call(statuses.dup, event, tw) rescue => e puts "Error: #{e}" puts e.backtrace.join("\n") end end end |
.clear_commands ⇒ Object
260 261 262 |
# File 'lib/termtter.rb', line 260 def clear_commands @@commands.clear end |
.clear_completions ⇒ Object
268 269 270 |
# File 'lib/termtter.rb', line 268 def clear_completions @@completions.clear end |
.clear_filters ⇒ Object
284 285 286 |
# File 'lib/termtter.rb', line 284 def clear_filters @@filters.clear end |
.clear_helps ⇒ Object
276 277 278 |
# File 'lib/termtter.rb', line 276 def clear_helps @@helps.clear end |
.clear_hooks ⇒ Object
246 247 248 |
# File 'lib/termtter.rb', line 246 def clear_hooks @@hooks.clear end |
.create_input_thread(tw) ⇒ Object
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 |
# File 'lib/termtter.rb', line 414 def create_input_thread(tw) Thread.new do erb = ERB.new(configatron.prompt) while buf = Readline.readline(erb.result(tw.__send__(:binding)), true) begin call_commands(buf, tw) rescue CommandNotFound => e puts "Unknown command \"#{buf}\"" puts 'Enter "help" for instructions' rescue => e puts "Error: #{e}" puts e.backtrace.join("\n") end end end end |
.exit ⇒ Object
348 349 350 351 352 353 |
# File 'lib/termtter.rb', line 348 def exit call_hooks([], :exit, nil) @@main_thread.kill @@update_thread.kill @@input_thread.kill end |
.find_filter_candidates(a, b, filters) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/plugin/filter.rb', line 33 def self.find_filter_candidates(a, b, filters) if a.empty? filters.to_a else filters.grep(/^#{Regexp.quote a}/i) end. map {|u| b % u } end |
.find_group_candidates(a, b) ⇒ Object
16 17 18 19 20 |
# File 'lib/plugin/group.rb', line 16 def self.find_group_candidates(a, b) configatron.plugins.group.groups.keys.map {|k| k.to_s}. grep(/^#{Regexp.quote a}/). map {|u| b % u } end |
.find_plugin_candidates(a, b) ⇒ Object
22 23 24 25 26 |
# File 'lib/plugin/plugin.rb', line 22 def self.find_plugin_candidates(a, b) public_storage[:plugins]. grep(/^#{Regexp.quote a}/i). map {|u| b % u } end |
.find_status_id_candidates(a, b) ⇒ Object
117 118 119 120 121 122 123 124 |
# File 'lib/plugin/standard_plugins.rb', line 117 def self.find_status_id_candidates(a, b) if a.empty? public_storage[:status_ids].to_a else public_storage[:status_ids].grep(/#{Regexp.quote a}/) end. map {|u| b % u } end |
.find_user_candidates(a, b) ⇒ Object
126 127 128 129 130 131 132 133 |
# File 'lib/plugin/standard_plugins.rb', line 126 def self.find_user_candidates(a, b) if a.empty? public_storage[:users].to_a else public_storage[:users].grep(/^#{Regexp.quote a}/i) end. map {|u| b % u } end |
.formatted_help ⇒ Object
93 94 95 96 97 98 99 100 |
# File 'lib/plugin/standard_plugins.rb', line 93 def self.formatted_help width = @@helps.map {|n, d| n.size }.max space = 3 "\nuser commands:\n" + @@helps.map {|name, desc| name.to_s.ljust(width + space) + desc.to_s }.join("\n") end |
.load_history ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/plugin/history.rb', line 9 def self.load_history filename = File.(configatron.plugins.history.filename) keys = configatron.plugins.history.keys if File.exist?(filename) begin history = Marshal.load Zlib::Inflate.inflate(File.read(filename)) rescue Zlib::DataError history = Marshal.load File.read(filename) end if history keys.each do |key| public_storage[key] = history[key] if history[key] end puts "history loaded(#{File.size(filename)}bytes)" end end end |
.open_uri(uri) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/plugin/uri-open.rb', line 12 def self.open_uri(uri) unless configatron.plugins.uri_open.browser.nil? system configatron.plugins.uri_open.browser, uri else case RUBY_PLATFORM when /linux/ system 'firefox', uri when /mswin(?!ce)|mingw|bccwin/ system 'explorer', uri else system 'open', uri end end end |
.pause ⇒ Object
339 340 341 |
# File 'lib/termtter.rb', line 339 def pause @@pause = true end |
.public_storage ⇒ Object
43 44 45 |
# File 'lib/termtter.rb', line 43 def self.public_storage @@public_storage ||= {} end |
.resume ⇒ Object
343 344 345 346 |
# File 'lib/termtter.rb', line 343 def resume @@pause = false @@update_thread.run end |
.run ⇒ Object
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 |
# File 'lib/termtter.rb', line 355 def run puts 'initializing...' initialized = false @@pause = false tw = Termtter::Twitter.new(configatron.user_name, configatron.password) call_hooks([], :initialize, tw) @@input_thread = nil @@update_thread = Thread.new do since_id = nil loop do begin Thread.stop if @@pause statuses = tw.get_friends_timeline(since_id) unless statuses.empty? since_id = statuses[0].id end print "\e[1K\e[0G" if !statuses.empty? && !win? call_hooks(statuses, :update_friends_timeline, tw) initialized = true @@input_thread.kill if @@input_thread && !statuses.empty? rescue OpenURI::HTTPError => e if e. == '401 Unauthorized' puts 'Could not login' puts 'plese check your account settings' exit! end rescue => e puts "Error: #{e}" puts e.backtrace.join("\n") ensure sleep configatron.update_interval end end end until initialized; end vi_or_emacs = configatron.editing_mode unless vi_or_emacs.empty? Readline.__send__("#{vi_or_emacs}_editing_mode") end begin stty_save = `stty -g`.chomp trap("INT") { system "stty", stty_save; exit } rescue Errno::ENOENT end @@main_thread = Thread.new do loop do @@input_thread = create_input_thread(tw) @@input_thread.join end end @@main_thread.join end |
.save_history ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/plugin/history.rb', line 28 def self.save_history filename = File.(configatron.plugins.history.filename) keys = configatron.plugins.history.keys history = { } keys.each do |key| history[key] = public_storage[key] end File.open(filename, 'w') do |f| f.write Zlib::Deflate.deflate(Marshal.dump(history)) end puts "history saved(#{File.size(filename)}bytes)" end |