aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin Kuehler <kkuehler@brave.com>2019-06-10 22:05:55 -0700
committerDrew DeVault <sir@cmpwn.com>2019-06-11 09:34:36 -0400
commit32f970e0531a0811392d2836773870acf649cf26 (patch)
tree2e65f2a122b6235c9e858ba4af36481475292747
parentd1df7103280ef488434b103e5e9e3129c407e5b6 (diff)
downloadaerc-32f970e0531a0811392d2836773870acf649cf26.tar.gz
Move select functionality from msglist to msgstore
Remove msglist Next and Prev commands Signed-off-by: Kevin Kuehler <kkuehler@brave.com>
-rw-r--r--commands/msg/archive.go3
-rw-r--r--commands/msg/delete.go3
-rw-r--r--commands/msg/move.go3
-rw-r--r--lib/msgstore.go41
-rw-r--r--widgets/msglist.go63
5 files changed, 67 insertions, 46 deletions
diff --git a/commands/msg/archive.go b/commands/msg/archive.go
index da4c421c..4fe73306 100644
--- a/commands/msg/archive.go
+++ b/commands/msg/archive.go
@@ -34,7 +34,8 @@ func Archive(aerc *widgets.Aerc, args []string) error {
msg := widget.SelectedMessage()
store := widget.Store()
archiveDir := acct.AccountConfig().Archive
- acct.Messages().Next()
+ store.Next()
+ acct.Messages().Scroll()
switch args[1] {
case ARCHIVE_MONTH:
diff --git a/commands/msg/delete.go b/commands/msg/delete.go
index 082dbe35..ee3dd290 100644
--- a/commands/msg/delete.go
+++ b/commands/msg/delete.go
@@ -31,7 +31,8 @@ func DeleteMessage(aerc *widgets.Aerc, args []string) error {
if isMsgView {
aerc.RemoveTab(widget)
}
- acct.Messages().Next()
+ store.Next()
+ acct.Messages().Scroll()
store.Delete([]uint32{msg.Uid}, func(msg types.WorkerMessage) {
switch msg := msg.(type) {
case *types.Done:
diff --git a/commands/msg/move.go b/commands/msg/move.go
index 5300c5a4..23670768 100644
--- a/commands/msg/move.go
+++ b/commands/msg/move.go
@@ -45,7 +45,8 @@ func Move(aerc *widgets.Aerc, args []string) error {
if isMsgView {
aerc.RemoveTab(widget)
}
- acct.Messages().Next()
+ store.Next()
+ acct.Messages().Scroll()
store.Move([]uint32{msg.Uid}, args[optind], createParents, func(
msg types.WorkerMessage) {
diff --git a/lib/msgstore.go b/lib/msgstore.go
index 5b4d5406..a81f9ad3 100644
--- a/lib/msgstore.go
+++ b/lib/msgstore.go
@@ -17,6 +17,7 @@ type MessageStore struct {
// Ordered list of known UIDs
Uids []uint32
+ selected int
bodyCallbacks map[uint32][]func(io.Reader)
headerCallbacks map[uint32][]func(*types.MessageInfo)
@@ -34,6 +35,7 @@ func NewMessageStore(worker *types.Worker,
Deleted: make(map[uint32]interface{}),
DirInfo: *dirInfo,
+ selected: 0,
bodyCallbacks: make(map[uint32][]func(io.Reader)),
headerCallbacks: make(map[uint32][]func(*types.MessageInfo)),
@@ -279,3 +281,42 @@ func (store *MessageStore) Read(uids []uint32, read bool,
Uids: set,
}, cb)
}
+
+func (store *MessageStore) Selected() *types.MessageInfo {
+ return store.Messages[store.Uids[len(store.Uids)-store.selected-1]]
+}
+
+func (store *MessageStore) SelectedIndex() int {
+ return store.selected
+}
+
+func (store *MessageStore) Select(index int) {
+ store.selected = index
+ for ; store.selected < 0; store.selected = len(store.Uids) + store.selected {
+ /* This space deliberately left blank */
+ }
+ if store.selected > len(store.Uids) {
+ store.selected = len(store.Uids)
+ }
+}
+
+func (store *MessageStore) nextPrev(delta int) {
+ if len(store.Uids) == 0 {
+ return
+ }
+ store.selected += delta
+ if store.selected < 0 {
+ store.selected = 0
+ }
+ if store.selected >= len(store.Uids) {
+ store.selected = len(store.Uids) - 1
+ }
+}
+
+func (store *MessageStore) Next() {
+ store.nextPrev(1)
+}
+
+func (store *MessageStore) Prev() {
+ store.nextPrev(-1)
+}
diff --git a/widgets/msglist.go b/widgets/msglist.go
index 1afe4879..43c2ee49 100644
--- a/widgets/msglist.go
+++ b/widgets/msglist.go
@@ -16,22 +16,20 @@ import (
type MessageList struct {
ui.Invalidatable
- conf *config.AercConfig
- logger *log.Logger
- height int
- scroll int
- selected int
- nmsgs int
- spinner *Spinner
- store *lib.MessageStore
+ conf *config.AercConfig
+ logger *log.Logger
+ height int
+ scroll int
+ nmsgs int
+ spinner *Spinner
+ store *lib.MessageStore
}
func NewMessageList(conf *config.AercConfig, logger *log.Logger) *MessageList {
ml := &MessageList{
- conf: conf,
- logger: logger,
- selected: 0,
- spinner: NewSpinner(),
+ conf: conf,
+ logger: logger,
+ spinner: NewSpinner(),
}
ml.spinner.OnInvalidate(func(_ ui.Drawable) {
ml.Invalidate()
@@ -78,7 +76,7 @@ func (ml *MessageList) Draw(ctx *ui.Context) {
style := tcell.StyleDefault
// current row
- if row == ml.selected-ml.scroll {
+ if row == ml.store.SelectedIndex()-ml.scroll {
style = style.Reverse(true)
}
// deleted message
@@ -139,12 +137,12 @@ func (ml *MessageList) storeUpdate(store *lib.MessageStore) {
// for the previously selected UID.
if len(store.Uids) > ml.nmsgs && ml.nmsgs != 0 {
for i := 0; i < len(store.Uids)-ml.nmsgs; i++ {
- ml.Next()
+ ml.Scroll()
}
}
if len(store.Uids) < ml.nmsgs && ml.nmsgs != 0 {
for i := 0; i < ml.nmsgs-len(store.Uids); i++ {
- ml.Prev()
+ ml.Scroll()
}
}
ml.nmsgs = len(store.Uids)
@@ -156,7 +154,6 @@ func (ml *MessageList) storeUpdate(store *lib.MessageStore) {
func (ml *MessageList) SetStore(store *lib.MessageStore) {
if ml.Store() != store {
ml.scroll = 0
- ml.selected = 0
}
ml.store = store
if store != nil {
@@ -180,54 +177,34 @@ func (ml *MessageList) Empty() bool {
func (ml *MessageList) Selected() *types.MessageInfo {
store := ml.Store()
- return store.Messages[store.Uids[len(store.Uids)-ml.selected-1]]
+ return store.Messages[store.Uids[len(store.Uids)-ml.store.SelectedIndex()-1]]
}
func (ml *MessageList) Select(index int) {
store := ml.Store()
+ store.Select(index)
- ml.selected = index
- for ; ml.selected < 0; ml.selected = len(store.Uids) + ml.selected {
- }
- if ml.selected > len(store.Uids) {
- ml.selected = len(store.Uids)
- }
// I'm too lazy to do the math right now
- for ml.selected-ml.scroll >= ml.Height() {
+ for store.SelectedIndex()-ml.scroll >= ml.Height() {
ml.scroll += 1
}
- for ml.selected-ml.scroll < 0 {
+ for store.SelectedIndex()-ml.scroll < 0 {
ml.scroll -= 1
}
}
-func (ml *MessageList) nextPrev(delta int) {
+func (ml *MessageList) Scroll() {
store := ml.Store()
if store == nil || len(store.Uids) == 0 {
return
}
- ml.selected += delta
- if ml.selected < 0 {
- ml.selected = 0
- }
- if ml.selected >= len(store.Uids) {
- ml.selected = len(store.Uids) - 1
- }
if ml.Height() != 0 {
- if ml.selected-ml.scroll >= ml.Height() {
+ if store.SelectedIndex()-ml.scroll >= ml.Height() {
ml.scroll += 1
- } else if ml.selected-ml.scroll < 0 {
+ } else if store.SelectedIndex()-ml.scroll < 0 {
ml.scroll -= 1
}
}
ml.Invalidate()
}
-
-func (ml *MessageList) Next() {
- ml.nextPrev(1)
-}
-
-func (ml *MessageList) Prev() {
- ml.nextPrev(-1)
-}