summaryrefslogtreecommitdiff
path: root/alot/widgets.py
blob: 2a7eee692f0cbd903c3f61983107ed8804dd88cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
"""
This file is part of alot.

Alot is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.

Notmuch is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with notmuch.  If not, see <http://www.gnu.org/licenses/>.

Copyright (C) 2011 Patrick Totzke <patricktotzke@gmail.com>
"""
import email
from urwid import Text
from urwid import Edit
from urwid import Pile
from urwid import Columns
from urwid import AttrMap
from urwid import WidgetWrap
from urwid import ListBox
from urwid import SimpleListWalker
from datetime import datetime

import settings
from helper import shorten
from helper import pretty_datetime


class ThreadlineWidget(AttrMap):
    def __init__(self, tid, dbman):
        self.dbman = dbman
        self.thread = dbman.get_thread(tid)
        self.rebuild()
        AttrMap.__init__(self, self.columns, 'threadline', 'threadline_focus')

    def rebuild(self):
        cols = []
        datestring = pretty_datetime(self.thread.get_newest_date())
        self.date_w = AttrMap(Text(datestring), 'threadline_date')
        cols.append(('fixed', len(datestring), self.date_w))

        mailcountstring = "(%d)" % self.thread.get_total_messages()
        self.mailcount_w = AttrMap(Text(mailcountstring),
                                   'threadline_mailcount')
        cols.append(('fixed', len(mailcountstring), self.mailcount_w))

        tagsstring = " ".join(self.thread.get_tags())
        self.tags_w = AttrMap(Text(tagsstring), 'threadline_tags')
        if tagsstring:
            cols.append(('fixed', len(tagsstring), self.tags_w))

        authors = self.thread.get_authors() or '(None)'
        authorsstring = shorten(authors, settings.authors_maxlength)
        self.authors_w = AttrMap(Text(authorsstring), 'threadline_authors')
        cols.append(('fixed', len(authorsstring), self.authors_w))

        subjectstring = self.thread.get_subject()
        self.subject_w = AttrMap(Text(subjectstring, wrap='clip'),
                                 'threadline_subject')
        if subjectstring:
            cols.append(self.subject_w)

        self.columns = Columns(cols, dividechars=1)
        self.original_widget = self.columns

    def render(self, size, focus=False):
        if focus:
            self.date_w.set_attr_map({None: 'threadline_date_linefocus'})
            self.mailcount_w.set_attr_map({None:
                                           'threadline_mailcount_linefocus'})
            self.tags_w.set_attr_map({None: 'threadline_tags_linefocus'})
            self.authors_w.set_attr_map({None: 'threadline_authors_linefocus'})
            self.subject_w.set_attr_map({None: 'threadline_subject_linefocus'})
        else:
            self.date_w.set_attr_map({None: 'threadline_date'})
            self.mailcount_w.set_attr_map({None: 'threadline_mailcount'})
            self.tags_w.set_attr_map({None: 'threadline_tags'})
            self.authors_w.set_attr_map({None: 'threadline_authors'})
            self.subject_w.set_attr_map({None: 'threadline_subject'})
        return AttrMap.render(self, size, focus)

    def selectable(self):
        return True

    def keypress(self, size, key):
        return key

    def get_thread(self):
        return self.thread


class BufferlineWidget(Text):
    def __init__(self, buffer):
        self.buffer = buffer
        Text.__init__(self, str(buffer), wrap='clip')

    def selectable(self):
        return True

    def keypress(self, size, key):
        return key

    def get_buffer(self):
        return self.buffer


class TagWidget(Text):
    def __init__(self, tag):
        self.tag = tag
        Text.__init__(self, tag, wrap='clip')

    def selectable(self):
        return True

    def keypress(self, size, key):
        return key

    def get_tag(self):
        return self.tag


