aerc/widgets/compose.go

209 lines
4.5 KiB
Go
Raw Normal View History

2019-05-12 04:06:09 +00:00
package widgets
import (
"io/ioutil"
"os"
2019-05-12 04:06:09 +00:00
"os/exec"
"github.com/gdamore/tcell"
"github.com/mattn/go-runewidth"
"git.sr.ht/~sircmpwn/aerc2/config"
2019-05-12 04:06:09 +00:00
"git.sr.ht/~sircmpwn/aerc2/lib/ui"
)
type Composer struct {
headers struct {
from *headerEditor
subject *headerEditor
to *headerEditor
}
config *config.AccountConfig
2019-05-12 04:06:09 +00:00
editor *Terminal
email *os.File
2019-05-12 04:06:09 +00:00
grid *ui.Grid
2019-05-13 20:24:05 +00:00
review *reviewMessage
2019-05-12 04:06:09 +00:00
focusable []ui.DrawableInteractive
focused int
}
// TODO: Let caller configure headers, initial body (for replies), etc
func NewComposer(conf *config.AccountConfig) *Composer {
2019-05-12 04:06:09 +00:00
grid := ui.NewGrid().Rows([]ui.GridSpec{
{ui.SIZE_EXACT, 3},
{ui.SIZE_WEIGHT, 1},
}).Columns([]ui.GridSpec{
{ui.SIZE_WEIGHT, 1},
})
// TODO: let user specify extra headers to edit by default
headers := ui.NewGrid().Rows([]ui.GridSpec{
{ui.SIZE_EXACT, 1}, // To/From
{ui.SIZE_EXACT, 1}, // Subject
{ui.SIZE_EXACT, 1}, // [spacer]
}).Columns([]ui.GridSpec{
{ui.SIZE_WEIGHT, 1},
{ui.SIZE_WEIGHT, 1},
})
2019-05-12 04:38:48 +00:00
to := newHeaderEditor("To", "")
from := newHeaderEditor("From", conf.From)
2019-05-12 04:38:48 +00:00
subject := newHeaderEditor("Subject", "")
headers.AddChild(to).At(0, 0)
headers.AddChild(from).At(0, 1)
headers.AddChild(subject).At(1, 0).Span(1, 2)
2019-05-12 04:06:09 +00:00
headers.AddChild(ui.NewFill(' ')).At(2, 0).Span(1, 2)
email, err := ioutil.TempFile("", "aerc-compose-*.eml")
if err != nil {
// TODO: handle this better
return nil
}
2019-05-12 04:06:09 +00:00
// TODO: built-in config option, $EDITOR, then vi, in that order
editor := exec.Command("vim", email.Name())
2019-05-12 04:06:09 +00:00
term, _ := NewTerminal(editor)
grid.AddChild(headers).At(0, 0)
grid.AddChild(term).At(1, 0)
2019-05-13 20:24:05 +00:00
c := &Composer{
config: conf,
2019-05-12 04:38:48 +00:00
editor: term,
email: email,
grid: grid,
2019-05-12 04:38:48 +00:00
// You have to backtab to get to "From", since you usually don't edit it
focused: 1,
focusable: []ui.DrawableInteractive{from, to, subject, term},
2019-05-12 04:06:09 +00:00
}
2019-05-13 20:24:05 +00:00
term.OnClose = c.termClosed
return c
2019-05-12 04:06:09 +00:00
}
func (c *Composer) Draw(ctx *ui.Context) {
c.grid.Draw(ctx)
}
func (c *Composer) Invalidate() {
c.grid.Invalidate()
}
func (c *Composer) OnInvalidate(fn func(d ui.Drawable)) {
c.grid.OnInvalidate(func(_ ui.Drawable) {
fn(c)
})
}
func (c *Composer) Event(event tcell.Event) bool {
2019-05-12 04:38:48 +00:00
return c.focusable[c.focused].Event(event)
2019-05-12 04:06:09 +00:00
}
func (c *Composer) Focus(focus bool) {
2019-05-12 04:38:48 +00:00
c.focusable[c.focused].Focus(focus)
2019-05-12 04:06:09 +00:00
}
2019-05-13 20:24:05 +00:00
func (c *Composer) termClosed(err error) {
// TODO: do we care about that error (note: yes, we do)
c.grid.RemoveChild(c.editor)
c.grid.AddChild(newReviewMessage(c)).At(1, 0)
c.editor.Destroy()
}
func (c *Composer) PrevField() {
c.focusable[c.focused].Focus(false)
c.focused--
if c.focused == -1 {
c.focused = len(c.focusable) - 1
}
c.focusable[c.focused].Focus(true)
}
func (c *Composer) NextField() {
c.focusable[c.focused].Focus(false)
c.focused = (c.focused + 1) % len(c.focusable)
c.focusable[c.focused].Focus(true)
}
2019-05-13 20:24:05 +00:00
type headerEditor struct {
name string
input *ui.TextInput
}
2019-05-12 04:06:09 +00:00
func newHeaderEditor(name string, value string) *headerEditor {
return &headerEditor{
input: ui.NewTextInput(value),
name: name,
}
}
func (he *headerEditor) Draw(ctx *ui.Context) {
name := he.name + " "
size := runewidth.StringWidth(name)
ctx.Fill(0, 0, size, ctx.Height(), ' ', tcell.StyleDefault)
ctx.Printf(0, 0, tcell.StyleDefault.Bold(true), "%s", name)
he.input.Draw(ctx.Subcontext(size, 0, ctx.Width()-size, 1))
}
func (he *headerEditor) Invalidate() {
2019-05-12 04:38:48 +00:00
he.input.Invalidate()
}
func (he *headerEditor) OnInvalidate(fn func(ui.Drawable)) {
he.input.OnInvalidate(func(_ ui.Drawable) {
fn(he)
})
}
func (he *headerEditor) Focus(focused bool) {
he.input.Focus(focused)
}
func (he *headerEditor) Event(event tcell.Event) bool {
return he.input.Event(event)
2019-05-12 04:06:09 +00:00
}
2019-05-13 20:24:05 +00:00
type reviewMessage struct {
composer *Composer
grid *ui.Grid
}
func newReviewMessage(composer *Composer) *reviewMessage {
grid := ui.NewGrid().Rows([]ui.GridSpec{
{ui.SIZE_EXACT, 2},
{ui.SIZE_EXACT, 1},
{ui.SIZE_WEIGHT, 1},
}).Columns([]ui.GridSpec{
{ui.SIZE_WEIGHT, 1},
})
grid.AddChild(ui.NewText(
"Send this email? [y]es/[n]o/[e]dit/[a]ttach file")).At(0, 0)
grid.AddChild(ui.NewText("Attachments:").
Reverse(true)).At(1, 0)
// TODO: Attachments
grid.AddChild(ui.NewText("(none)")).At(2, 0)
return &reviewMessage{
composer: composer,
grid: grid,
}
}
func (rm *reviewMessage) Invalidate() {
rm.grid.Invalidate()
}
func (rm *reviewMessage) OnInvalidate(fn func(ui.Drawable)) {
rm.grid.OnInvalidate(func(_ ui.Drawable) {
fn(rm)
})
}
func (rm *reviewMessage) Draw(ctx *ui.Context) {
rm.grid.Draw(ctx)
}