Updates the specified IPython console shell with the conf.color_theme scapy theme.
(shell)
| 377 | |
| 378 | |
| 379 | def apply_ipython_style(shell): |
| 380 | # type: (Any) -> None |
| 381 | """Updates the specified IPython console shell with |
| 382 | the conf.color_theme scapy theme.""" |
| 383 | try: |
| 384 | from IPython.terminal.prompts import Prompts, Token |
| 385 | except Exception: |
| 386 | from scapy.error import log_loading |
| 387 | log_loading.warning( |
| 388 | "IPython too old. Shell color won't be handled." |
| 389 | ) |
| 390 | return |
| 391 | from scapy.config import conf |
| 392 | scapy_style = {} |
| 393 | # Overwrite colors |
| 394 | if isinstance(conf.color_theme, NoTheme): |
| 395 | shell.colors = 'nocolor' |
| 396 | elif isinstance(conf.color_theme, BrightTheme): |
| 397 | # lightbg is optimized for light backgrounds |
| 398 | shell.colors = 'lightbg' |
| 399 | elif isinstance(conf.color_theme, ColorOnBlackTheme): |
| 400 | # linux is optimised for dark backgrounds |
| 401 | shell.colors = 'linux' |
| 402 | else: |
| 403 | # default |
| 404 | shell.colors = 'neutral' |
| 405 | try: |
| 406 | get_ipython() # type: ignore |
| 407 | # This function actually contains tons of hacks |
| 408 | color_magic = shell.magics_manager.magics["line"]["colors"] |
| 409 | color_magic(shell.colors) |
| 410 | except NameError: |
| 411 | pass |
| 412 | # Prompt Style |
| 413 | if isinstance(conf.prompt, Prompts): |
| 414 | # Set custom prompt style |
| 415 | shell.prompts_class = conf.prompt |
| 416 | else: |
| 417 | if isinstance(conf.color_theme, (FormatTheme, NoTheme)): |
| 418 | # Formatable |
| 419 | if isinstance(conf.color_theme, HTMLTheme): |
| 420 | prompt = html.escape(conf.prompt) |
| 421 | elif isinstance(conf.color_theme, LatexTheme): |
| 422 | from scapy.utils import tex_escape |
| 423 | prompt = tex_escape(conf.prompt) |
| 424 | else: |
| 425 | prompt = conf.prompt |
| 426 | prompt = conf.color_theme.prompt(prompt) |
| 427 | else: |
| 428 | # Needs to be manually set |
| 429 | prompt = str(conf.prompt) |
| 430 | scapy_style[Token.Prompt] = Color.ansi_to_pygments( |
| 431 | conf.color_theme.style_prompt |
| 432 | ) |
| 433 | |
| 434 | class ClassicPrompt(Prompts): |
| 435 | def in_prompt_tokens(self, cli=None): |
| 436 | # type: (Any) -> List[Tuple[Any, str]] |
no test coverage detected