def _trim_text(text, max_width):
 width = get_cwidth(text)
 if (width > max_width):
  if (len(text) == width):
   trimmed_text = (text[:max(1, (max_width - 3))] + u'...')[:max_width]
   return (trimmed_text, len(trimmed_text))
  else:
   trimmed_text = u''
   for c in text:
    if (get_cwidth((trimmed_text + c)) <= (max_width - 3)):
     trimmed_text += c
   trimmed_text += u'...'
   return (trimmed_text, get_cwidth(trimmed_text))
 else:
  return (text, width)

def _trim_text ( text , max_width ) : width = get_cwidth ( text ) if ( width > max_width ) : if ( len ( text ) == width ) : trimmed_text = ( text [ : max ( 1 , ( max_width - 3 ) ) ] + u'...' ) [ : max_width ] return ( trimmed_text , len ( trimmed_text ) ) else : trimmed_text = u'' for c in text : if ( get_cwidth ( ( trimmed_text + c ) ) <= ( max_width - 3 ) ) : trimmed_text += c trimmed_text += u'...' return ( trimmed_text , get_cwidth ( trimmed_text ) ) else : return ( text , width )
trim the text to max_width .