Source code for chromag.string_helpers
# -*- coding: utf-8 -*-
"""String helper routines.
"""
[docs]
def truncate(
s: str, max_length: int, continuation_chars: str = "...", padding: bool = False
):
"""Truncate a string to a length if necessary, but indicate truncated
strings with the given characters. Also, pad with spaces to
`max_length` if `padding` is set.
"""
if len(s) > max_length:
new_s = s[0 : max_length - len(continuation_chars)] + continuation_chars
return new_s
return s.ljust(max_length, " ") if padding else s