#: Shell param checker regexp
_SHELLPARAM_REGEX = re.compile(r"^[-a-zA-Z0-9._+/:%@]+$")
+#: ASCII equivalent of unicode character 'HORIZONTAL ELLIPSIS' (U+2026)
+_ASCII_ELLIPSIS = "..."
+
def MatchNameComponent(key, name_list, case_sensitive=True):
"""Try to match a name against a list.
"""
assert self._indent >= 0
- self._fh.write(self._indent * self.INDENT_STR)
-
if args:
- self._fh.write(txt % args)
+ line = txt % args
else:
- self._fh.write(txt)
+ line = txt
+
+ if line:
+ # Indent only if there's something on the line
+ self._fh.write(self._indent * self.INDENT_STR)
+
+ self._fh.write(line)
self._fh.write("\n")
suffix = "th"
return "%s%s" % (value, suffix)
+
+
+def Truncate(text, length):
+ """Truncate string and add ellipsis if needed.
+
+ @type text: string
+ @param text: Text
+ @type length: integer
+ @param length: Desired length
+ @rtype: string
+ @return: Truncated text
+
+ """
+ assert length > len(_ASCII_ELLIPSIS)
+
+ # Serialize if necessary
+ if not isinstance(text, basestring):
+ text = str(text)
+
+ if len(text) <= length:
+ return text
+ else:
+ return text[:length - len(_ASCII_ELLIPSIS)] + _ASCII_ELLIPSIS