From 29315b87d3b344d77b2f16625a9a51a419f1cbc6 2023-12-07 19:40:42 From: Matthias Bussonnier Date: 2023-12-07 19:40:42 Subject: [PATCH] attempt fix --- diff --git a/IPython/terminal/interactiveshell.py b/IPython/terminal/interactiveshell.py index 4a063d4..b2e8995 100644 --- a/IPython/terminal/interactiveshell.py +++ b/IPython/terminal/interactiveshell.py @@ -588,6 +588,17 @@ class TerminalInteractiveShell(InteractiveShell): help="Display the current vi mode (when using vi editing mode)." ).tag(config=True) + prompt_line_number_format = Unicode( + "", + help="The format for line numbering, will be passed `line` (int, 1 based)" + " the current line number and `rel_line` the relative line number." + " for example to display both you can use the following template string :" + " c.TerminalInteractiveShell.prompt_line_number_format='{line: 4d}/{rel_line:+03d} | '" + " This will display the current line number, with leading space and a width of at least 4" + " character, as well as the relative line number 0 padded and always with a + or - sign." + " Note that when using Emacs mode the prompt of the first line may not update.", + ).tag(config=True) + @observe('term_title') def init_term_title(self, change=None): # Enable or disable the terminal title. diff --git a/IPython/terminal/prompts.py b/IPython/terminal/prompts.py index 8a1176f..ca56d91 100644 --- a/IPython/terminal/prompts.py +++ b/IPython/terminal/prompts.py @@ -25,11 +25,20 @@ class Prompts(object): return '['+mode+'] ' return '' + def current_line(self) -> int: + if self.shell.pt_app is not None: + return self.shell.pt_app.default_buffer.document.cursor_position_row or 0 + return 0 def in_prompt_tokens(self): return [ (Token.Prompt, self.vi_mode()), - (Token.Prompt, "1 | "), + ( + Token.Prompt, + self.shell.prompt_line_number_format.format( + line=1, rel_line=-self.current_line() + ), + ), (Token.Prompt, "In ["), (Token.PromptNum, str(self.shell.execution_count)), (Token.Prompt, ']: '), @@ -41,7 +50,12 @@ class Prompts(object): def continuation_prompt_tokens(self, width=None, *, lineno=None): if width is None: width = self._width() - prefix = " " * len(self.vi_mode()) + str(lineno + 1) + " | " + line = lineno + 1 if lineno is not None else 0 + prefix = " " * len( + self.vi_mode() + ) + self.shell.prompt_line_number_format.format( + line=line, rel_line=line - self.current_line() - 1 + ) return [ ( Token.Prompt,