Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/IPython/terminal/prompts.py: 37%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

73 statements  

1"""Terminal input and output prompts.""" 

2 

3from __future__ import annotations 

4 

5from pygments.token import _TokenType, Token 

6import sys 

7 

8from IPython.core.displayhook import DisplayHook 

9 

10from prompt_toolkit.formatted_text import fragment_list_width, PygmentsTokens 

11from prompt_toolkit.shortcuts import print_formatted_text 

12from prompt_toolkit.enums import EditingMode 

13from typing import TYPE_CHECKING, Any 

14 

15if TYPE_CHECKING: 

16 from IPython.terminal.interactiveshell import TerminalInteractiveShell 

17 

18 

19class Prompts: 

20 def __init__(self, shell: TerminalInteractiveShell): 

21 self.shell = shell 

22 

23 def vi_mode(self): 

24 if (getattr(self.shell.pt_app, 'editing_mode', None) == EditingMode.VI 

25 and self.shell.prompt_includes_vi_mode): 

26 mode = str(self.shell.pt_app.app.vi_state.input_mode) 

27 if mode.startswith('InputMode.'): 

28 mode = mode[10:13].lower() 

29 elif mode.startswith('vi-'): 

30 mode = mode[3:6] 

31 return '['+mode+'] ' 

32 return '' 

33 

34 def current_line(self) -> int: 

35 if self.shell.pt_app is not None: 

36 return self.shell.pt_app.default_buffer.document.cursor_position_row or 0 

37 return 0 

38 

39 def in_prompt_tokens(self): 

40 return [ 

41 (Token.Prompt.Mode, self.vi_mode()), 

42 ( 

43 Token.Prompt.LineNumber, 

44 self.shell.prompt_line_number_format.format( 

45 line=1, rel_line=-self.current_line() 

46 ), 

47 ), 

48 (Token.Prompt, "In ["), 

49 (Token.PromptNum, str(self.shell.execution_count)), 

50 (Token.Prompt, ']: '), 

51 ] 

52 

53 def _width(self): 

54 return fragment_list_width(self.in_prompt_tokens()) 

55 

56 def continuation_prompt_tokens( 

57 self, 

58 width: int | None = None, 

59 *, 

60 lineno: int | None = None, 

61 wrap_count: int | None = None, 

62 ): 

63 if width is None: 

64 width = self._width() 

65 line = lineno + 1 if lineno is not None else 0 

66 if wrap_count: 

67 return [ 

68 ( 

69 Token.Prompt.Wrap, 

70 # (" " * (width - 2)) + "\N{HORIZONTAL ELLIPSIS} ", 

71 (" " * (width - 2)) + "\N{VERTICAL ELLIPSIS} ", 

72 ), 

73 ] 

74 prefix = " " * len( 

75 self.vi_mode() 

76 ) + self.shell.prompt_line_number_format.format( 

77 line=line, rel_line=line - self.current_line() - 1 

78 ) 

79 return [ 

80 ( 

81 getattr(Token.Prompt.Continuation, f"L{lineno}"), 

82 prefix + (" " * (width - len(prefix) - 5)) + "...:", 

83 ), 

84 (Token.Prompt.Padding, " "), 

85 ] 

86 

87 def rewrite_prompt_tokens(self): 

88 width = self._width() 

89 return [ 

90 (Token.Prompt, ('-' * (width - 2)) + '> '), 

91 ] 

92 

93 def out_prompt_tokens(self) -> list[tuple[_TokenType, str]]: 

94 return [ 

95 (Token.OutPrompt, 'Out['), 

96 (Token.OutPromptNum, str(self.shell.execution_count - 1)), 

97 (Token.OutPrompt, ']: '), 

98 ] 

99 

100class ClassicPrompts(Prompts): 

101 def in_prompt_tokens(self): 

102 return [ 

103 (Token.Prompt, '>>> '), 

104 ] 

105 

106 def continuation_prompt_tokens(self, width=None): 

107 return [(Token.Prompt.Continuation, "... ")] 

108 

109 def rewrite_prompt_tokens(self): 

110 return [] 

111 

112 def out_prompt_tokens(self): 

113 return [] 

114 

115class RichPromptDisplayHook(DisplayHook): 

116 """Subclass of base display hook using coloured prompt""" 

117 def write_output_prompt(self): 

118 sys.stdout.write(self.shell.separate_out) 

119 # If we're not displaying a prompt, it effectively ends with a newline, 

120 # because the output will be left-aligned. 

121 self.prompt_end_newline = True 

122 

123 if self.do_full_cache: 

124 tokens = self.shell.prompts.out_prompt_tokens() 

125 prompt_txt = "".join(s for _, s in tokens) 

126 if prompt_txt and not prompt_txt.endswith("\n"): 

127 # Ask for a newline before multiline output 

128 self.prompt_end_newline = False 

129 

130 if self.shell.pt_app: 

131 print_formatted_text(PygmentsTokens(tokens), 

132 style=self.shell.pt_app.app.style, end='', 

133 ) 

134 else: 

135 sys.stdout.write(prompt_txt) 

136 

137 def write_format_data(self, format_dict: dict[str, str], md_dict: dict[Any, Any] | None = None) -> None: 

138 assert self.shell is not None 

139 if self.shell.mime_renderers: 

140 

141 for mime, handler in self.shell.mime_renderers.items(): 

142 if mime in format_dict: 

143 handler(format_dict[mime], None) 

144 return 

145 

146 super().write_format_data(format_dict, md_dict)