Skip to content

Commit

Permalink
Support formatted text in menu labels
Browse files Browse the repository at this point in the history
And still count the lines correctly.
  • Loading branch information
lpenz committed Nov 14, 2023
1 parent f7836fe commit bca7391
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/bin/ptvertmenu-dir
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ from prompt_toolkit.widgets import Frame, TextArea
E = KeyPressEvent


def showfile(contents: TextArea, item: Optional[tuple[str, Any]], index: int) -> None:
def showfile(contents: TextArea, item: Optional[tuple[Any, Any]], index: int) -> None:
assert item
name = item[1]
try:
Expand Down
4 changes: 2 additions & 2 deletions src/bin/ptvertmenu-man
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import argparse
import asyncio
import os
import re
from typing import Generator, List, Optional, Sequence, cast
from typing import Any, Generator, List, Optional, Sequence, cast

import ptvertmenu
from prompt_toolkit import Application
Expand All @@ -25,7 +25,7 @@ E = KeyPressEvent

PATH = "/usr/share/man"

ManItem = tuple[str, tuple[int, str]]
ManItem = tuple[Any, tuple[int, str]]


def generator() -> Generator[ManItem, None, None]:
Expand Down
22 changes: 15 additions & 7 deletions src/ptvertmenu/vertmenuuicontrol.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,32 @@
from typing import (
TYPE_CHECKING,
Any,
Callable,
Dict,
Iterable,
Iterator,
NewType,
Optional,
cast,
Callable,
)

from prompt_toolkit.data_structures import Point
from prompt_toolkit.filters import FilterOrBool, to_filter
from prompt_toolkit.formatted_text import StyleAndTextTuples
from prompt_toolkit.formatted_text import (
AnyFormattedText,
StyleAndTextTuples,
split_lines,
to_formatted_text,
to_plain_text,
)
from prompt_toolkit.key_binding.key_bindings import KeyBindingsBase
from prompt_toolkit.layout.controls import GetLinePrefixCallable, UIContent, UIControl
from prompt_toolkit.mouse_events import MouseEvent, MouseEventType

if TYPE_CHECKING:
from prompt_toolkit.key_binding.key_bindings import NotImplementedOrNone

Item = tuple[str, Any]
Item = tuple[AnyFormattedText, Any]
Index = NewType("Index", int)


Expand Down Expand Up @@ -67,7 +73,8 @@ def _gen_lineno_mappings(self) -> None:
self._width = 30
for index, item in self._items_enumerate():
self._index_to_lineno[Index(index)] = lineno
for line in item[0].split("\n"):
for formatted_line in split_lines(to_formatted_text(item[0])):
line = to_plain_text(formatted_line)
self._lineno_to_index[lineno] = index
lineno += 1
self._width = max(self._width, len(line))
Expand Down Expand Up @@ -140,12 +147,13 @@ def is_focusable(self) -> bool:
def _get_line(self, lineno: int) -> StyleAndTextTuples:
index = self._lineno_to_index[lineno]
item = self._items[index]
itemlines = item[0].split("\n")
itemlines = list(split_lines(to_formatted_text(item[0])))
line = itemlines[lineno - self._index_to_lineno[index]]
if self.selected_item == item:
return [("class:vertmenu.selected", line)]
style = "class:vertmenu.selected"
else:
return [("class:vertmenu.item", line)]
style = "class:vertmenu.item"
return [(frag[0] + " " + style if frag[0] else style, frag[1]) for frag in line]

def _cursor_position(self) -> Point:
item = self.selected_item
Expand Down

0 comments on commit bca7391

Please sign in to comment.