2018-02-28 02:17:26 +00:00
|
|
|
package widgets
|
|
|
|
|
|
|
|
import (
|
2019-07-19 18:15:48 +00:00
|
|
|
"errors"
|
2018-02-28 02:17:26 +00:00
|
|
|
"log"
|
2019-07-19 18:15:48 +00:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
2019-03-17 20:19:15 +00:00
|
|
|
"time"
|
2018-02-28 02:17:26 +00:00
|
|
|
|
2018-06-01 07:58:00 +00:00
|
|
|
"github.com/gdamore/tcell"
|
2019-07-21 20:01:51 +00:00
|
|
|
"github.com/google/shlex"
|
2018-02-28 02:17:26 +00:00
|
|
|
|
2019-05-18 00:57:10 +00:00
|
|
|
"git.sr.ht/~sircmpwn/aerc/config"
|
|
|
|
"git.sr.ht/~sircmpwn/aerc/lib/ui"
|
|
|
|
libui "git.sr.ht/~sircmpwn/aerc/lib/ui"
|
2018-02-28 02:17:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Aerc struct {
|
2019-03-17 20:19:15 +00:00
|
|
|
accounts map[string]*AccountView
|
2019-07-21 20:01:51 +00:00
|
|
|
cmd func(cmd []string) error
|
2019-06-27 17:33:11 +00:00
|
|
|
complete func(cmd string) []string
|
2019-03-17 20:19:15 +00:00
|
|
|
conf *config.AercConfig
|
|
|
|
focused libui.Interactive
|
|
|
|
grid *libui.Grid
|
|
|
|
logger *log.Logger
|
2019-03-21 21:49:59 +00:00
|
|
|
simulating int
|
2019-03-17 20:19:15 +00:00
|
|
|
statusbar *libui.Stack
|
|
|
|
statusline *StatusLine
|
|
|
|
pendingKeys []config.KeyStroke
|
|
|
|
tabs *libui.Tabs
|
2018-02-28 02:17:26 +00:00
|
|
|
}
|
|
|
|
|
2019-03-11 01:15:24 +00:00
|
|
|
func NewAerc(conf *config.AercConfig, logger *log.Logger,
|
2019-07-21 20:01:51 +00:00
|
|
|
cmd func(cmd []string) error, complete func(cmd string) []string) *Aerc {
|
2019-03-11 01:15:24 +00:00
|
|
|
|
2018-02-28 02:17:26 +00:00
|
|
|
tabs := libui.NewTabs()
|
|
|
|
|
2019-03-17 20:19:15 +00:00
|
|
|
statusbar := ui.NewStack()
|
|
|
|
statusline := NewStatusLine()
|
|
|
|
statusbar.Push(statusline)
|
|
|
|
|
|
|
|
grid := libui.NewGrid().Rows([]libui.GridSpec{
|
2018-06-12 13:50:46 +00:00
|
|
|
{libui.SIZE_EXACT, 1},
|
|
|
|
{libui.SIZE_WEIGHT, 1},
|
2019-03-17 20:19:15 +00:00
|
|
|
{libui.SIZE_EXACT, 1},
|
2018-02-28 02:17:26 +00:00
|
|
|
}).Columns([]libui.GridSpec{
|
2018-06-12 13:50:46 +00:00
|
|
|
{libui.SIZE_WEIGHT, 1},
|
2018-02-28 02:17:26 +00:00
|
|
|
})
|
2019-03-30 18:12:04 +00:00
|
|
|
grid.AddChild(tabs.TabStrip)
|
|
|
|
grid.AddChild(tabs.TabContent).At(1, 0)
|
|
|
|
grid.AddChild(statusbar).At(2, 0)
|
2018-02-28 02:17:26 +00:00
|
|
|
|
2019-01-14 13:14:03 +00:00
|
|
|
aerc := &Aerc{
|
2019-03-17 20:19:15 +00:00
|
|
|
accounts: make(map[string]*AccountView),
|
|
|
|
conf: conf,
|
|
|
|
cmd: cmd,
|
2019-06-27 17:33:11 +00:00
|
|
|
complete: complete,
|
2019-03-17 20:19:15 +00:00
|
|
|
grid: grid,
|
|
|
|
logger: logger,
|
|
|
|
statusbar: statusbar,
|
|
|
|
statusline: statusline,
|
|
|
|
tabs: tabs,
|
2019-01-14 13:14:03 +00:00
|
|
|
}
|
2019-01-13 18:25:56 +00:00
|
|
|
|
2019-07-16 17:43:08 +00:00
|
|
|
statusline.SetAerc(aerc)
|
2019-07-21 20:01:51 +00:00
|
|
|
conf.Triggers.ExecuteCommand = cmd
|
2019-07-16 17:43:08 +00:00
|
|
|
|
2019-05-22 14:39:52 +00:00
|
|
|
for i, acct := range conf.Accounts {
|
|
|
|
view := NewAccountView(conf, &conf.Accounts[i], logger, aerc)
|
2019-01-14 13:14:03 +00:00
|
|
|
aerc.accounts[acct.Name] = view
|
2019-01-13 18:03:28 +00:00
|
|
|
tabs.Add(view, acct.Name)
|
2018-06-11 23:23:09 +00:00
|
|
|
}
|
2018-02-28 02:17:26 +00:00
|
|
|
|
2019-05-22 15:35:55 +00:00
|
|
|
if len(conf.Accounts) == 0 {
|
|
|
|
wizard := NewAccountWizard(aerc.Config(), aerc)
|
|
|
|
wizard.Focus(true)
|
|
|
|
aerc.NewTab(wizard, "New account")
|
|
|
|
}
|
|
|
|
|
2019-01-14 13:14:03 +00:00
|
|
|
return aerc
|
2018-02-28 02:17:26 +00:00
|
|
|
}
|
|
|
|
|
2019-05-19 09:49:57 +00:00
|
|
|
func (aerc *Aerc) Tick() bool {
|
|
|
|
more := false
|
|
|
|
for _, acct := range aerc.accounts {
|
|
|
|
more = acct.Tick() || more
|
|
|
|
}
|
|
|
|
return more
|
|
|
|
}
|
|
|
|
|
2019-02-10 21:46:13 +00:00
|
|
|
func (aerc *Aerc) Children() []ui.Drawable {
|
2019-01-20 20:08:30 +00:00
|
|
|
return aerc.grid.Children()
|
|
|
|
}
|
|
|
|
|
2018-02-28 02:17:26 +00:00
|
|
|
func (aerc *Aerc) OnInvalidate(onInvalidate func(d libui.Drawable)) {
|
2019-01-13 18:25:56 +00:00
|
|
|
aerc.grid.OnInvalidate(func(_ libui.Drawable) {
|
|
|
|
onInvalidate(aerc)
|
|
|
|
})
|
2018-02-28 02:17:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (aerc *Aerc) Invalidate() {
|
|
|
|
aerc.grid.Invalidate()
|
|
|
|
}
|
|
|
|
|
2019-03-17 18:02:33 +00:00
|
|
|
func (aerc *Aerc) Focus(focus bool) {
|
|
|
|
// who cares
|
|
|
|
}
|
|
|
|
|
2018-02-28 02:17:26 +00:00
|
|
|
func (aerc *Aerc) Draw(ctx *libui.Context) {
|
|
|
|
aerc.grid.Draw(ctx)
|
|
|
|
}
|
|
|
|
|
2019-03-21 21:36:42 +00:00
|
|
|
func (aerc *Aerc) getBindings() *config.KeyBindings {
|
2019-05-14 18:27:28 +00:00
|
|
|
switch view := aerc.SelectedTab().(type) {
|
2019-03-21 21:36:42 +00:00
|
|
|
case *AccountView:
|
|
|
|
return aerc.conf.Bindings.MessageList
|
2019-05-21 20:31:04 +00:00
|
|
|
case *AccountWizard:
|
|
|
|
return aerc.conf.Bindings.AccountWizard
|
2019-05-12 04:06:09 +00:00
|
|
|
case *Composer:
|
2019-05-14 18:27:28 +00:00
|
|
|
switch view.Bindings() {
|
|
|
|
case "compose::editor":
|
|
|
|
return aerc.conf.Bindings.ComposeEditor
|
|
|
|
case "compose::review":
|
|
|
|
return aerc.conf.Bindings.ComposeReview
|
|
|
|
default:
|
|
|
|
return aerc.conf.Bindings.Compose
|
|
|
|
}
|
2019-03-31 01:45:41 +00:00
|
|
|
case *MessageViewer:
|
|
|
|
return aerc.conf.Bindings.MessageView
|
2019-03-30 18:12:04 +00:00
|
|
|
case *Terminal:
|
2019-03-21 21:44:44 +00:00
|
|
|
return aerc.conf.Bindings.Terminal
|
2019-03-21 21:36:42 +00:00
|
|
|
default:
|
|
|
|
return aerc.conf.Bindings.Global
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (aerc *Aerc) simulate(strokes []config.KeyStroke) {
|
|
|
|
aerc.pendingKeys = []config.KeyStroke{}
|
2019-03-21 21:49:59 +00:00
|
|
|
aerc.simulating += 1
|
2019-03-21 21:36:42 +00:00
|
|
|
for _, stroke := range strokes {
|
|
|
|
simulated := tcell.NewEventKey(
|
|
|
|
stroke.Key, stroke.Rune, tcell.ModNone)
|
|
|
|
aerc.Event(simulated)
|
|
|
|
}
|
2019-03-21 21:49:59 +00:00
|
|
|
aerc.simulating -= 1
|
2019-03-21 21:36:42 +00:00
|
|
|
}
|
|
|
|
|
2018-06-01 07:58:00 +00:00
|
|
|
func (aerc *Aerc) Event(event tcell.Event) bool {
|
2019-03-17 20:19:15 +00:00
|
|
|
if aerc.focused != nil {
|
|
|
|
return aerc.focused.Event(event)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch event := event.(type) {
|
|
|
|
case *tcell.EventKey:
|
2019-03-22 01:34:12 +00:00
|
|
|
aerc.statusline.Expire()
|
2019-03-17 20:19:15 +00:00
|
|
|
aerc.pendingKeys = append(aerc.pendingKeys, config.KeyStroke{
|
|
|
|
Key: event.Key(),
|
|
|
|
Rune: event.Rune(),
|
|
|
|
})
|
2019-07-16 17:43:08 +00:00
|
|
|
aerc.statusline.Invalidate()
|
2019-03-21 21:36:42 +00:00
|
|
|
bindings := aerc.getBindings()
|
|
|
|
incomplete := false
|
|
|
|
result, strokes := bindings.GetBinding(aerc.pendingKeys)
|
2019-03-17 20:19:15 +00:00
|
|
|
switch result {
|
|
|
|
case config.BINDING_FOUND:
|
2019-03-21 21:36:42 +00:00
|
|
|
aerc.simulate(strokes)
|
|
|
|
return true
|
2019-03-17 20:19:15 +00:00
|
|
|
case config.BINDING_INCOMPLETE:
|
2019-03-21 21:36:42 +00:00
|
|
|
incomplete = true
|
2019-03-17 20:19:15 +00:00
|
|
|
case config.BINDING_NOT_FOUND:
|
2019-03-21 21:36:42 +00:00
|
|
|
}
|
|
|
|
if bindings.Globals {
|
|
|
|
result, strokes = aerc.conf.Bindings.Global.
|
|
|
|
GetBinding(aerc.pendingKeys)
|
|
|
|
switch result {
|
|
|
|
case config.BINDING_FOUND:
|
|
|
|
aerc.simulate(strokes)
|
|
|
|
return true
|
|
|
|
case config.BINDING_INCOMPLETE:
|
|
|
|
incomplete = true
|
|
|
|
case config.BINDING_NOT_FOUND:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !incomplete {
|
2019-03-17 20:19:15 +00:00
|
|
|
aerc.pendingKeys = []config.KeyStroke{}
|
2019-03-21 21:49:59 +00:00
|
|
|
exKey := bindings.ExKey
|
|
|
|
if aerc.simulating > 0 {
|
|
|
|
// Keybindings still use : even if you change the ex key
|
|
|
|
exKey = aerc.conf.Bindings.Global.ExKey
|
|
|
|
}
|
|
|
|
if event.Key() == exKey.Key && event.Rune() == exKey.Rune {
|
2019-03-17 20:19:15 +00:00
|
|
|
aerc.BeginExCommand()
|
|
|
|
return true
|
|
|
|
}
|
2019-03-17 21:39:22 +00:00
|
|
|
interactive, ok := aerc.tabs.Tabs[aerc.tabs.Selected].Content.(ui.Interactive)
|
|
|
|
if ok {
|
|
|
|
return interactive.Event(event)
|
|
|
|
}
|
|
|
|
return false
|
2019-03-17 20:19:15 +00:00
|
|
|
}
|
2019-07-11 22:15:15 +00:00
|
|
|
case *tcell.EventMouse:
|
|
|
|
aerc.tabs.MouseEvent(event)
|
2019-03-17 20:19:15 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (aerc *Aerc) Config() *config.AercConfig {
|
|
|
|
return aerc.conf
|
2018-02-28 02:17:26 +00:00
|
|
|
}
|
2019-01-14 13:14:03 +00:00
|
|
|
|
2019-06-21 18:33:09 +00:00
|
|
|
func (aerc *Aerc) Logger() *log.Logger {
|
|
|
|
return aerc.logger
|
|
|
|
}
|
|
|
|
|
2019-03-11 01:15:24 +00:00
|
|
|
func (aerc *Aerc) SelectedAccount() *AccountView {
|
2019-03-17 18:57:05 +00:00
|
|
|
acct, ok := aerc.accounts[aerc.tabs.Tabs[aerc.tabs.Selected].Name]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return acct
|
2019-01-14 13:14:03 +00:00
|
|
|
}
|
2019-03-17 20:19:15 +00:00
|
|
|
|
2019-03-17 21:23:53 +00:00
|
|
|
func (aerc *Aerc) SelectedTab() ui.Drawable {
|
|
|
|
return aerc.tabs.Tabs[aerc.tabs.Selected].Content
|
|
|
|
}
|
|
|
|
|
2019-03-17 20:19:15 +00:00
|
|
|
func (aerc *Aerc) NewTab(drawable ui.Drawable, name string) *ui.Tab {
|
|
|
|
tab := aerc.tabs.Add(drawable, name)
|
|
|
|
aerc.tabs.Select(len(aerc.tabs.Tabs) - 1)
|
|
|
|
return tab
|
|
|
|
}
|
|
|
|
|
2019-03-17 21:23:53 +00:00
|
|
|
func (aerc *Aerc) RemoveTab(tab ui.Drawable) {
|
|
|
|
aerc.tabs.Remove(tab)
|
|
|
|
}
|
|
|
|
|
2019-06-11 05:05:56 +00:00
|
|
|
func (aerc *Aerc) ReplaceTab(tabSrc ui.Drawable, tabTarget ui.Drawable, name string) {
|
|
|
|
aerc.tabs.Replace(tabSrc, tabTarget, name)
|
|
|
|
}
|
|
|
|
|
2019-03-17 20:24:17 +00:00
|
|
|
func (aerc *Aerc) NextTab() {
|
|
|
|
next := aerc.tabs.Selected + 1
|
|
|
|
if next >= len(aerc.tabs.Tabs) {
|
|
|
|
next = 0
|
|
|
|
}
|
|
|
|
aerc.tabs.Select(next)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (aerc *Aerc) PrevTab() {
|
|
|
|
next := aerc.tabs.Selected - 1
|
|
|
|
if next < 0 {
|
|
|
|
next = len(aerc.tabs.Tabs) - 1
|
|
|
|
}
|
|
|
|
aerc.tabs.Select(next)
|
|
|
|
}
|
|
|
|
|
2019-07-19 17:12:57 +00:00
|
|
|
func (aerc *Aerc) SelectTab(name string) bool {
|
|
|
|
for i, tab := range aerc.tabs.Tabs {
|
|
|
|
if tab.Name == name {
|
|
|
|
aerc.tabs.Select(i)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (aerc *Aerc) TabNames() []string {
|
|
|
|
var names []string
|
|
|
|
for _, tab := range aerc.tabs.Tabs {
|
|
|
|
names = append(names, tab.Name)
|
|
|
|
}
|
|
|
|
return names
|
|
|
|
}
|
|
|
|
|
|
|
|
func (aerc *Aerc) SelectPreviousTab() bool {
|
|
|
|
return aerc.tabs.SelectPrevious()
|
|
|
|
}
|
|
|
|
|
2019-03-17 20:19:15 +00:00
|
|
|
// TODO: Use per-account status lines, but a global ex line
|
|
|
|
func (aerc *Aerc) SetStatus(status string) *StatusMessage {
|
|
|
|
return aerc.statusline.Set(status)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (aerc *Aerc) PushStatus(text string, expiry time.Duration) *StatusMessage {
|
|
|
|
return aerc.statusline.Push(text, expiry)
|
|
|
|
}
|
|
|
|
|
2019-05-26 21:37:39 +00:00
|
|
|
func (aerc *Aerc) PushError(text string) {
|
|
|
|
aerc.PushStatus(text, 10*time.Second).Color(tcell.ColorDefault, tcell.ColorRed)
|
|
|
|
}
|
|
|
|
|
2019-03-17 20:19:15 +00:00
|
|
|
func (aerc *Aerc) focus(item libui.Interactive) {
|
|
|
|
if aerc.focused == item {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if aerc.focused != nil {
|
|
|
|
aerc.focused.Focus(false)
|
|
|
|
}
|
|
|
|
aerc.focused = item
|
2019-03-22 01:34:12 +00:00
|
|
|
interactive, ok := aerc.tabs.Tabs[aerc.tabs.Selected].Content.(ui.Interactive)
|
2019-03-17 20:19:15 +00:00
|
|
|
if item != nil {
|
|
|
|
item.Focus(true)
|
2019-03-22 01:34:12 +00:00
|
|
|
if ok {
|
|
|
|
interactive.Focus(false)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ok {
|
|
|
|
interactive.Focus(true)
|
|
|
|
}
|
2019-03-17 20:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (aerc *Aerc) BeginExCommand() {
|
|
|
|
previous := aerc.focused
|
|
|
|
exline := NewExLine(func(cmd string) {
|
2019-07-21 20:01:51 +00:00
|
|
|
parts, err := shlex.Split(cmd)
|
|
|
|
if err != nil {
|
|
|
|
aerc.PushStatus(" "+err.Error(), 10*time.Second).
|
|
|
|
Color(tcell.ColorDefault, tcell.ColorRed)
|
|
|
|
}
|
|
|
|
err = aerc.cmd(parts)
|
2019-03-17 20:19:15 +00:00
|
|
|
if err != nil {
|
|
|
|
aerc.PushStatus(" "+err.Error(), 10*time.Second).
|
2019-03-30 17:05:00 +00:00
|
|
|
Color(tcell.ColorDefault, tcell.ColorRed)
|
2019-03-17 20:19:15 +00:00
|
|
|
}
|
|
|
|
aerc.statusbar.Pop()
|
|
|
|
aerc.focus(previous)
|
|
|
|
}, func() {
|
|
|
|
aerc.statusbar.Pop()
|
|
|
|
aerc.focus(previous)
|
2019-06-27 17:33:11 +00:00
|
|
|
}, func(cmd string) []string {
|
|
|
|
return aerc.complete(cmd)
|
2019-03-17 20:19:15 +00:00
|
|
|
})
|
|
|
|
aerc.statusbar.Push(exline)
|
|
|
|
aerc.focus(exline)
|
|
|
|
}
|
2019-07-19 18:15:48 +00:00
|
|
|
|
|
|
|
func (aerc *Aerc) Mailto(addr *url.URL) error {
|
|
|
|
acct := aerc.SelectedAccount()
|
|
|
|
if acct == nil {
|
|
|
|
return errors.New("No account selected")
|
|
|
|
}
|
|
|
|
defaults := make(map[string]string)
|
|
|
|
defaults["To"] = addr.Opaque
|
|
|
|
headerMap := map[string]string{
|
|
|
|
"cc": "Cc",
|
|
|
|
"in-reply-to": "In-Reply-To",
|
|
|
|
"subject": "Subject",
|
|
|
|
}
|
|
|
|
for key, vals := range addr.Query() {
|
|
|
|
if header, ok := headerMap[strings.ToLower(key)]; ok {
|
|
|
|
defaults[header] = strings.Join(vals, ",")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
composer := NewComposer(aerc.Config(),
|
|
|
|
acct.AccountConfig(), acct.Worker()).Defaults(defaults)
|
|
|
|
composer.FocusSubject()
|
|
|
|
title := "New email"
|
|
|
|
if subj, ok := defaults["Subject"]; ok {
|
|
|
|
title = subj
|
|
|
|
composer.FocusTerminal()
|
|
|
|
}
|
|
|
|
tab := aerc.NewTab(composer, title)
|
|
|
|
composer.OnSubjectChange(func(subject string) {
|
|
|
|
if subject == "" {
|
|
|
|
tab.Name = "New email"
|
|
|
|
} else {
|
|
|
|
tab.Name = subject
|
|
|
|
}
|
|
|
|
tab.Content.Invalidate()
|
|
|
|
})
|
|
|
|
return nil
|
|
|
|
}
|