Skip to content
Snippets Groups Projects
Legend.py 624 B
from GEODE.Functional import curry

@curry
def take(maxWidth, shards):
    result = []
    budget = maxWidth
    i = 0
    while i < len(shards) and budget > 1:
        shard = shards[i]
        if len(shard) > budget:
            shard = f"{shard[:budget-1]}."
        result.append(shard)
        budget -= len(shard) + 1
        i += 1
    return tuple(result)

def trim(labels, maxWidth=10):
    if maxWidth is None:
        return labels
    else:
        shards = [label.split(' ') for label in labels]
        prefixes = [*map(take(maxWidth), shards)]
        return [' '.join(prefix) for prefix in sorted(prefixes)]