| 1 |
package app |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"sync" |
| 6 |
"time" |
| 7 |
|
| 8 |
"github.com/mattn/go-runewidth" |
| 9 |
|
| 10 |
"git.sr.ht/~rjarry/aerc/config" |
| 11 |
"git.sr.ht/~rjarry/aerc/lib/log" |
| 12 |
"git.sr.ht/~rjarry/aerc/lib/state" |
| 13 |
"git.sr.ht/~rjarry/aerc/lib/templates" |
| 14 |
"git.sr.ht/~rjarry/aerc/lib/ui" |
| 15 |
"go.rockorager.dev/vaxis" |
| 16 |
) |
| 17 |
|
| 18 |
type StatusLine struct { |
| 19 |
sync.Mutex |
| 20 |
stack []*StatusMessage |
| 21 |
acct *AccountView |
| 22 |
err string |
| 23 |
} |
| 24 |
|
| 25 |
type StatusMessage struct { |
| 26 |
style vaxis.Style |
| 27 |
message string |
| 28 |
} |
| 29 |
|
| 30 |
func (status *StatusLine) Invalidate() { |
| 31 |
ui.Invalidate() |
| 32 |
} |
| 33 |
|
| 34 |
func (status *StatusLine) Draw(ctx *ui.Context) { |
| 35 |
status.Lock() |
| 36 |
defer status.Unlock() |
| 37 |
style := status.uiConfig().GetStyle(config.STYLE_STATUSLINE_DEFAULT) |
| 38 |
ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', style) |
| 39 |
switch { |
| 40 |
case len(status.stack) != 0: |
| 41 |
line := status.stack[len(status.stack)-1] |
| 42 |
msg := runewidth.Truncate(line.message, ctx.Width(), "") |
| 43 |
msg = runewidth.FillRight(msg, ctx.Width()) |
| 44 |
ctx.Printf(0, 0, line.style, "%s", msg) |
| 45 |
case status.err != "": |
| 46 |
msg := runewidth.Truncate(status.err, ctx.Width(), "") |
| 47 |
msg = runewidth.FillRight(msg, ctx.Width()) |
| 48 |
style := status.uiConfig().GetStyle(config.STYLE_STATUSLINE_ERROR) |
| 49 |
ctx.Printf(0, 0, style, "%s", msg) |
| 50 |
case status.acct != nil: |
| 51 |
data := state.NewDataSetter() |
| 52 |
data.SetPendingKeys(aerc.pendingKeys) |
| 53 |
data.SetState(&status.acct.state) |
| 54 |
data.SetAccount(status.acct.acct) |
| 55 |
data.SetFolder(status.acct.Directories().SelectedDirectory()) |
| 56 |
msg, _ := status.acct.SelectedMessage() |
| 57 |
data.SetInfo(msg, 0, false) |
| 58 |
data.SetRUE(status.acct.dirlist.List(), status.acct.dirlist.GetRUECount) |
| 59 |
if store := status.acct.Store(); store != nil { |
| 60 |
data.SetVisual(store.Marker().IsVisualMark()) |
| 61 |
} |
| 62 |
table := ui.NewTable( |
| 63 |
ctx.Height(), |
| 64 |
config.Statusline().StatusColumns, |
| 65 |
config.Statusline().ColumnSeparator, |
| 66 |
nil, |
| 67 |
func(*ui.Table, int) vaxis.Style { return style }, |
| 68 |
) |
| 69 |
var buf bytes.Buffer |
| 70 |
cells := make([]string, len(table.Columns)) |
| 71 |
for c, col := range table.Columns { |
| 72 |
err := templates.Render(col.Def.Template, &buf, |
| 73 |
data.Data()) |
| 74 |
if err != nil { |
| 75 |
log.Errorf("%s", err) |
| 76 |
cells[c] = err.Error() |
| 77 |
} else { |
| 78 |
cells[c] = buf.String() |
| 79 |
} |
| 80 |
buf.Reset() |
| 81 |
} |
| 82 |
table.AddRow(cells, nil) |
| 83 |
table.Draw(ctx) |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
func (status *StatusLine) Update(acct *AccountView) { |
| 88 |
status.acct = acct |
| 89 |
status.Invalidate() |
| 90 |
} |
| 91 |
|
| 92 |
func (status *StatusLine) SetError(err string) { |
| 93 |
prev := status.err |
| 94 |
status.err = err |
| 95 |
if prev != status.err { |
| 96 |
status.Invalidate() |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
func (status *StatusLine) Clear() { |
| 101 |
status.SetError("") |
| 102 |
status.acct = nil |
| 103 |
} |
| 104 |
|
| 105 |
func (status *StatusLine) Push(text string, expiry time.Duration) *StatusMessage { |
| 106 |
status.Lock() |
| 107 |
defer status.Unlock() |
| 108 |
log.Debugf(text) |
| 109 |
msg := &StatusMessage{ |
| 110 |
style: status.uiConfig().GetStyle(config.STYLE_STATUSLINE_DEFAULT), |
| 111 |
message: text, |
| 112 |
} |
| 113 |
status.stack = append(status.stack, msg) |
| 114 |
go (func() { |
| 115 |
defer log.PanicHandler() |
| 116 |
|
| 117 |
time.Sleep(expiry) |
| 118 |
status.Lock() |
| 119 |
defer status.Unlock() |
| 120 |
for i, m := range status.stack { |
| 121 |
if m == msg { |
| 122 |
status.stack = append(status.stack[:i], status.stack[i+1:]...) |
| 123 |
break |
| 124 |
} |
| 125 |
} |
| 126 |
status.Invalidate() |
| 127 |
})() |
| 128 |
status.Invalidate() |
| 129 |
return msg |
| 130 |
} |
| 131 |
|
| 132 |
func (status *StatusLine) PushError(text string) *StatusMessage { |
| 133 |
log.Errorf(text) |
| 134 |
msg := status.Push(text, 10*time.Second) |
| 135 |
msg.Color(status.uiConfig().GetStyle(config.STYLE_STATUSLINE_ERROR)) |
| 136 |
return msg |
| 137 |
} |
| 138 |
|
| 139 |
func (status *StatusLine) PushWarning(text string) *StatusMessage { |
| 140 |
log.Warnf(text) |
| 141 |
msg := status.Push(text, 10*time.Second) |
| 142 |
msg.Color(status.uiConfig().GetStyle(config.STYLE_STATUSLINE_WARNING)) |
| 143 |
return msg |
| 144 |
} |
| 145 |
|
| 146 |
func (status *StatusLine) PushSuccess(text string) *StatusMessage { |
| 147 |
log.Tracef(text) |
| 148 |
msg := status.Push(text, 10*time.Second) |
| 149 |
msg.Color(status.uiConfig().GetStyle(config.STYLE_STATUSLINE_SUCCESS)) |
| 150 |
return msg |
| 151 |
} |
| 152 |
|
| 153 |
func (status *StatusLine) Expire() { |
| 154 |
status.Lock() |
| 155 |
defer status.Unlock() |
| 156 |
status.stack = nil |
| 157 |
} |
| 158 |
|
| 159 |
func (status *StatusLine) uiConfig() *config.UIConfig { |
| 160 |
return SelectedAccountUiConfig() |
| 161 |
} |
| 162 |
|
| 163 |
func (msg *StatusMessage) Color(style vaxis.Style) { |
| 164 |
msg.style = style |
| 165 |
} |