diff options
author | emkael <emkael@tlen.pl> | 2018-10-02 13:32:08 +0200 |
---|---|---|
committer | emkael <emkael@tlen.pl> | 2018-10-02 13:32:08 +0200 |
commit | b2a6d843d69edc40c8f7782e47db28ca89baf77e (patch) | |
tree | 032c5193ab15cb5e472a0db4dbf555cef8604849 | |
parent | 79c281bb47d08f1fd1be04b1d7d58dfd8c672bc5 (diff) |
Bringing back label shortening, this time compatible with separate team labels
-rw-r--r-- | jfr_playoff/generator.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/jfr_playoff/generator.py b/jfr_playoff/generator.py index 7895a73..beaabd7 100644 --- a/jfr_playoff/generator.py +++ b/jfr_playoff/generator.py @@ -57,6 +57,30 @@ class PlayoffGenerator(object): template = 'MATCH_TEAM_LABEL' return self.p_temp.get(template, team_name) + def __shorten_labels(self, labels, limit, separator, ellipsis): + if limit > 0: + current_length = 0 + shortened = [] + for l in range(0, len(labels)): + if current_length + len(labels[l]) > limit: + # current label won't fit within limit, shorten it and stop + shortened.append(labels[l][0:limit-current_length] + ellipsis) + break + else: + # current label fits, add it to output + shortened.append(labels[l]) + current_length += len(labels[l]) + if l < len(labels) - 1: + # if it's not the last label, separator will be added + # if it was the last label, next condition won't run and ellipsis won't be added + current_length += len(separator) + if current_length > limit: + # if separator puts us over the limit, add ellipsis and stop + shortened.append(ellipsis) + break + labels = shortened + return labels + def get_match_table(self, match): rows = '' for team in match.teams: @@ -97,6 +121,9 @@ class PlayoffGenerator(object): for l in range(0, len(labels)): # fill any remaining empty labels (i.e. these which had empty predictions available) with placeholders labels[l] = coalesce(labels[l], '??') + # shorten concatenated label to specified combined length + labels = self.__shorten_labels(labels, self.page.get('label_length_limit', 0), label_separator, '(...)') + for l in range(0, len(labels)): # concatenate labels, assigning appropriate classes to predicted teams team_label.append(self.__get_team_label( labels[l], |