code: def _trim_text(text, max_width):
      """
      Trim the text to `max_width`, append dots when the text is too long.
      Returns (text, width) tuple.
      """
      width = get_cwidth(text)
  
      # When the text is too wide, trim it.
      if width > max_width:
          # When there are no double width characters, just use slice operation.
          if len(text) == width:
              trimmed_text = (text[: max(1, max_width - 3)] + "...")[
                  :max_width
              ]
              return trimmed_text, len(trimmed_text)
  
          # Otherwise, loop until we have the desired width. (Rather
          # inefficient, but ok for now.)
          else:
              trimmed_text = ""
              for c in text:
                  if get_cwidth(trimmed_text + c) <= max_width - 3:
                      trimmed_text += c
              trimmed_text += "..."
  
              return (trimmed_text, get_cwidth(trimmed_text))
      else:
          return text, width
  
imports: from __future__ import unicode_literals
  from six.moves import zip_longest, range
  from prompt_toolkit.filters import HasCompletions, IsDone, Condition, to_cli_filter
  from prompt_toolkit.mouse_events import MouseEventType
  from prompt_toolkit.token import Token
  from prompt_toolkit.utils import get_cwidth
  from .containers import Window, HSplit, ConditionalContainer, ScrollOffsets
  from .controls import UIControl, UIContent
  from .dimension import LayoutDimension
  from .margins import ScrollbarMargin
  from .screen import Point, Char
  import math
label_desc: Use image node_id 1 for calling a global function with example usage: _trim_text(text, max_width) and returns: text,  width, trimmed_text,  len, trimmed_text,  get_cwidth