diff options
author | Patrick Totzke <patricktotzke@gmail.com> | 2011-09-26 17:11:51 +0100 |
---|---|---|
committer | Patrick Totzke <patricktotzke@gmail.com> | 2011-09-26 17:11:51 +0100 |
commit | 0da0171a191d4c54ea02fd722c57f1e7195eb65d (patch) | |
tree | 017273f89de14c21f7b1bedaaaf1edae5fe5903b /alot | |
parent | 62ba57c9fbcd5975d2a5c6f7d02bba8075c75336 (diff) |
fancy shortening for authors list
issue #67
Diffstat (limited to 'alot')
-rw-r--r-- | alot/helper.py | 95 | ||||
-rw-r--r-- | alot/widgets.py | 4 |
2 files changed, 93 insertions, 6 deletions
diff --git a/alot/helper.py b/alot/helper.py index 91da0811..606e4f4a 100644 --- a/alot/helper.py +++ b/alot/helper.py @@ -18,7 +18,8 @@ Copyright (C) 2011 Patrick Totzke <patricktotzke@gmail.com> """ from datetime import date from datetime import timedelta - +from collections import deque +from string import strip import shlex import subprocess import email @@ -32,9 +33,95 @@ from email.mime.multipart import MIMEMultipart def shorten(string, maxlen): - if len(string) > maxlen - 3: - string = string[:maxlen - 3] + u'\u2026' - return string + if maxlen > 1 and len(string) > maxlen: + string = string[:maxlen - 1] + u'\u2026' + return string[:maxlen] + + +def shorten_author_string(authors_string, maxlength): + """ + Parse a list of authors concatenated as a text string (comma + separated) and smartly adjust them to maxlength. + + 1) If the complete list of sender names does not fit in maxlength, it + tries to shorten names by using only the first part of each. + + 2) If the list is still too long, hide authors according to the + following priority: + + - First author is always shown (if too long is shorten with ellipsis) + + - If possible, last author is also shown (if too long, uses + ellipsis) + + - If there are more than 2 authors in the thread, show the + maximum of them. More recent senders have more priority (Is + the list of authors already sorted by the date of msgs????) + + - If it is finally necessary to hide any author, an ellipsis + between first and next authors is added. + + + EXAMPLE (authors string with different length constrains): + 'King Kong, Mucho Muchacho, Jaime Huerta, Flash Gordon' + 'King, Mucho, Jaime, Flash' + 'King, ., Jaime, Flash' + 'King, ., J., Flash' + 'King, ., Flash' + 'King, ., Fl.' + 'King, .' + 'K., .' + 'K.' + """ + + # I will create a list of authors by parsing author_string. I use + # deque to do popleft without performance penalties + authors = deque() + + # If author list is too long, it uses only the first part of each + # name (gmail style) + short_names = len(authors_string) > maxlength + for au in authors_string.split(", "): + if short_names: + authors.append(strip(au.split()[0])) + else: + authors.append(au) + + # Author chain will contain the list of author strings to be + # concatenated using commas for the final formatted author_string. + authors_chain = deque() + + # reserve space for first author + first_au = shorten(authors.popleft(), maxlength) + remaining_length = maxlength - len(first_au) + + # Tries to add an ellipsis if no space to show more than 1 author + if authors and maxlength > 3 and remaining_length < 3: + first_au = shorten(first_au, maxlength - 3) + remaining_length += 3 + + # Tries to add as more authors as possible. It takes into account + # that if any author will be hidden, and ellipsis should be added + while authors and remaining_length >= 3: + au = authors.pop() + if len(au) > 1 and (remaining_length == 3 or + (authors and remaining_length < 7)): + authors_chain.appendleft(u'\u2026') + break + else: + if authors: + # 5= ellipsis + 2 x comma and space used as separators + au_string = shorten(au, remaining_length - 5) + else: + # 2 = comma and space used as separator + au_string = shorten(au, remaining_length - 2) + remaining_length -= len(au_string) + 2 + authors_chain.appendleft(au_string) + + # Add the first author to the list and concatenate list + authors_chain.appendleft(first_au) + authorsstring = ', '.join(authors_chain) + return authorsstring def pretty_datetime(d): diff --git a/alot/widgets.py b/alot/widgets.py index 1645aff5..badc4fa5 100644 --- a/alot/widgets.py +++ b/alot/widgets.py @@ -22,7 +22,7 @@ from urwid.command_map import command_map import logging from settings import config -from helper import shorten +from helper import shorten_author_string from helper import pretty_datetime import message @@ -63,7 +63,7 @@ class ThreadlineWidget(urwid.AttrMap): authors = self.thread.get_authors() or '(None)' maxlength = config.getint('general', 'authors_maxlength') - authorsstring = shorten(authors, maxlength) + authorsstring = shorten_author_string(authors, maxlength) self.authors_w = urwid.AttrMap(urwid.Text(authorsstring), 'threadline_authors') cols.append(('fixed', len(authorsstring), self.authors_w)) |