code: def format_filename(
      filename: "t.Union[str, bytes, os.PathLike[str], os.PathLike[bytes]]",
      shorten: bool = False,
  ) -> str:
      """Format a filename as a string for display. Ensures the filename can be
      displayed by replacing any invalid bytes or surrogate escapes in the name
      with the replacement character ``�``.
  
      Invalid bytes or surrogate escapes will raise an error when written to a
      stream with ``errors="strict". This will typically happen with ``stdout``
      when the locale is something like ``en_GB.UTF-8``.
  
      Many scenarios *are* safe to write surrogates though, due to PEP 538 and
      PEP 540, including:
  
      -   Writing to ``stderr``, which uses ``errors="backslashreplace"``.
      -   The system has ``LANG=C.UTF-8``, ``C``, or ``POSIX``. Python opens
          stdout and stderr with ``errors="surrogateescape"``.
      -   None of ``LANG/LC_*`` are set. Python assumes ``LANG=C.UTF-8``.
      -   Python is started in UTF-8 mode  with  ``PYTHONUTF8=1`` or ``-X utf8``.
          Python opens stdout and stderr with ``errors="surrogateescape"``.
  
      :param filename: formats a filename for UI display.  This will also convert
                       the filename into unicode without failing.
      :param shorten: this optionally shortens the filename to strip of the
                      path that leads up to it.
      """
      if shorten:
          filename = os.path.basename(filename)
      else:
          filename = os.fspath(filename)
  
      if isinstance(filename, bytes):
          filename = filename.decode(
              sys.getfilesystemencoding(), "replace"
          )
      else:
          filename = filename.encode("utf-8", "surrogateescape").decode(
              "utf-8", "replace"
          )
  
      return filename
  
imports: import os
  import re
  import sys
  import typing
  from functools import update_wrapper
  from types import ModuleType
  from types import TracebackType
  from ._compat import _default_text_stderr
  from ._compat import _default_text_stdout
  from ._compat import _find_binary_writer
  from ._compat import auto_wrap_for_ansi
  from ._compat import binary_streams
  from ._compat import open_stream
  from ._compat import should_strip_ansi
  from ._compat import strip_ansi
  from ._compat import text_streams
  from ._compat import WIN
  from .globals import resolve_color_default
label_desc: Use image node_id 9 for calling a global function with example usage: format_filename(filename, shorten) and returns: filename