Broken search

This commit is contained in:
Sebastian Lohff 2019-02-25 02:00:57 +01:00
parent ec07ebe536
commit 237900e362
1 changed files with 34 additions and 3 deletions

View File

@ -3,6 +3,8 @@
from prompt_toolkit.application import Application from prompt_toolkit.application import Application
from prompt_toolkit.buffer import Buffer from prompt_toolkit.buffer import Buffer
from prompt_toolkit.document import Document from prompt_toolkit.document import Document
from prompt_toolkit.filters import is_searching
from prompt_toolkit.filters.base import Condition
from prompt_toolkit.key_binding import KeyBindings from prompt_toolkit.key_binding import KeyBindings
from prompt_toolkit.layout import Layout, Window, HSplit from prompt_toolkit.layout import Layout, Window, HSplit
from prompt_toolkit.layout.controls import BufferControl from prompt_toolkit.layout.controls import BufferControl
@ -68,6 +70,12 @@ class CliMenuTheme:
ANSI_CYAN = CliMenuStyle('ansicyan', 'ansibrightcyan', 'ansicyan') ANSI_CYAN = CliMenuStyle('ansicyan', 'ansibrightcyan', 'ansicyan')
@Condition
def is_not_searching():
print("CALLED IT")
return not is_searching()
class CliMenu: class CliMenu:
default_stye = CliMenuTheme.BASIC default_stye = CliMenuTheme.BASIC
default_cursor = CliMenuCursor.TRIANGLE default_cursor = CliMenuCursor.TRIANGLE
@ -139,6 +147,8 @@ class CliMenu:
return ' ' * (len(self._cursor) + 1 * self._dedent_selection) return ' ' * (len(self._cursor) + 1 * self._dedent_selection)
def _transform_line(self, ti): def _transform_line(self, ti):
if len(list(ti.fragments)) == 0:
return Transformation(ti.fragments)
style, text = list(ti.fragments)[0] style, text = list(ti.fragments)[0]
item = self._items[ti.lineno] item = self._items[ti.lineno]
s = self._style s = self._style
@ -160,7 +170,9 @@ class CliMenu:
indent += ' ' * (self._header_indent + len(self._cursor) + 1) indent += ' ' * (self._header_indent + len(self._cursor) + 1)
style = s.header_style style = s.header_style
return Transformation([('', indent), (style, prefix + text)]) items = [(s if s else style, t) for s, t in ti.fragments]
return Transformation([('', indent), (style, prefix)] + items)
def next_item(self, direction): def next_item(self, direction):
if not any(item.focusable for item in self._items): if not any(item.focusable for item in self._items):
@ -198,21 +210,40 @@ class CliMenu:
def up(event): def up(event):
self.next_item(-1) self.next_item(-1)
@self._kb.add('n')
def search_inc(event, filter=is_searching):
print(self._bufctrl.search_state.text)
print("curr pos", self._buf.get_search_position(self._bufctrl.search_state))
# search_state.direction = search.SearchDirection.BACKWARD
next10 = []
for i in range(10):
p = self._buf.get_search_position(self._bufctrl.search_state, count=i + 1, include_current_position=False)
next10.append(p)
print("next 10 pos are", next10)
#search.do_incremental_search(search.SearchDirection.FORWARD)
@self._kb.add('right') @self._kb.add('right')
@self._kb.add('c-m') #@self._kb.add('c-m')
@self._kb.add('c-space') @self._kb.add('c-space')
def accept(event): def accept(event):
self._success = True self._success = True
event.app.exit() event.app.exit()
self._searchbar = SearchToolbar(text_if_not_searching=[('class:not-searching', 'NOOT NOOT')], ignore_case=True)
text = '\n'.join(map(lambda _x: _x.text, self._items)) text = '\n'.join(map(lambda _x: _x.text, self._items))
self._doc = Document(text, cursor_position=self._pos) self._doc = Document(text, cursor_position=self._pos)
self._buf = Buffer(read_only=True, document=self._doc) self._buf = Buffer(read_only=True, document=self._doc)
self._bufctrl = BufferControl(self._buf, self._bufctrl = BufferControl(self._buf,
search_buffer_control=self._searchbar.control,
preview_search=True,
input_processors=[MenuColorizer()]) input_processors=[MenuColorizer()])
split = HSplit([Window(self._bufctrl, split = HSplit([Window(self._bufctrl,
wrap_lines=True, wrap_lines=True,
always_hide_cursor=True)]) always_hide_cursor=True),
self._searchbar])
# set initial pos # set initial pos
if not self._items[self._pos].focusable: if not self._items[self._pos].focusable: