from typing import List
from typing import Any
from typing import Optional


def jinja_calc_buffer(fields: List[Any], category: Optional[str] = None) -> int:
    """calculate buffer for list of fields based on their length"""
    if category:
        fields = [f for f in fields if f.category == category]
    return max(len(f.to_string()) for f in fields)