# Path: alot/helper.py
# 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 higher priority.
# 
#       - If it is finally necessary to hide any author, an ellipsis
#         between first and next authors is added.
#     """
# 
#     # 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:
#             author_as_list = au.split()
#             if len(author_as_list) > 0:
#                 authors.append(author_as_list[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()
# 
#     if len(authors) == 0:
#         return ''
# 
#     # 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('…')
#             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


import urwid
from ..settings.const import settings
from ..helper import shorten_author_string
from .utils import AttrFlipWidget
from .globals import TagWidget

def prepare_mailcount_string(thread):
    return "(%d)" % thread.get_total_messages()


def prepare_authors_string(thread):
    return thread.get_authors_string() or '(None)'


def prepare_subject_string(thread):
    return thread.get_subject() or ' '


def prepare_content_string(thread):
    msgs = sorted(thread.get_messages().keys(),
                  key=lambda msg: msg.get_date(), reverse=True)
    lastcontent = ' '.join(m.get_body_text() for m in msgs)
    lastcontent = lastcontent.replace('^>.*$', '')
    return lastcontent


def prepare_string(partname, thread, maxw):
    """
    extract a content string for part 'partname' from 'thread' of maximal
    length 'maxw'.
    """
    # map part names to function extracting content string and custom shortener
    prep = {
        'mailcount': (prepare_mailcount_string, None),
        'date': (prepare_date_string, None),

# Path: alot/helper.py
# 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 higher priority.
# 
#       - If it is finally necessary to hide any author, an ellipsis
#         between first and next authors is added.
#     """
# 
#     # 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:
#             author_as_list = au.split()
#             if len(author_as_list) > 0:
#                 authors.append(author_as_list[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()
# 
#     if len(authors) == 0:
#         return ''
# 
#     # 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('…')
#             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


# Path: alot/widgets/search.py
import urwid
from ..settings.const import settings
from ..helper import shorten_author_string
from .utils import AttrFlipWidget
from .globals import TagWidget


def prepare_mailcount_string(thread):
    return "(%d)" % thread.get_total_messages()


def prepare_authors_string(thread):
    return thread.get_authors_string() or '(None)'


def prepare_subject_string(thread):
    return thread.get_subject() or ' '


def prepare_content_string(thread):
    msgs = sorted(thread.get_messages().keys(),
                  key=lambda msg: msg.get_date(), reverse=True)
    lastcontent = ' '.join(m.get_body_text() for m in msgs)
    lastcontent = lastcontent.replace('^>.*$', '')
    return lastcontent


def prepare_string(partname, thread, maxw):
    """
    extract a content string for part 'partname' from 'thread' of maximal
    length 'maxw'.
    """
    # map part names to function extracting content string and custom shortener
    prep = {
        'mailcount': (prepare_mailcount_string, None),
        'date': (prepare_date_string, None),

        'authors': (prepare_authors_string, shorten_author_string),