MCPcopy
hub / github.com/prompt-toolkit/ptpython / read

Method read

src/ptpython/python_input.py:1074–1120  ·  view source on GitHub ↗

Read the input. This will run the Python input user interface in another thread, wait for input to be accepted and return that. By running the UI in another thread, we avoid issues regarding possibly nested event loops. This can raise EOFError, when Control

(self)

Source from the content-addressed store, hash-verified

1072 asyncio.ensure_future(do_in_terminal())
1073
1074 def read(self) -> str:
1075 """
1076 Read the input.
1077
1078 This will run the Python input user interface in another thread, wait
1079 for input to be accepted and return that. By running the UI in another
1080 thread, we avoid issues regarding possibly nested event loops.
1081
1082 This can raise EOFError, when Control-D is pressed.
1083 """
1084
1085 # Capture the current input_mode in order to restore it after reset,
1086 # for ViState.reset() sets it to InputMode.INSERT unconditionally and
1087 # doesn't accept any arguments.
1088 def pre_run(
1089 last_input_mode: InputMode = self.app.vi_state.input_mode,
1090 ) -> None:
1091 if self.vi_keep_last_used_mode:
1092 self.app.vi_state.input_mode = last_input_mode
1093
1094 if not self.vi_keep_last_used_mode and self.vi_start_in_navigation_mode:
1095 self.app.vi_state.input_mode = InputMode.NAVIGATION
1096
1097 # Run the UI.
1098 while True:
1099 try:
1100 result = self.app.run(pre_run=pre_run, in_thread=True)
1101
1102 if result.lstrip().startswith("\x1a"):
1103 # When the input starts with Ctrl-Z, quit the REPL.
1104 # (Important for Windows users.)
1105 raise EOFError
1106
1107 # Remove leading whitespace.
1108 # (Users can add extra indentation, which happens for
1109 # instance because of copy/pasting code.)
1110 result = unindent_code(result)
1111
1112 if result and not result.isspace():
1113 if self.insert_blank_line_after_input:
1114 self.app.output.write("\n")
1115
1116 return result
1117 except KeyboardInterrupt:
1118 # Abort - try again.
1119 self.signatures = []
1120 self.default_buffer.document = Document()

Callers 6

run_startup_scriptsFunction · 0.80
_load_start_pathsMethod · 0.80
runMethod · 0.80
run_configFunction · 0.80
runFunction · 0.80
runFunction · 0.80

Calls 3

unindent_codeFunction · 0.85
runMethod · 0.80
writeMethod · 0.45

Tested by

no test coverage detected