class PromptWidget(AttrMap):
    def __init__(self, prefix, text='', completer=None):
        self.completer = completer
        leftpart = Text(prefix, align='left')
        self.editpart = Edit(edit_text=text)
        self.start_completion_pos = len(text)
        self.completion_results = None
        both = Columns(
            [
                ('fixed', len(prefix) + 1, leftpart),
                ('weight', 1, self.editpart),
            ])
        AttrMap.__init__(self, both, 'prompt', 'prompt')

    def set_input(self, txt):
        return self.editpart.set_edit_text(txt)

    def get_input(self):
        return self.editpart.get_edit_text()

    def keypress(self, size, key):
        if key in ['tab', 'shift tab']:
            if self.completer:
                pos = self.start_completion_pos
                original = self.editpart.edit_text[:pos]
                if not self.completion_results: #not already in completion mode
                    self.completion_results = [''] + self.completer.complete(original)
                    self.focus_in_clist = 1
                else:
                    if key == 'tab':
                        self.focus_in_clist += 1
                    else:
                        self.focus_in_clist -= 1
                if len(self.completion_results) > 1:
                    suffix = self.completion_results[self.focus_in_clist %
                                              len(self.completion_results)]
                    self.editpart.set_edit_text(original + suffix)
                    self.editpart.edit_pos += len(suffix)
                else:
                    self.editpart.set_edit_text(original + ' ')
                    self.editpart.edit_pos += 1
                    self.start_completion_pos = self.editpart.edit_pos
                    self.completion_results = None
        else:
            result = self.editpart.keypress(size, key)
            self.start_completion_pos = self.editpart.edit_pos
            self.completion_results = None
            return result


class MessageWidget(WidgetWrap):
    def __init__(self, message, folded=True):
        self.message = message
        self.sumw = MessageSummaryWidget(self.message)
        self.headerw = MessageHeaderWidget(self.message.get_email())
        self.bodyw = MessageBodyWidget(self.message.get_email())
        self.displayed_list = [self.sumw]
        if not folded:
            self.displayed_list.append(self.bodyw)
        self.body = Pile(self.displayed_list)
        WidgetWrap.__init__(self, self.body)

    def rebuild(self):
        self.body = Pile(self.displayed_list)
        self._w = self.body

    def toggle_header(self):
        if self.headerw in self.displayed_list:
            self.displayed_list.remove(self.headerw)
        else:
            self.displayed_list.insert(1, self.headerw)
        self.rebuild()

    def toggle_body(self):
        if self.bodyw in self.displayed_list:
            self.displayed_list.remove(self.bodyw)
        else:
            self.displayed_list.append(self.bodyw)
        self.sumw.toggle_folded()
        self.rebuild()

    def selectable(self):
        return True

    def keypress(self, size, key):
        if key == 'h':
            self.toggle_header()
        elif key == 'enter':
            self.toggle_body()
        else:
            return self.body.keypress(size, key)

    def get_message(self):
        return self.message

    def get_email(self):
        return self.message.get_email()


class MessageSummaryWidget(WidgetWrap):
    def __init__(self, message, folded=True):
        self.message = message
        self.folded = folded
        WidgetWrap.__init__(self, Text(str(self)))

    def __str__(self):
        prefix = "-"
        if self.folded:
            prefix = '+'
        return prefix + str(self.message)

    def toggle_folded(self):
        self.folded = not self.folded
        self._w = Text(str(self))

    def selectable(self):
        return True

    def keypress(self, size, key):
        return key


class MessageHeaderWidget(AttrMap):
    def __init__(self, eml):
        self.eml = eml
        headerlines = []
        for line in settings.displayed_headers:
            if line in eml:
                headerlines.append('%s:%s' % (line, eml.get(line)))
        headertxt = '\n'.join(headerlines)
        AttrMap.__init__(self, Text(headertxt), 'message_header')

    def selectable(self):
        return True

    def keypress(self, size, key):
        return key


class MessageBodyWidget(AttrMap):
    def __init__(self, eml):
        self.eml = eml
        bodytxt = ''.join(email.iterators.body_line_iterator(self.eml))
        AttrMap.__init__(self, Text(bodytxt), 'message_body')

    def selectable(self):
        return True

    def keypress(self, size, key):
        return key