Adds history support (with incremental history searching) to the Reader class.
| 212 | |
| 213 | @dataclass |
| 214 | class HistoricalReader(Reader): |
| 215 | """Adds history support (with incremental history searching) to the |
| 216 | Reader class. |
| 217 | """ |
| 218 | |
| 219 | history: list[str] = field(default_factory=list) |
| 220 | historyi: int = 0 |
| 221 | next_history: int | None = None |
| 222 | transient_history: dict[int, str] = field(default_factory=dict) |
| 223 | isearch_term: str = "" |
| 224 | isearch_direction: str = ISEARCH_DIRECTION_NONE |
| 225 | isearch_start: tuple[int, int] = field(init=False) |
| 226 | isearch_trans: input.KeymapTranslator = field(init=False) |
| 227 | yank_arg_i: int = 0 |
| 228 | yank_arg_yanked: str = "" |
| 229 | |
| 230 | def __post_init__(self) -> None: |
| 231 | super().__post_init__() |
| 232 | for c in [ |
| 233 | next_history, |
| 234 | previous_history, |
| 235 | restore_history, |
| 236 | first_history, |
| 237 | last_history, |
| 238 | yank_arg, |
| 239 | forward_history_isearch, |
| 240 | reverse_history_isearch, |
| 241 | isearch_end, |
| 242 | isearch_add_character, |
| 243 | isearch_cancel, |
| 244 | isearch_add_character, |
| 245 | isearch_backspace, |
| 246 | isearch_forwards, |
| 247 | isearch_backwards, |
| 248 | operate_and_get_next, |
| 249 | history_search_backward, |
| 250 | history_search_forward, |
| 251 | ]: |
| 252 | self.commands[c.__name__] = c |
| 253 | self.commands[c.__name__.replace("_", "-")] = c |
| 254 | self.isearch_start = self.historyi, self.pos |
| 255 | self.isearch_trans = input.KeymapTranslator( |
| 256 | isearch_keymap, invalid_cls=isearch_end, character_cls=isearch_add_character |
| 257 | ) |
| 258 | |
| 259 | def collect_keymap(self) -> tuple[tuple[KeySpec, CommandName], ...]: |
| 260 | return super().collect_keymap() + ( |
| 261 | (r"\C-n", "next-history"), |
| 262 | (r"\C-p", "previous-history"), |
| 263 | (r"\C-o", "operate-and-get-next"), |
| 264 | (r"\C-r", "reverse-history-isearch"), |
| 265 | (r"\C-s", "forward-history-isearch"), |
| 266 | (r"\M-r", "restore-history"), |
| 267 | (r"\M-.", "yank-arg"), |
| 268 | (r"\<page down>", "history-search-forward"), |
| 269 | (r"\x1b[6~", "history-search-forward"), |
| 270 | (r"\<page up>", "history-search-backward"), |
| 271 | (r"\x1b[5~", "history-search-backward"), |