diff options
author | Dylan Baker <dylan@pnwbakers.com> | 2017-01-20 16:01:25 -0800 |
---|---|---|
committer | Dylan Baker <dylan@pnwbakers.com> | 2017-01-27 10:34:45 -0800 |
commit | bef724a5d142a1ca06476d56015a6c6bcdf89f1b (patch) | |
tree | c8f650a4d68ed47fef3a4cf758fa7b6c9919ef4a /alot/widgets/globals.py | |
parent | 7fd9b50a7f43821c6931e4f19efc37c138d77837 (diff) |
Replace cmp with rich comparisons
This is both a performance issue (since cmp is slower than rich
comparisons), and a python3 issue since cmp (and __cmp__) are gone in
python 3.
Diffstat (limited to 'alot/widgets/globals.py')
-rw-r--r-- | alot/widgets/globals.py | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/alot/widgets/globals.py b/alot/widgets/globals.py index 822d0ae9..c4f7379c 100644 --- a/alot/widgets/globals.py +++ b/alot/widgets/globals.py @@ -7,6 +7,7 @@ This contains alot-specific :class:`urwid.Widget` used in more than one mode. """ from __future__ import absolute_import +import functools import re import operator import urwid @@ -271,6 +272,7 @@ class HeadersList(urwid.WidgetWrap): return headerlines +@functools.total_ordering class TagWidget(urwid.AttrMap): """ text widget that renders a tagstring. @@ -313,3 +315,29 @@ class TagWidget(urwid.AttrMap): def set_unfocussed(self): self.set_attr_map(self.attmaps['normal']) + + def __lt__(self, other): + """Groups tags of 1 character first, then alphabetically. + + This groups tags unicode characters at the begnining. + """ + if not isinstance(other, TagWidget): + return NotImplemented + + self_len = len(self.translated) + oth_len = len(other.translated) + if min(self_len, oth_len) == 1 and max(self_len, oth_len) > 1: + return self_len < oth_len + return self.translated.lower() < other.translated.lower() + + def __eq__(self, other): + if not isinstance(other, TagWidget): + return NotImplemented + if len(self.translated) != len(other.translated): + return False + return self.translated.lower() == other.translated.lower() + + def __ne__(self, other): + if not isinstance(other, TagWidget): + return NotImplemented + return self.translated.lower() != other.translated.lower() |