#3 ✓resolved
jugyo

新しいプラグインの仕組みを考える

Reported by jugyo | January 12th, 2009 @ 10:45 PM

今のプラグインの仕組みは若干柔軟性に欠ける部分があると思うので改善したい。 あと、プラグインの定義をもうちょっとわかりやすく書けるようにしたい。

現在 new_plugin_system というブランチにて開発中〜。

Comments and changes to this ticket

  • jugyo

    jugyo January 12th, 2009 @ 10:52 PM

    以下のような感じでコマンドを定義できるようにしようかなーと思ってます。

    command = Command.new(
              :names => ['update', 'u'],
              :pattern => /^(update|u)\s+(.*)/,
              :exec => proc {|match|
                # exec command
              },
              :completion => proc {|input|
                # return array of candidates
              },
              :help => 'test command'
            )
    
    
  • ujihisa
  • jugyo

    jugyo January 13th, 2009 @ 12:22 PM

    新しいコマンドの定義方法ですが、いろいろ考えた結果、以下のような感じになりそうです。

    コマンドの定義方法はこんな感じです。

    Termtter.Client.add_command(
      Termtter::Command.new(
        :name => :update,
        :exec_proc => proc{|arg| twitter.post(arg)}
      )
    )
    
    

    もしくは以下のようにして、必要なデータを渡してTermtter::Client 側で Command を new するようにしてもいいかもしれません(こっちのほうがよさそうだな)。追記: このやりかたでもいけるようになりました!

    Termtter.Client.add_command(
      :name => :update,
      :exec_proc => proc{|arg| twitter.post(arg)}
    )
    
    

    今の add_command との大きな違いは、コマンドの処理を行う proc が受け取る引数です。 上の例の「:exec => proc{ ... }」として指定しているのがそれで、arg にはコマンドの後ろにくっついている文字列がそのまま渡されます。 現状の MatchData を受け取るやりかたよりもこっちのほうが楽な気がしてます。

    あと、前みいに add_completion しなくても基本的な補完はできるようになっています。

    Termtter::Command のコンストラクタには、help や alias、completion なども引数として渡せます。

    Termtter::Command.new(
      :name => :update,
      :exec_proc => proc{|arg| twitter.post(arg)}
      :aliases => ['u']
      :help => ['update,u TEXT', 'update your status']
      :completion_proc => proc {|command, arg| ... }
    )
    
    

    既存のコマンドの動作を変更するのも簡単にできます。 以下のような感じで。

    Termtter::Client.get_command(:update).exec_proc =
        proc { |arg| twitter.update(arg + '!') }
    
    
  • mattn

    mattn January 14th, 2009 @ 09:02 AM

    ただ、これまでの様に正規表現で書かれている処理のメリットはあったかと思います。 例えば、同じコマンドでも引数があるか無いかで呼び出しを変えたり等...

  • jugyo

    jugyo January 14th, 2009 @ 10:05 AM

    それは確かにそうで、悩んだ所ではあるんですよねー。 ま、でも、複数の引数を取るコマンドってそんなにないかなーというのと、 できるだけ簡単にコマンドを追加できるようにしたかったのでこんな感じになりました。 Command オブジェクトのメソッドを再定義するかたちで同様のことはできそうな気がしてるので、調べてみます。

    追記

    exec_if_match というメソッドを再定義することで、 引数に応じて呼び出す処理を変えるということが一応可能になります。

    さらに追記: 当初予定していた実装とは若干変わってきてしまったので、下記のコードは現在はうまく機能しません。現在別の方法を検討中。

    
    command = Termtter::Command.new(:name => :update)
    
    class << command
      def exec_if_match(input)
        case input
        when /^update\s+foo\s*(.*)/
          foo($1)
        when /^update\s+bar\s*(.*)/
          bar($1)
        end
      end
      def foo(arg)
        ...
      end
      def bar(arg)
        ...
      end
    end
    

    どうですかね?

  • jugyo

    jugyo January 24th, 2009 @ 09:43 PM

    • Tag set to enhancement
    • State changed from “new” to “resolved”
    • Assigned user set to “jugyo”

    ちょっとタスクの粒度が大きすぎるので、もうちょっと細分化することにして、このタスクはクローズします。 とりあえずコマンドの登録に関しての諸々は決まったということで。

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Termtter is a terminal based Twitter client

People watching this ticket

Pages