code: def trunc(text, length):
      
      if length < 1:
          raise ValueError("length should be 1 or larger")
  
      # Remove whitespace first so no unneccesary truncation is done.
      text = text.strip()
      text_length = wcswidth(text)
  
      if text_length <= length:
          return text
  
      # We cannot just remove n characters from the end since we don't know how
      # wide these characters are and how it will affect text length.
      # Use wcwidth to determine how many characters need to be truncated.
      chars_to_truncate = 0
      trunc_length = 0
      for char in reversed(text):
          chars_to_truncate += 1
          trunc_length += wcwidth(char)
          if text_length - trunc_length <= length:
              break
  
      # Additional char to make room for elipsis
      n = chars_to_truncate + 1
      return text[:-n].strip() + '…'
docs: Truncates text to given length, taking into account wide characters.
  
      If truncated, the last char is replaced by an elipsis.