From 75ee7826e16a63f296a04774cf49fb855f70f69c Mon Sep 17 00:00:00 2001 From: Kevin Kuehler Date: Mon, 28 Oct 2019 12:07:02 -0700 Subject: widgets/msglist: Draw each row in a function This function can be reused by the threading code. Signed-off-by: Kevin Kuehler --- widgets/msglist.go | 98 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 54 insertions(+), 44 deletions(-) diff --git a/widgets/msglist.go b/widgets/msglist.go index 5c2d2f4..ac3d6cc 100644 --- a/widgets/msglist.go +++ b/widgets/msglist.go @@ -71,52 +71,9 @@ func (ml *MessageList) Draw(ctx *ui.Context) { for i := len(uids) - 1 - ml.scroll; i >= 0; i-- { uid := uids[i] - msg := store.Messages[uid] - - if row >= ctx.Height() { + if ml.drawRow(ctx, uid, i, row, &needsHeaders) { break } - - if msg == nil { - needsHeaders = append(needsHeaders, uid) - ml.spinner.Draw(ctx.Subcontext(0, row, ctx.Width(), 1)) - row += 1 - continue - } - - style := tcell.StyleDefault - - // current row - if row == ml.store.SelectedIndex()-ml.scroll { - style = style.Reverse(true) - } - // deleted message - if _, ok := store.Deleted[msg.Uid]; ok { - style = style.Foreground(tcell.ColorGray) - } - // unread message - seen := false - for _, flag := range msg.Flags { - if flag == models.SeenFlag { - seen = true - } - } - if !seen { - style = style.Bold(true) - } - - ctx.Fill(0, row, ctx.Width(), 1, ' ', style) - fmtStr, args, err := format.ParseMessageFormat( - ml.conf.Ui.IndexFormat, - ml.conf.Ui.TimestampFormat, "", i, msg) - if err != nil { - ctx.Printf(0, row, style, "%v", err) - } else { - line := fmt.Sprintf(fmtStr, args...) - line = runewidth.Truncate(line, ctx.Width(), "…") - ctx.Printf(0, row, style, "%s", line) - } - row += 1 } @@ -132,6 +89,59 @@ func (ml *MessageList) Draw(ctx *ui.Context) { } } +// Draw one message. +// +// Return: If true, don't fetch any more messages +func (ml *MessageList) drawRow(ctx *ui.Context, uid uint32, number int, row int, needsHeaders *[]uint32) bool { + store := ml.store + msg := store.Messages[uid] + + if row >= ctx.Height() { + return true + } + + if msg == nil { + *needsHeaders = append(*needsHeaders, uid) + ml.spinner.Draw(ctx.Subcontext(0, row, ctx.Width(), 1)) + return false + } + + style := tcell.StyleDefault + + // current row + if row == ml.store.SelectedIndex()-ml.scroll { + style = style.Reverse(true) + } + // deleted message + if _, ok := store.Deleted[msg.Uid]; ok { + style = style.Foreground(tcell.ColorGray) + } + // unread message + seen := false + for _, flag := range msg.Flags { + if flag == models.SeenFlag { + seen = true + } + } + if !seen { + style = style.Bold(true) + } + + ctx.Fill(0, row, ctx.Width(), 1, ' ', style) + fmtStr, args, err := format.ParseMessageFormat( + ml.conf.Ui.IndexFormat, + ml.conf.Ui.TimestampFormat, "", number, msg) + if err != nil { + ctx.Printf(0, row, style, "%v", err) + } else { + line := fmt.Sprintf(fmtStr, args...) + line = runewidth.Truncate(line, ctx.Width(), "…") + ctx.Printf(0, row, style, "%s", line) + } + + return false +} + func (ml *MessageList) MouseEvent(localX int, localY int, event tcell.Event) { switch event := event.(type) { case *tcell.EventMouse: -- cgit v1.2.3