Instantly share code, notes, and snippets.

Embed
What would you like to do?
from prompt_toolkit.application import Application
from prompt_toolkit.application.current import get_app
from prompt_toolkit.buffer import Buffer
from prompt_toolkit.document import Document
from prompt_toolkit.filters import Condition, is_searching
from prompt_toolkit.key_binding import KeyBindings
from prompt_toolkit.key_binding.bindings import search
from prompt_toolkit.layout.containers import HSplit, Window
from prompt_toolkit.layout.controls import BufferControl
from prompt_toolkit.layout.layout import Layout
from prompt_toolkit.styles import Style
from prompt_toolkit.widgets.toolbars import SearchToolbar
from prompt_toolkit.layout.processors import (
ConditionalProcessor,
DisplayMultipleCursors,
HighlightIncrementalSearchProcessor,
HighlightSearchProcessor,
HighlightSelectionProcessor,
)
from logging import getLogger, config
config.fileConfig("logger.conf")
logger = getLogger(__name__)
text = "\n".join([str(i) for i in range(100)])
all_input_processors = [
# 検索モードではないときだけハイライト
ConditionalProcessor(HighlightSearchProcessor(), ~is_searching),
HighlightIncrementalSearchProcessor(),
HighlightSelectionProcessor(),
DisplayMultipleCursors(),
]
# vi_mode=Trueとすると`/`と`?`で表示されるようになる
search_toolbar = SearchToolbar(vi_mode=True)
control = BufferControl(
Buffer(document=Document(text), read_only=True),
search_buffer_control=search_toolbar.control,
preview_search=True,
include_default_input_processors=False,
input_processors=all_input_processors,
)
# ウィンドウ
body = HSplit([Window(control), search_toolbar])
# キーバインディング
kb = KeyBindings()
@kb.add("q")
def _(event):
event.app.exit()
@Condition
def search_buffer_is_empty():
" Returns True when the search buffer is empty. "
return get_app().current_buffer.text == ""
kb.add("/")(search.start_forward_incremental_search)
kb.add("?")(search.start_reverse_incremental_search)
kb.add("enter", filter=is_searching)(search.accept_search)
kb.add("c-c")(search.abort_search)
kb.add("backspace", filter=search_buffer_is_empty)(search.abort_search)
@kb.add("n", filter=~is_searching)
def _(event):
search_state = get_app().current_search_state
current_buffer = get_app().current_buffer
cursor_position = current_buffer.get_search_position(
search_state, include_current_position=False
)
current_buffer.cursor_position = cursor_position
@kb.add("N", filter=~is_searching)
def _(event):
search_state = get_app().current_search_state
current_buffer = get_app().current_buffer
cursor_position = current_buffer.get_search_position(
~search_state, include_current_position=False
)
current_buffer.cursor_position = cursor_position
# incsearchようスタイル
style = Style([("incsearch", "fg:ansibrightyellow reverse")])
app = Application(layout=Layout(body), key_bindings=kb, full_screen=True, style=style)
app.run()
from prompt_toolkit.application import Application
from prompt_toolkit.application.current import get_app
from prompt_toolkit.buffer import Buffer
from prompt_toolkit.document import Document
from prompt_toolkit.filters import Condition, is_searching
from prompt_toolkit.key_binding import KeyBindings
from prompt_toolkit.key_binding.bindings import search
from prompt_toolkit.layout.containers import HSplit, Window
from prompt_toolkit.layout.controls import BufferControl
from prompt_toolkit.layout.layout import Layout
from prompt_toolkit.widgets.toolbars import SearchToolbar
text = "\n".join([str(i) for i in range(100)])
# vi_mode=Trueとすると`/`と`?`で表示されるようになる
search_toolbar = SearchToolbar(vi_mode=True)
control = BufferControl(
Buffer(document=Document(text), read_only=True),
search_buffer_control=search_toolbar.control,
)
# ウィンドウ
body = HSplit([Window(control), search_toolbar])
# キーバインディング
kb = KeyBindings()
@kb.add("q")
def _(event):
event.app.exit()
@Condition
def search_buffer_is_empty():
" Returns True when the search buffer is empty. "
return get_app().current_buffer.text == ""
kb.add("/")(search.start_forward_incremental_search)
kb.add("?")(search.start_reverse_incremental_search)
kb.add("enter", filter=is_searching)(search.accept_search)
kb.add("c-c")(search.abort_search)
kb.add("backspace", filter=search_buffer_is_empty)(search.abort_search)
@kb.add("n", filter=~is_searching)
def _(event):
search_state = get_app().current_search_state
current_buffer = get_app().current_buffer
cursor_position = current_buffer.get_search_position(
search_state, include_current_position=False
)
current_buffer.cursor_position = cursor_position
@kb.add("N", filter=~is_searching)
def _(event):
search_state = get_app().current_search_state
current_buffer = get_app().current_buffer
cursor_position = current_buffer.get_search_position(
~search_state, include_current_position=False
)
current_buffer.cursor_position = cursor_position
app = Application(layout=Layout(body), key_bindings=kb, full_screen=True)
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment