percol で候補を選択するときにプレビュー的なことをしたいので、環境変数を設定しておくことで、そのコマンドを実行できるようにしてみました。
例えば、以下のようにすると候補を選択(移動?)するたびに通知が飛びます。
(%s
のところに選択された候補が入ります)
$ seq 1 100 | PERCOL_HOOK_ON_SELECT='notify-send -u low %s' percol
rc.py
↓を書いておきます。
def hack_hook_on_select(): import os hook_on_select = os.environ.get('PERCOL_HOOK_ON_SELECT') if hook_on_select is None: return from percol.model import SelectorModel o = SelectorModel.select_index def wrapped(self, idx): result = self.results[idx] if result is not None: try: seleted_text = result[0] import subprocess # import shlex # quoted_text = shlex.quote(seleted_text) # cmd = hook_on_select.replace('%s', quoted_text) cmd = hook_on_select.replace('%s', seleted_text) subprocess.check_output(['bash', '-c', cmd], stderr=subprocess.STDOUT) except Exception as e: pass return o(self, idx) SelectorModel.select_index = wrapped hack_hook_on_select()
… Python2 でエスケープしたいときどうするんだろ。 ライブラリか自前になっちゃう?
こんなハックじゃなくても出来たりして。