MCPcopy Index your code
hub / github.com/python-cmd2/cmd2 / _get_core_print_console

Method _get_core_print_console

cmd2/cmd2.py:1390–1440  ·  view source on GitHub ↗

Get a console configured for the specified stream and formatting settings. This method is intended for internal use by cmd2's core print methods. To avoid the overhead of repeated initialization, it manages a thread-local cache for consoles targeting ``self.stdout`` or ``sys

(
        self,
        *,
        file: IO[str],
        emoji: bool,
        markup: bool,
        highlight: bool,
    )

Source from the content-addressed store, hash-verified

1388 return su.strip_style(self.prompt)
1389
1390 def _get_core_print_console(
1391 self,
1392 *,
1393 file: IO[str],
1394 emoji: bool,
1395 markup: bool,
1396 highlight: bool,
1397 ) -> Cmd2BaseConsole:
1398 """Get a console configured for the specified stream and formatting settings.
1399
1400 This method is intended for internal use by cmd2's core print methods.
1401 To avoid the overhead of repeated initialization, it manages a thread-local
1402 cache for consoles targeting ``self.stdout`` or ``sys.stderr``. It returns a cached
1403 instance if its configuration matches the request. For all other streams, or if
1404 the configuration has changed, a new console is created.
1405
1406 Note: This implementation works around a bug in Rich where passing formatting settings
1407 (emoji, markup, and highlight) directly to console.print() or console.log() does not
1408 always work when printing certain Renderables. Passing them to the constructor instead
1409 ensures they are correctly propagated. Once this bug is fixed, these parameters can
1410 be removed from this method. For more details, see:
1411 https://github.com/Textualize/rich/issues/4028
1412 """
1413 # Dictionary of settings to check against cached consoles
1414 kwargs = {
1415 "emoji": emoji,
1416 "markup": markup,
1417 "highlight": highlight,
1418 }
1419
1420 # Check if we should use or update a cached console
1421 if file is self.stdout:
1422 cached = self._console_cache.stdout
1423 if cached is not None and cached.matches_config(file=file, **kwargs):
1424 return cached
1425
1426 # Create new console and update cache
1427 self._console_cache.stdout = Cmd2BaseConsole(file=file, **kwargs)
1428 return self._console_cache.stdout
1429
1430 if file is sys.stderr:
1431 cached = self._console_cache.stderr
1432 if cached is not None and cached.matches_config(file=file, **kwargs):
1433 return cached
1434
1435 # Create new console and update cache
1436 self._console_cache.stderr = Cmd2BaseConsole(file=file, **kwargs)
1437 return self._console_cache.stderr
1438
1439 # For any other file, just create a new console
1440 return Cmd2BaseConsole(file=file, **kwargs)
1441
1442 def print_to(
1443 self,

Callers 5

print_toMethod · 0.95
ppagedMethod · 0.95

Calls 2

Cmd2BaseConsoleClass · 0.85
matches_configMethod · 0.80