Revision 5401c39d lib/utils/text.py

b/lib/utils/text.py
43 43
#: Shell param checker regexp
44 44
_SHELLPARAM_REGEX = re.compile(r"^[-a-zA-Z0-9._+/:%@]+$")
45 45

  
46
#: ASCII equivalent of unicode character 'HORIZONTAL ELLIPSIS' (U+2026)
47
_ASCII_ELLIPSIS = "..."
48

  
46 49

  
47 50
def MatchNameComponent(key, name_list, case_sensitive=True):
48 51
  """Try to match a name against a list.
......
556 559
    suffix = "th"
557 560

  
558 561
  return "%s%s" % (value, suffix)
562

  
563

  
564
def Truncate(text, length):
565
  """Truncate string and add ellipsis if needed.
566

  
567
  @type text: string
568
  @param text: Text
569
  @type length: integer
570
  @param length: Desired length
571
  @rtype: string
572
  @return: Truncated text
573

  
574
  """
575
  assert length > len(_ASCII_ELLIPSIS)
576

  
577
  # Serialize if necessary
578
  if not isinstance(text, basestring):
579
    text = str(text)
580

  
581
  if len(text) <= length:
582
    return text
583
  else:
584
    return text[:length - len(_ASCII_ELLIPSIS)] + _ASCII_ELLIPSIS

Also available in: Unified diff