Skip to content

Commit

Permalink
attempt fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Carreau authored and Chris Hamill committed Dec 7, 2023
1 parent df4685d commit 29315b8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
11 changes: 11 additions & 0 deletions IPython/terminal/interactiveshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,17 @@ def _merge_shortcuts(self, user_shortcuts):
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.
Expand Down
18 changes: 16 additions & 2 deletions IPython/terminal/prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,20 @@ def vi_mode(self):
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, ']: '),
Expand All @@ -41,7 +50,12 @@ def _width(self):
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,
Expand Down

0 comments on commit 29315b8

Please sign in to comment.