code: def _trim_formatted_text(
      formatted_text: StyleAndTextTuples, max_width: int
  ) -> tuple[StyleAndTextTuples, int]:
      """
      Trim the text to `max_width`, append dots when the text is too long.
      Returns (text, width) tuple.
      """
      width = fragment_list_width(formatted_text)
  
      # When the text is too wide, trim it.
      if width > max_width:
          result = []  # Text fragments.
          remaining_width = max_width - 3
  
          for style_and_ch in explode_text_fragments(formatted_text):
              ch_width = get_cwidth(style_and_ch[1])
  
              if ch_width <= remaining_width:
                  result.append(style_and_ch)
                  remaining_width -= ch_width
              else:
                  break
  
          result.append(("", "..."))
  
          return result, max_width - remaining_width
      else:
          return formatted_text, width
  
imports: from __future__ import annotations
  import math
  from itertools import zip_longest
  from typing import TYPE_CHECKING, Callable, Iterable, Sequence, TypeVar, cast
  from weakref import WeakKeyDictionary
  from prompt_toolkit.application.current import get_app
  from prompt_toolkit.buffer import CompletionState
  from prompt_toolkit.completion import Completion
  from prompt_toolkit.data_structures import Point
  from prompt_toolkit.filters import Condition, FilterOrBool, has_completions, is_done, to_filter
  from prompt_toolkit.formatted_text import StyleAndTextTuples, fragment_list_width, to_formatted_text
  from prompt_toolkit.key_binding.key_processor import KeyPressEvent
  from prompt_toolkit.layout.utils import explode_text_fragments
  from prompt_toolkit.mouse_events import MouseEvent, MouseEventType
  from prompt_toolkit.utils import get_cwidth
  from .containers import ConditionalContainer, HSplit, ScrollOffsets, Window
  from .controls import GetLinePrefixCallable, UIContent, UIControl
  from .dimension import Dimension
  from .margins import ScrollbarMargin
label_desc: Use image node_id 2 for calling a global function with example usage: _trim_formatted_text(formatted_text, max_width) and returns: result,  unknown, formatted_text,  width