TIL

Today I Learned. 知ったこと、学んだことを書いていく

vimっぽいページスクロール(c-dとc-u)をpython-prompt-toolkitで実装 - Python

python-prompt-toolkitを使って、ページのスクロールを実装したからメモメモ

以下の2つだけとりあえず、実装してみる

https://github.com/prompt-toolkit/python-prompt-toolkit/blob/master/prompt_toolkit/key_binding/bindings/page_navigation.py#L48-L65

    handle('c-d')(scroll_half_page_down)
    handle('c-u')(scroll_half_page_up)

prompt_toolkit.key_binding.bindings.scrollscroll_half_page_downscroll_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_navigationload_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インスタンスを返してるから、それをなんかするのかも?

https://github.com/prompt-toolkit/python-prompt-toolkit/blob/master/prompt_toolkit/key_binding/bindings/page_navigation.py#L48-L65

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モードのときにしか聞かないのかな?