| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -402,6 +402,7 @@ def __init__(self, pdb_instance, stdin, stdout, prompt): | |||
| 402 | 402 | completer_delims=frozenset(' \t\n`@#%^&*()=+[{]}\\|;:\'",<>?') | |
| 403 | 403 | ) | |
| 404 | 404 | ) | |
| 405 | + self.readline_wrapper.get_reader().gen_colors = self.gen_colors | ||
| 405 | 406 | ||
| 406 | 407 | def readline(self): | |
| 407 | 408 | ||
@@ -463,6 +464,26 @@ def complete(self, text, state): | |||
| 463 | 464 | except IndexError: | |
| 464 | 465 | return None | |
| 465 | 466 | ||
| 467 | + def gen_colors(self, buffer): | ||
| 468 | + from _pyrepl.utils import ColorSpan, Span | ||
| 469 | + | ||
| 470 | + if not buffer.strip(): | ||
| 471 | + return | ||
| 472 | + | ||
| 473 | + leading_spaces = len(buffer) - len(buffer.lstrip()) | ||
| 474 | + leading_text = buffer.split()[0] | ||
| 475 | + if hasattr(self.pdb_instance, 'do_' + leading_text): | ||
| 476 | + yield ColorSpan( | ||
| 477 | + Span(leading_spaces, leading_spaces + len(leading_text) - 1), | ||
| 478 | + "soft_keyword" | ||
| 479 | + ) | ||
| 480 | + # Redact the command text with spaces so there will be no duplicated | ||
| 481 | + # color span generated for it later. | ||
| 482 | + redact_length = leading_spaces + len(leading_text) | ||
| 483 | + buffer = ' ' * redact_length + buffer[redact_length:] | ||
| 484 | + | ||
| 485 | + yield from _pyrepl.utils.gen_colors(buffer) | ||
| 486 | + | ||
| 466 | 487 | ||
| 467 | 488 | class Pdb(bdb.Bdb, cmd.Cmd): | |
| 468 | 489 | _previous_sigint_handler = None | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4981,6 +4981,29 @@ def test_stack_entry(self): | |||
| 4981 | 4981 | p.set_trace(commands=['w', 'c']) | |
| 4982 | 4982 | self.assertIn("\x1b", output.getvalue()) | |
| 4983 | 4983 | ||
| 4984 | + @unittest.skipIf(not pdb._pyrepl_available(), "pyrepl is not available") | ||
| 4985 | + def test_gen_colors(self): | ||
| 4986 | + p = pdb.Pdb() | ||
| 4987 | + gen_colors = p.pyrepl_input.gen_colors | ||
| 4988 | + | ||
| 4989 | + test_cases = [ | ||
| 4990 | + ("longlist", [((0, 7), "soft_keyword")]), | ||
| 4991 | + ("!longlist", [((0, 0), "op")]), | ||
| 4992 | + ("list", [((0, 3), "soft_keyword")]), | ||
| 4993 | + ("list(", [((0, 3), "builtin"), ((4, 4), "op")]), | ||
| 4994 | + ("a = 1", [ | ||
| 4995 | + ((0, 0), "soft_keyword"), | ||
| 4996 | + ((2, 2), "op"), | ||
| 4997 | + ((4, 4), "number"), | ||
| 4998 | + ]) | ||
| 4999 | + ] | ||
| 5000 | + | ||
| 5001 | + for buffer, expected in test_cases: | ||
| 5002 | + for color_span, ((start, end), tag) in zip(gen_colors(buffer), expected, strict=True): | ||
| 5003 | + self.assertEqual(color_span.span.start, start) | ||
| 5004 | + self.assertEqual(color_span.span.end, end) | ||
| 5005 | + self.assertEqual(color_span.tag, tag) | ||
| 5006 | + | ||
| 4984 | 5007 | ||
| 4985 | 5008 | @support.force_not_colorized_test_class | |
| 4986 | 5009 | @support.requires_subprocess() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1 @@ | |||
| 1 | + Generate consistent colors for :mod:`pdb` commands in :mod:`pdb` REPL. | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments