|
| 1 | +"""ipython |
| 2 | +========== |
| 3 | +""" |
| 4 | +from typing import Any, Callable |
| 5 | + |
| 6 | +from IPython.terminal.interactiveshell import TerminalInteractiveShell |
| 7 | +from IPython.terminal.prompts import ClassicPrompts, Prompts |
| 8 | +from pygments.token import _TokenType |
| 9 | +from traitlets.config.loader import Config, LazyConfigValue |
| 10 | + |
| 11 | +from . import wakatime_hook |
| 12 | + |
| 13 | + |
| 14 | +def get_new_prompts_class( |
| 15 | + prompts_class: type, |
| 16 | + hook: Callable = wakatime_hook, |
| 17 | + args: tuple = (), |
| 18 | + kwargs: dict[str, Any] = {}, |
| 19 | +) -> type: |
| 20 | + """Get new prompts class. |
| 21 | +
|
| 22 | + :param prompts_class: |
| 23 | + :type prompts_class: type |
| 24 | + :param hook: |
| 25 | + :type hook: Callable |
| 26 | + :param args: |
| 27 | + :type args: tuple |
| 28 | + :param kwargs: |
| 29 | + :type kwargs: dict[str, Any] |
| 30 | + :rtype: type |
| 31 | + """ |
| 32 | + if isinstance(prompts_class, LazyConfigValue): |
| 33 | + prompts_class = ClassicPrompts |
| 34 | + shell = TerminalInteractiveShell() |
| 35 | + |
| 36 | + class Ps(Prompts): |
| 37 | + """Ps.""" |
| 38 | + |
| 39 | + def in_prompt_tokens(self) -> list[tuple[_TokenType, str]]: |
| 40 | + """In prompt tokens. |
| 41 | +
|
| 42 | + :rtype: list[tuple[_TokenType, str]] |
| 43 | + """ |
| 44 | + return prompts_class(shell).in_prompt_tokens() |
| 45 | + |
| 46 | + def continuation_prompt_tokens( |
| 47 | + self, width: int | None = None |
| 48 | + ) -> list[tuple[_TokenType, str]]: |
| 49 | + """Continuation prompt tokens. |
| 50 | +
|
| 51 | + :param width: |
| 52 | + :type width: int | None |
| 53 | + :rtype: list[tuple[_TokenType, str]] |
| 54 | + """ |
| 55 | + return prompts_class(shell).continuation_prompt_tokens(width) |
| 56 | + |
| 57 | + def rewrite_prompt_tokens(self) -> list[tuple[_TokenType, str]]: |
| 58 | + """Rewrite prompt tokens. |
| 59 | +
|
| 60 | + :rtype: list[tuple[_TokenType, str]] |
| 61 | + """ |
| 62 | + return prompts_class(shell).rewrite_prompt_tokens() |
| 63 | + |
| 64 | + def out_prompt_tokens(self) -> list[tuple[_TokenType, str]]: |
| 65 | + """Out prompt tokens. |
| 66 | +
|
| 67 | + :rtype: list[tuple[_TokenType, str]] |
| 68 | + """ |
| 69 | + hook(*args, **kwargs) |
| 70 | + return prompts_class(shell).out_prompt_tokens() |
| 71 | + |
| 72 | + return Ps |
| 73 | + |
| 74 | + |
| 75 | +def install_hook( |
| 76 | + c: Config, |
| 77 | + hook: Callable = wakatime_hook, |
| 78 | + args: tuple = (), |
| 79 | + kwargs: dict[str, Any] = {}, |
| 80 | +) -> Config: |
| 81 | + """Install hook. |
| 82 | +
|
| 83 | + :param c: |
| 84 | + :type c: Config |
| 85 | + :param hook: |
| 86 | + :type hook: Callable |
| 87 | + :param args: |
| 88 | + :type args: tuple |
| 89 | + :param kwargs: |
| 90 | + :type kwargs: dict[str, Any] |
| 91 | + :rtype: Config |
| 92 | + """ |
| 93 | + c.TerminalInteractiveShell.prompts_class = get_new_prompts_class( # type: ignore |
| 94 | + c.TerminalInteractiveShell.prompts_class, hook, args, kwargs # type: ignore |
| 95 | + ) |
| 96 | + return c |
0 commit comments