Move ex line into account

This commit is contained in:
Drew DeVault 2019-01-13 13:33:43 -05:00
parent b76deea963
commit c286d3da6b
2 changed files with 58 additions and 66 deletions

View File

@ -1,6 +1,7 @@
package widgets package widgets
import ( import (
"fmt"
"log" "log"
"time" "time"
@ -16,15 +17,19 @@ type AccountView struct {
conf *config.AccountConfig conf *config.AccountConfig
grid *ui.Grid grid *ui.Grid
logger *log.Logger logger *log.Logger
interactive ui.Interactive
onInvalidate func(d ui.Drawable) onInvalidate func(d ui.Drawable)
status *StatusLine statusline *StatusLine
statusbar *ui.Stack
worker *types.Worker worker *types.Worker
} }
func NewAccountView(conf *config.AccountConfig, func NewAccountView(
logger *log.Logger, statusbar ui.Drawable) *AccountView { conf *config.AccountConfig, logger *log.Logger) *AccountView {
status := NewStatusLine() statusbar := ui.NewStack()
statusline := NewStatusLine()
statusbar.Push(statusline)
grid := ui.NewGrid().Rows([]ui.GridSpec{ grid := ui.NewGrid().Rows([]ui.GridSpec{
{ui.SIZE_WEIGHT, 1}, {ui.SIZE_WEIGHT, 1},
@ -36,28 +41,27 @@ func NewAccountView(conf *config.AccountConfig,
grid.AddChild(ui.NewBordered( grid.AddChild(ui.NewBordered(
ui.NewFill('s'), ui.BORDER_RIGHT)).Span(2, 1) ui.NewFill('s'), ui.BORDER_RIGHT)).Span(2, 1)
grid.AddChild(ui.NewFill('.')).At(0, 1) grid.AddChild(ui.NewFill('.')).At(0, 1)
grid.AddChild(status).At(1, 1) grid.AddChild(statusbar).At(1, 1)
worker, err := worker.NewWorker(conf.Source, logger) worker, err := worker.NewWorker(conf.Source, logger)
if err != nil { if err != nil {
acct := &AccountView{
conf: conf,
grid: grid,
logger: logger,
status: status,
}
// TODO: Update status line with error // TODO: Update status line with error
return acct return &AccountView{
conf: conf,
grid: grid,
logger: logger,
statusline: statusline,
}
} }
acct := &AccountView{ acct := &AccountView{
conf: conf, conf: conf,
grid: grid, grid: grid,
logger: logger, logger: logger,
status: status, statusline: statusline,
worker: worker, statusbar: statusbar,
worker: worker,
} }
logger.Printf("My grid is %p; status %p", grid, status)
go worker.Backend.Run() go worker.Backend.Run()
go func() { go func() {
@ -71,31 +75,26 @@ func NewAccountView(conf *config.AccountConfig,
worker.PostAction(&types.Configure{Config: conf}, nil) worker.PostAction(&types.Configure{Config: conf}, nil)
worker.PostAction(&types.Connect{}, acct.connected) worker.PostAction(&types.Connect{}, acct.connected)
go func() {
time.Sleep(10 * time.Second)
status.Set("Test")
}()
return acct return acct
} }
func (acct *AccountView) connected(msg types.WorkerMessage) { func (acct *AccountView) connected(msg types.WorkerMessage) {
switch msg := msg.(type) { switch msg := msg.(type) {
case *types.Done: case *types.Done:
acct.status.Set("Connected.") acct.statusline.Set("Connected.")
acct.logger.Println("Connected.") acct.logger.Println("Connected.")
acct.worker.PostAction(&types.ListDirectories{}, nil) acct.worker.PostAction(&types.ListDirectories{}, nil)
case *types.CertificateApprovalRequest: case *types.CertificateApprovalRequest:
// TODO: Ask the user // TODO: Ask the user
acct.logger.Println("Approved unknown certificate.") acct.logger.Println("Approved unknown certificate.")
acct.status.Push("Approved unknown certificate.", 5*time.Second) acct.statusline.Push("Approved unknown certificate.", 5*time.Second)
acct.worker.PostAction(&types.ApproveCertificate{ acct.worker.PostAction(&types.ApproveCertificate{
Message: types.RespondTo(msg), Message: types.RespondTo(msg),
Approved: true, Approved: true,
}, acct.connected) }, acct.connected)
default: default:
acct.logger.Println("Connection failed.") acct.logger.Println("Connection failed.")
acct.status.Set("Connection failed."). acct.statusline.Set("Connection failed.").
Color(tcell.ColorRed, tcell.ColorDefault) Color(tcell.ColorRed, tcell.ColorDefault)
} }
} }
@ -113,3 +112,27 @@ func (acct *AccountView) Invalidate() {
func (acct *AccountView) Draw(ctx *ui.Context) { func (acct *AccountView) Draw(ctx *ui.Context) {
acct.grid.Draw(ctx) acct.grid.Draw(ctx)
} }
func (acct *AccountView) Event(event tcell.Event) bool {
if acct.interactive != nil {
return acct.interactive.Event(event)
}
switch event := event.(type) {
case *tcell.EventKey:
if event.Rune() == ':' {
exline := NewExLine(func(command string) {
acct.statusline.Push(
fmt.Sprintf("TODO: execute %s", command), 3*time.Second)
acct.statusbar.Pop()
acct.interactive = nil
}, func() {
acct.statusbar.Pop()
acct.interactive = nil
})
acct.interactive = exline
acct.statusbar.Push(exline)
return true
}
}
return false
}

View File

@ -1,9 +1,7 @@
package widgets package widgets
import ( import (
"fmt"
"log" "log"
"time"
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
@ -12,12 +10,9 @@ import (
) )
type Aerc struct { type Aerc struct {
accounts map[string]*AccountView accounts map[string]*AccountView
grid *libui.Grid grid *libui.Grid
tabs *libui.Tabs tabs *libui.Tabs
statusbar *libui.Stack
statusline *StatusLine
interactive libui.Interactive
} }
func NewAerc(conf *config.AercConfig, logger *log.Logger) *Aerc { func NewAerc(conf *config.AercConfig, logger *log.Logger) *Aerc {
@ -31,10 +26,6 @@ func NewAerc(conf *config.AercConfig, logger *log.Logger) *Aerc {
{libui.SIZE_WEIGHT, 1}, {libui.SIZE_WEIGHT, 1},
}) })
statusbar := libui.NewStack()
statusline := NewStatusLine()
statusbar.Push(statusline)
// TODO: Grab sidebar size from config and via :set command // TODO: Grab sidebar size from config and via :set command
mainGrid.AddChild(libui.NewText("aerc"). mainGrid.AddChild(libui.NewText("aerc").
Strategy(libui.TEXT_CENTER). Strategy(libui.TEXT_CENTER).
@ -45,17 +36,15 @@ func NewAerc(conf *config.AercConfig, logger *log.Logger) *Aerc {
accts := make(map[string]*AccountView) accts := make(map[string]*AccountView)
for _, acct := range conf.Accounts { for _, acct := range conf.Accounts {
view := NewAccountView(&acct, logger, statusbar) view := NewAccountView(&acct, logger)
accts[acct.Name] = view accts[acct.Name] = view
tabs.Add(view, acct.Name) tabs.Add(view, acct.Name)
} }
return &Aerc{ return &Aerc{
accounts: accts, accounts: accts,
grid: mainGrid, grid: mainGrid,
statusbar: statusbar, tabs: tabs,
statusline: statusline,
tabs: tabs,
} }
} }
@ -74,26 +63,6 @@ func (aerc *Aerc) Draw(ctx *libui.Context) {
} }
func (aerc *Aerc) Event(event tcell.Event) bool { func (aerc *Aerc) Event(event tcell.Event) bool {
switch event := event.(type) { acct, _ := aerc.tabs.Tabs[aerc.tabs.Selected].Content.(*AccountView)
case *tcell.EventKey: return acct.Event(event)
if event.Rune() == ':' {
exline := NewExLine(func(command string) {
aerc.statusline.Push(fmt.Sprintf("TODO: execute %s", command),
3*time.Second)
aerc.statusbar.Pop()
aerc.interactive = nil
}, func() {
aerc.statusbar.Pop()
aerc.interactive = nil
})
aerc.interactive = exline
aerc.statusbar.Push(exline)
return true
}
}
if aerc.interactive != nil {
return aerc.interactive.Event(event)
} else {
return false
}
} }