Start building out command subsystem

This commit is contained in:
Drew DeVault 2019-03-10 21:15:24 -04:00
parent 62862d8a9e
commit b60999c39e
7 changed files with 119 additions and 8 deletions

View File

@ -10,6 +10,7 @@ import (
"github.com/mattn/go-isatty" "github.com/mattn/go-isatty"
"git.sr.ht/~sircmpwn/aerc2/config" "git.sr.ht/~sircmpwn/aerc2/config"
"git.sr.ht/~sircmpwn/aerc2/commands"
libui "git.sr.ht/~sircmpwn/aerc2/lib/ui" libui "git.sr.ht/~sircmpwn/aerc2/lib/ui"
"git.sr.ht/~sircmpwn/aerc2/widgets" "git.sr.ht/~sircmpwn/aerc2/widgets"
) )
@ -32,7 +33,12 @@ func main() {
panic(err) panic(err)
} }
ui, err := libui.Initialize(conf, widgets.NewAerc(conf, logger)) var aerc *widgets.Aerc
aerc = widgets.NewAerc(conf, logger, func(cmd string) error {
return commands.ExecuteCommand(aerc, cmd)
})
ui, err := libui.Initialize(conf, aerc)
if err != nil { if err != nil {
panic(err) panic(err)
} }

28
commands/commands.go Normal file
View File

@ -0,0 +1,28 @@
package commands
import (
"errors"
"git.sr.ht/~sircmpwn/aerc2/widgets"
)
type AercCommand func(aerc *widgets.Aerc, cmd string) error
var (
commands map[string]AercCommand
)
func init() {
commands = make(map[string]AercCommand)
}
func Register(name string, cmd AercCommand) {
commands[name] = cmd
}
func ExecuteCommand(aerc *widgets.Aerc, cmd string) error {
if fn, ok := commands[cmd]; ok {
return fn(aerc, cmd)
}
return errors.New("Unknown command " + cmd)
}

15
commands/next-folder.go Normal file
View File

@ -0,0 +1,15 @@
package commands
import (
"git.sr.ht/~sircmpwn/aerc2/widgets"
)
func init() {
Register("next-folder", NextFolder)
}
func NextFolder(aerc *widgets.Aerc, cmd string) error {
acct := aerc.SelectedAccount()
acct.Directories().Next()
return nil
}

15
commands/prev-folder.go Normal file
View File

@ -0,0 +1,15 @@
package commands
import (
"git.sr.ht/~sircmpwn/aerc2/widgets"
)
func init() {
Register("prev-folder", PrevFolder)
}
func PrevFolder(aerc *widgets.Aerc, cmd string) error {
acct := aerc.SelectedAccount()
acct.Directories().Prev()
return nil
}

View File

@ -75,7 +75,7 @@ func NewAccountView(conf *config.AccountConfig,
for { for {
msg := <-worker.Messages msg := <-worker.Messages
msg = worker.ProcessMessage(msg) msg = worker.ProcessMessage(msg)
// TODO: dispatch to appropriate handlers acct.onMessage(msg)
} }
}() }()
@ -164,3 +164,11 @@ func (acct *AccountView) connected(msg types.WorkerMessage) {
Color(tcell.ColorRed, tcell.ColorDefault) Color(tcell.ColorRed, tcell.ColorDefault)
} }
} }
func (acct *AccountView) Directories() *DirectoryList {
return acct.dirlist
}
func (acct *AccountView) onMessage(msg types.WorkerMessage) {
// TODO
}

View File

@ -1,7 +1,6 @@
package widgets package widgets
import ( import (
"fmt"
"log" "log"
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
@ -13,11 +12,14 @@ import (
type Aerc struct { type Aerc struct {
accounts map[string]*AccountView accounts map[string]*AccountView
cmd func(cmd string) error
grid *libui.Grid grid *libui.Grid
tabs *libui.Tabs tabs *libui.Tabs
} }
func NewAerc(conf *config.AercConfig, logger *log.Logger) *Aerc { func NewAerc(conf *config.AercConfig, logger *log.Logger,
cmd func(cmd string) error) *Aerc {
tabs := libui.NewTabs() tabs := libui.NewTabs()
mainGrid := libui.NewGrid().Rows([]libui.GridSpec{ mainGrid := libui.NewGrid().Rows([]libui.GridSpec{
@ -37,12 +39,13 @@ func NewAerc(conf *config.AercConfig, logger *log.Logger) *Aerc {
aerc := &Aerc{ aerc := &Aerc{
accounts: make(map[string]*AccountView), accounts: make(map[string]*AccountView),
cmd: cmd,
grid: mainGrid, grid: mainGrid,
tabs: tabs, tabs: tabs,
} }
for _, acct := range conf.Accounts { for _, acct := range conf.Accounts {
view := NewAccountView(&acct, logger, aerc.RunCommand) view := NewAccountView(&acct, logger, cmd)
aerc.accounts[acct.Name] = view aerc.accounts[acct.Name] = view
tabs.Add(view, acct.Name) tabs.Add(view, acct.Name)
} }
@ -73,7 +76,6 @@ func (aerc *Aerc) Event(event tcell.Event) bool {
return acct.Event(event) return acct.Event(event)
} }
func (aerc *Aerc) RunCommand(cmd string) error { func (aerc *Aerc) SelectedAccount() *AccountView {
// TODO return aerc.accounts[aerc.tabs.Tabs[aerc.tabs.Selected].Name]
return fmt.Errorf("TODO: execute '%s'", cmd)
} }

View File

@ -103,3 +103,40 @@ func (dirlist *DirectoryList) Draw(ctx *ui.Context) {
row++ row++
} }
} }
func (dirlist *DirectoryList) nextPrev(delta int) {
for i, dir := range dirlist.dirs {
if dir == dirlist.selected {
var j int
ndirs := len(dirlist.dirs)
for j = i + delta; j != i; j += delta {
if j < 0 {
j = ndirs - 1
}
if j >= ndirs {
j = 0
}
name := dirlist.dirs[j]
if len(dirlist.conf.Folders) > 1 && name != dirlist.selected {
idx := sort.SearchStrings(dirlist.conf.Folders, name)
if idx == len(dirlist.conf.Folders) ||
dirlist.conf.Folders[idx] != name {
continue
}
}
break
}
dirlist.Select(dirlist.dirs[j])
break
}
}
}
func (dirlist *DirectoryList) Next() {
dirlist.nextPrev(1)
}
func (dirlist *DirectoryList) Prev() {
dirlist.nextPrev(-1)
}