|
62 | 62 | BOOKMARKS_FILE = "bookmarks.txt"
|
63 | 63 |
|
64 | 64 |
|
65 |
| -superscript_map = { |
66 |
| - "0": "⁰", |
67 |
| - "1": "¹", |
68 |
| - "2": "²", |
69 |
| - "3": "³", |
70 |
| - "4": "⁴", |
71 |
| - "5": "⁵", |
72 |
| - "6": "⁶", |
73 |
| - "7": "⁷", |
74 |
| - "8": "⁸", |
75 |
| - "9": "⁹", |
76 |
| -} |
77 |
| - |
78 |
| - |
79 |
| -class TextWithLinks(urwid.WidgetWrap): |
80 |
| - def __init__(self, markup, on_link_click): |
81 |
| - self.markup = markup |
82 |
| - self.on_link_click = on_link_click |
83 |
| - self.text = urwid.Text(markup) |
84 |
| - self.focused_item_index = 0 |
85 |
| - self.focusable_items = self.get_focusable_items(markup) |
86 |
| - self.update_focus(markup) |
87 |
| - super().__init__(self.text) |
88 |
| - |
89 |
| - def update_focus(self, markup): |
90 |
| - rewrite = [] |
91 |
| - index = 0 |
92 |
| - for item in markup: |
93 |
| - if isinstance(item, tuple) and item[0].startswith("http"): |
94 |
| - if index == self.focused_item_index: |
95 |
| - rewrite.append( |
96 |
| - ("link_focused", f"{item[1]} {superscript_map[str(index+1)]}") |
97 |
| - ) |
98 |
| - else: |
99 |
| - rewrite.append( |
100 |
| - ("link", f"{item[1]} {superscript_map[str(index+1)]}") |
101 |
| - ) |
102 |
| - index += 1 |
103 |
| - else: |
104 |
| - rewrite.append(item) |
105 |
| - self.text.set_text(rewrite) |
106 |
| - |
107 |
| - def get_focusable_items(self, markup): |
108 |
| - return [ |
109 |
| - item |
110 |
| - for item in markup |
111 |
| - if isinstance(item, tuple) and item[0].startswith("http") |
112 |
| - ] |
113 |
| - |
114 |
| - def keypress(self, size, key): |
115 |
| - max_position = len(self.focusable_items) - 1 |
116 |
| - if key == "up" or key == "left": |
117 |
| - self.focused_item_index = max(0, self.focused_item_index - 1) |
118 |
| - elif key == "down" or key == "right": |
119 |
| - self.focused_item_index = min(max_position, self.focused_item_index + 1) |
120 |
| - elif key == "enter": |
121 |
| - self.on_link_click(self.focusable_items[self.focused_item_index][0]) |
122 |
| - else: |
123 |
| - return key |
124 |
| - self.update_focus(self.markup) |
125 |
| - |
126 |
| - def selectable(self): |
127 |
| - return True |
128 |
| - |
129 |
| - |
130 | 65 | class Hyperlink(urwid.SelectableIcon):
|
131 | 66 | signals = ["click"]
|
132 | 67 |
|
|
0 commit comments