def trim_string(
    string: str,
    limit: int,
    max_length: int,
    trim_back: bool,
) -> str:
    """Trip a string to fit in viewport.

    Args:
        string: The string to trim
        limit: The available screen estate (horizontal)
        max_length: The length of the longest file name in a directory
        trim_back: Whether to trim the end or the beginning of the string

    Returns:
        Trimmed string
    """
    if len(string) > limit:
        if trim_back:
            return string[0 : limit - 1] + "…"
        else:
            return "…" + string[-limit + 1 :]
    else:
        return string + " " * (min(max_length, limit) - len(string))