vimっぽいページスクロール(c-dとc-u)をpython-prompt-toolkitで実装 - Python
python-prompt-toolkitを使って、ページのスクロールを実装したからメモメモ
以下の2つだけとりあえず、実装してみる
handle('c-d')(scroll_half_page_down) handle('c-u')(scroll_half_page_up)
prompt_toolkit.key_binding.bindings.scroll
のscroll_half_page_down
とscroll_half_page_up
の2つだ!
from prompt_toolkit.application import Application from prompt_toolkit.key_binding import KeyBindings from prompt_toolkit.layout.layout import Layout from prompt_toolkit.buffer import Buffer from prompt_toolkit.layout.containers import Window from prompt_toolkit.layout.controls import BufferControl from prompt_toolkit.document import Document # これ from prompt_toolkit.key_binding.bindings.scroll import scroll_half_page_down, scroll_half_page_up def get_text(): return '\n'.join([chr(i) for i in range(65, 65+26)] * 3) body = Window(BufferControl(Buffer(document=Document(get_text(), cursor_position=0), read_only=True))) # キーバインディング kb = KeyBindings() # これ kb.add('c-d')(scroll_half_page_down) kb.add('c-u')(scroll_half_page_up) @kb.add('q') def _(event): event.app.exit() app = Application( layout=Layout(body), key_bindings=kb, full_screen=True) app.run()
できた!!!!!!!
c-d
で半分下にスクロール、c-u
で半分上にスクロール!!
できなかったやーつ
prompt_toolkit.key_binding.bindings.page_navigation
にload_vi_page_navigation_bindings
っていうのがあった
試してみる
以下のようにして、実行したら、c-d
で半分下に移動ができなかった...
ただ呼ぶだけじゃだめなのかな?
from prompt_toolkit.application import Application from prompt_toolkit.key_binding import KeyBindings from prompt_toolkit.layout.layout import Layout from prompt_toolkit.buffer import Buffer from prompt_toolkit.layout.containers import Window from prompt_toolkit.layout.controls import BufferControl from prompt_toolkit.document import Document from prompt_toolkit.key_binding.bindings.page_navigation import load_vi_page_navigation_bindings def get_text(): return '\n'.join([chr(i) for i in range(65, 65+26)] * 3) body = Window(BufferControl(Buffer(document=Document(get_text(), cursor_position=0), read_only=True))) # キーバインディング kb = KeyBindings() load_vi_page_navigation_bindings() @kb.add('q') def _(event): event.app.exit() app = Application( layout=Layout(body), key_bindings=kb, full_screen=True) app.run()
ソースを見ると、ConditionalKeyBindings
のインスタンスを返してるから、それをなんかするのかも?
def load_vi_page_navigation_bindings(): """ Key bindings, for scrolling up and down through pages. This are separate bindings, because GNU readline doesn't have them. """ key_bindings = KeyBindings() handle = key_bindings.add handle('c-f')(scroll_forward) handle('c-b')(scroll_backward) handle('c-d')(scroll_half_page_down) handle('c-u')(scroll_half_page_up) handle('c-e')(scroll_one_line_down) handle('c-y')(scroll_one_line_up) handle('pagedown')(scroll_page_down) handle('pageup')(scroll_page_up) return ConditionalKeyBindings(key_bindings, vi_mode)
prompt_toolkit.key_binding.key_bindings.ContiditionalKeyBindings
だ
viモードのときにしか聞かないのかな?