Blogged by Ujihisa. Standard methods of programming and thoughts including Clojure, Vim, LLVM, Haskell, Ruby and Mathematics written by a Japanese programmer. github/ujihisa

Wednesday, May 13, 2009

Fixed uri-open of Termtter

Termtter's uri-open plugin

I committed some to Termtter. The following diff is the summary of my patches today.

diff --git a/lib/plugins/uri-open.rb b/lib/plugins/uri-open.rb
index 0afe73f..06a521b 100644
--- a/lib/plugins/uri-open.rb
+++ b/lib/plugins/uri-open.rb
@@ -8,44 +8,43 @@ module Termtter::Client
     :points => [:output],
     :exec_proc => lambda {|statuses, event|
       statuses.each do |s|
-        public_storage[:uris] += s[:text].scan(%r|https?://[^\s]+|)
+        public_storage[:uris] = s[:text].scan(%r|https?://[^\s]+|) + public_storage[:uris]
       end
     }
   )

   def self.open_uri(uri)
-    unless config.plugins.uri_open.browser.empty?
-      system config.plugins.uri_open.browser, uri
-    else
-      case RUBY_PLATFORM
-      when /linux/
-        system 'firefox', uri
-      when /mswin(?!ce)|mingw|bccwin/
-        system 'explorer', uri
+    cmd =
+      unless config.plugins.uri_open.browser.empty?
+        config.plugins.uri_open.browser
       else
-        system 'open', uri
+        case RUBY_PLATFORM
+        when /linux/; 'firefox'
+        when /mswin(?!ce)|mingw|bccwin/; 'explorer'
+        else; 'open'
+        end
       end
-    end
+    system cmd, uri
   end

   register_command(
     :name => :'uri-open', :aliases => [:uo],
-    :exec_proc => lambda{|arg|
+    :exec_proc => lambda {|arg|
       case arg
-      when /^\s+$/
-        public_storage[:uris].each do |uri|
-          open_uri(uri)
-        end
-        public_storage[:uris].clear
+      when ''
+        open_uri public_storage[:uris].shift
       when /^\s*all\s*$/
-        public_storage[:uris].each do |uri|
-          open_uri(uri)
-        end
-        public_storage[:uris].clear
+        public_storage[:uris].
+          each {|uri| open_uri(uri) }.
+          clear
       when /^\s*list\s*$/
-        public_storage[:uris].each_with_index do |uri, index|
-          puts "#{index}: #{uri}"
-        end
+        public_storage[:uris].
+          enum_for(:each_with_index).
+          to_a.
+          reverse.
+          each  do |uri, index|
+            puts "#{index}: #{uri}"
+          end
       when /^\s*delete\s+(\d+)\s*$/
         puts 'delete'
         public_storage[:uris].delete_at($1.to_i)
@@ -53,19 +52,20 @@ module Termtter::Client
         public_storage[:uris].clear
         puts "clear uris"
       when /^\s*(\d+)\s*$/
-        open_uri(public_storage[:uris][$1.to_i])
-        public_storage[:uris].delete_at($1.to_i)
+        open_uri(public_storage[:uris].delete_at($1.to_i))
+      else
+        puts "**parse error in uri-open**"
       end
     },
-    :completion_proc => lambda{|cmd, arg|
-      %w(all list delete clear).grep(/^#{Regexp.quote arg}/).map{|a| "#{cmd} #{a}"}
+    :completion_proc => lambda {|cmd, arg|
+      %w(all list delete clear).grep(/^#{Regexp.quote arg}/).map {|a| "#{cmd} #{a}" }
     }
   )
 end
-# ~/.termtter
+# ~/.termtter/config
 # plugin 'uri-open'
 #
 # see also: http://ujihisa.nowa.jp/entry/c3dd00c4e0
 #
 # KNOWN BUG
-# * In Debian, exit or C-c in the termtter kills your firefox.
+# * In Debian, exit or C-c in the termtter would kill your firefox.

No comments:

Post a Comment

Followers