MCPcopy Index your code
hub / github.com/python/cpython / calc_screen

Method calc_screen

Lib/_pyrepl/reader.py:286–397  ·  view source on GitHub ↗

Translate changes in self.buffer into changes in self.console.screen.

(self)

Source from the content-addressed store, hash-verified

284 return default_keymap
285
286 def calc_screen(self) -> list[str]:
287 """Translate changes in self.buffer into changes in self.console.screen."""
288 # Since the last call to calc_screen:
289 # screen and screeninfo may differ due to a completion menu being shown
290 # pos and cxy may differ due to edits, cursor movements, or completion menus
291
292 # Lines that are above both the old and new cursor position can't have changed,
293 # unless the terminal has been resized (which might cause reflowing) or we've
294 # entered or left paste mode (which changes prompts, causing reflowing).
295 num_common_lines = 0
296 offset = 0
297 if self.last_refresh_cache.valid(self):
298 offset, num_common_lines = self.last_refresh_cache.get_cached_location(self)
299
300 screen = self.last_refresh_cache.screen
301 del screen[num_common_lines:]
302
303 screeninfo = self.last_refresh_cache.screeninfo
304 del screeninfo[num_common_lines:]
305
306 last_refresh_line_end_offsets = self.last_refresh_cache.line_end_offsets
307 del last_refresh_line_end_offsets[num_common_lines:]
308
309 pos = self.pos
310 pos -= offset
311
312 prompt_from_cache = (offset and self.buffer[offset - 1] != "\n")
313
314 if self.can_colorize:
315 colors = list(gen_colors(self.get_unicode()))
316 else:
317 colors = None
318 trace("colors = {colors}", colors=colors)
319 lines = "".join(self.buffer[offset:]).split("\n")
320 cursor_found = False
321 lines_beyond_cursor = 0
322 for ln, line in enumerate(lines, num_common_lines):
323 line_len = len(line)
324 if 0 <= pos <= line_len:
325 self.lxy = pos, ln
326 cursor_found = True
327 elif cursor_found:
328 lines_beyond_cursor += 1
329 if lines_beyond_cursor > self.console.height:
330 # No need to keep formatting lines.
331 # The console can't show them.
332 break
333 if prompt_from_cache:
334 # Only the first line's prompt can come from the cache
335 prompt_from_cache = False
336 prompt = ""
337 else:
338 prompt = self.get_prompt(ln, line_len >= pos >= 0)
339 while "\n" in prompt:
340 pre_prompt, _, prompt = prompt.partition("\n")
341 last_refresh_line_end_offsets.append(offset)
342 screen.append(pre_prompt)
343 screeninfo.append((0, []))

Callers 2

prepareMethod · 0.95
refreshMethod · 0.95

Calls 15

get_unicodeMethod · 0.95
get_promptMethod · 0.95
process_promptMethod · 0.95
pos2xyMethod · 0.95
traceFunction · 0.90
listClass · 0.85
gen_colorsFunction · 0.85
enumerateFunction · 0.85
disp_strFunction · 0.85
get_cached_locationMethod · 0.80
update_cacheMethod · 0.80
validMethod · 0.45

Tested by

no test coverage detected