##// END OF EJS Templates
Feature/add line numbers to prompt (#14223)...
Matthias Bussonnier -
r28541:b5bf8f15 merge
parent child Browse files
Show More
@@ -0,0 +1,4 b''
1 Line Numbers
2 ============
3
4 This PR add line numbers to the default prompt.
@@ -588,6 +588,17 b' class TerminalInteractiveShell(InteractiveShell):'
588 help="Display the current vi mode (when using vi editing mode)."
588 help="Display the current vi mode (when using vi editing mode)."
589 ).tag(config=True)
589 ).tag(config=True)
590
590
591 prompt_line_number_format = Unicode(
592 "",
593 help="The format for line numbering, will be passed `line` (int, 1 based)"
594 " the current line number and `rel_line` the relative line number."
595 " for example to display both you can use the following template string :"
596 " c.TerminalInteractiveShell.prompt_line_number_format='{line: 4d}/{rel_line:+03d} | '"
597 " This will display the current line number, with leading space and a width of at least 4"
598 " character, as well as the relative line number 0 padded and always with a + or - sign."
599 " Note that when using Emacs mode the prompt of the first line may not update.",
600 ).tag(config=True)
601
591 @observe('term_title')
602 @observe('term_title')
592 def init_term_title(self, change=None):
603 def init_term_title(self, change=None):
593 # Enable or disable the terminal title.
604 # Enable or disable the terminal title.
@@ -736,7 +747,7 b' class TerminalInteractiveShell(InteractiveShell):'
736 def get_message():
747 def get_message():
737 return PygmentsTokens(self.prompts.in_prompt_tokens())
748 return PygmentsTokens(self.prompts.in_prompt_tokens())
738
749
739 if self.editing_mode == 'emacs':
750 if self.editing_mode == "emacs" and self.prompt_line_number_format == "":
740 # with emacs mode the prompt is (usually) static, so we call only
751 # with emacs mode the prompt is (usually) static, so we call only
741 # the function once. With VI mode it can toggle between [ins] and
752 # the function once. With VI mode it can toggle between [ins] and
742 # [nor] so we can't precompute.
753 # [nor] so we can't precompute.
@@ -753,7 +764,7 b' class TerminalInteractiveShell(InteractiveShell):'
753 "message": get_message,
764 "message": get_message,
754 "prompt_continuation": (
765 "prompt_continuation": (
755 lambda width, lineno, is_soft_wrap: PygmentsTokens(
766 lambda width, lineno, is_soft_wrap: PygmentsTokens(
756 self.prompts.continuation_prompt_tokens(width)
767 self.prompts.continuation_prompt_tokens(width, lineno=lineno)
757 )
768 )
758 ),
769 ),
759 "multiline": True,
770 "multiline": True,
@@ -25,11 +25,21 b' class Prompts(object):'
25 return '['+mode+'] '
25 return '['+mode+'] '
26 return ''
26 return ''
27
27
28 def current_line(self) -> int:
29 if self.shell.pt_app is not None:
30 return self.shell.pt_app.default_buffer.document.cursor_position_row or 0
31 return 0
28
32
29 def in_prompt_tokens(self):
33 def in_prompt_tokens(self):
30 return [
34 return [
31 (Token.Prompt, self.vi_mode() ),
35 (Token.Prompt, self.vi_mode()),
32 (Token.Prompt, 'In ['),
36 (
37 Token.Prompt,
38 self.shell.prompt_line_number_format.format(
39 line=1, rel_line=-self.current_line()
40 ),
41 ),
42 (Token.Prompt, "In ["),
33 (Token.PromptNum, str(self.shell.execution_count)),
43 (Token.PromptNum, str(self.shell.execution_count)),
34 (Token.Prompt, ']: '),
44 (Token.Prompt, ']: '),
35 ]
45 ]
@@ -37,11 +47,20 b' class Prompts(object):'
37 def _width(self):
47 def _width(self):
38 return fragment_list_width(self.in_prompt_tokens())
48 return fragment_list_width(self.in_prompt_tokens())
39
49
40 def continuation_prompt_tokens(self, width=None):
50 def continuation_prompt_tokens(self, width=None, *, lineno=None):
41 if width is None:
51 if width is None:
42 width = self._width()
52 width = self._width()
53 line = lineno + 1 if lineno is not None else 0
54 prefix = " " * len(
55 self.vi_mode()
56 ) + self.shell.prompt_line_number_format.format(
57 line=line, rel_line=line - self.current_line() - 1
58 )
43 return [
59 return [
44 (Token.Prompt, (' ' * (width - 5)) + '...: '),
60 (
61 Token.Prompt,
62 prefix + (" " * (width - len(prefix) - 5)) + "...: ",
63 ),
45 ]
64 ]
46
65
47 def rewrite_prompt_tokens(self):
66 def rewrite_prompt_tokens(self):
General Comments 0
You need to be logged in to leave comments. Login now