From 4074445cbb45dc6ec132e67b7eac9f32dcfd53de Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Thu, 11 Jan 2018 09:04:18 -0500 Subject: Move worker into account tab --- ui/account.go | 31 +++++++++++++++++++++++++++---- ui/helpers.go | 30 +++++++++++++++++++++++++----- ui/types.go | 15 +++++++++++++-- ui/ui.go | 15 ++++++++++++++- 4 files changed, 79 insertions(+), 12 deletions(-) (limited to 'ui') diff --git a/ui/account.go b/ui/account.go index 9c16cc5..50f41e4 100644 --- a/ui/account.go +++ b/ui/account.go @@ -1,25 +1,35 @@ package ui import ( + "fmt" + tb "github.com/nsf/termbox-go" "git.sr.ht/~sircmpwn/aerc2/config" "git.sr.ht/~sircmpwn/aerc2/worker" + "git.sr.ht/~sircmpwn/aerc2/worker/types" ) type AccountTab struct { Config *config.AccountConfig - Worker *worker.Worker + Worker worker.Worker Parent *UIState counter int + log []string } -func NewAccountTab(conf *config.AccountConfig, work *worker.Worker) *AccountTab { +func NewAccountTab(conf *config.AccountConfig) (*AccountTab, error) { + work, err := worker.NewWorker(conf.Source) + if err != nil { + return nil, err + } + go work.Run() + work.PostAction(types.Configure{Config: conf}) return &AccountTab{ Config: conf, Worker: work, - } + }, nil } func (acc *AccountTab) Name() string { @@ -32,13 +42,26 @@ func (acc *AccountTab) SetParent(parent *UIState) { func (acc *AccountTab) Render(at Geometry) { cell := tb.Cell{ + Ch: ' ', Fg: tb.ColorDefault, Bg: tb.ColorDefault, } - TPrintf(&at, cell, "%s %d", acc.Name(), acc.counter) + TFill(at, cell) + TPrintf(&at, cell, "%s %d\n", acc.Name(), acc.counter) + for _, str := range acc.log { + TPrintf(&at, cell, "%s\n", str) + } acc.counter++ if acc.counter%10000 == 0 { acc.counter = 0 } acc.Parent.InvalidateFrom(acc) } + +func (acc *AccountTab) GetChannel() chan types.WorkerMessage { + return acc.Worker.GetMessages() +} + +func (acc *AccountTab) HandleMessage(msg types.WorkerMessage) { + acc.log = append(acc.log, fmt.Sprintf("<- %T", msg)) +} diff --git a/ui/helpers.go b/ui/helpers.go index 0b8789e..f2b2adf 100644 --- a/ui/helpers.go +++ b/ui/helpers.go @@ -9,13 +9,33 @@ import ( func TPrintf(geo *Geometry, ref tb.Cell, format string, a ...interface{}) { str := fmt.Sprintf(format, a...) _geo := *geo + newline := func() { + // TODO: Abort when out of room? + geo.Col = _geo.Col + geo.Row++ + } for _, ch := range str { - tb.SetCell(geo.Col, geo.Row, ch, ref.Fg, ref.Bg) - geo.Col++ - if geo.Col == _geo.Col+geo.Width { - // TODO: Abort when out of room? + switch ch { + case '\n': + newline() + case '\r': geo.Col = _geo.Col - geo.Row++ + default: + tb.SetCell(geo.Col, geo.Row, ch, ref.Fg, ref.Bg) + geo.Col++ + if geo.Col == _geo.Col+geo.Width { + newline() + } + } + } +} + +func TFill(geo Geometry, ref tb.Cell) { + _geo := geo + for ; geo.Row < geo.Height; geo.Row++ { + for ; geo.Col < geo.Width; geo.Col++ { + tb.SetCell(geo.Col, geo.Row, ref.Ch, ref.Fg, ref.Bg) } + geo.Col = _geo.Col } } diff --git a/ui/types.go b/ui/types.go index 14a91c3..5437642 100644 --- a/ui/types.go +++ b/ui/types.go @@ -4,20 +4,19 @@ import ( tb "github.com/nsf/termbox-go" "git.sr.ht/~sircmpwn/aerc2/config" + "git.sr.ht/~sircmpwn/aerc2/worker/types" ) const ( Valid = 0 InvalidateTabList = 1 << iota InvalidateTabView - InvalidateSidebar InvalidateStatusBar ) const ( InvalidateAll = InvalidateTabList | InvalidateTabView | - InvalidateSidebar | InvalidateStatusBar ) @@ -34,6 +33,16 @@ type AercTab interface { SetParent(parent *UIState) } +type WorkerListener interface { + GetChannel() chan types.WorkerMessage + HandleMessage(msg types.WorkerMessage) +} + +type wrappedMessage struct { + msg types.WorkerMessage + listener WorkerListener +} + type UIState struct { Config *config.AercConfig Exit bool @@ -57,4 +66,6 @@ type UIState struct { } tbEvents chan tb.Event + // Aggregate channel for all worker messages + workerEvents chan wrappedMessage } diff --git a/ui/ui.go b/ui/ui.go index f01af08..db31696 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -11,7 +11,8 @@ func Initialize(conf *config.AercConfig) (*UIState, error) { Config: conf, InvalidPanes: InvalidateAll, - tbEvents: make(chan tb.Event, 10), + tbEvents: make(chan tb.Event, 10), + workerEvents: make(chan wrappedMessage), } if err := tb.Init(); err != nil { return nil, err @@ -33,6 +34,16 @@ func (state *UIState) Close() { func (state *UIState) AddTab(tab AercTab) { tab.SetParent(state) state.Tabs = append(state.Tabs, tab) + if listener, ok := tab.(WorkerListener); ok { + go (func() { + for msg := range listener.GetChannel() { + state.workerEvents <- wrappedMessage{ + msg: msg, + listener: listener, + } + } + })() + } } func (state *UIState) Invalidate(what uint) { @@ -67,6 +78,8 @@ func (state *UIState) Tick() bool { case tb.EventResize: state.Invalidate(InvalidateAll) } + case msg := <-state.workerEvents: + msg.listener.HandleMessage(msg.msg) default: // no-op break -- cgit v1.2.3