statusline: make statusline folder-specific

Make statusline folder-specific. Update filter, search and threading
status when changing folders.

Commit 2512c04 ("statusline: implement per-account status") introduced
an account-specific statusline. This makes it account- and
folder-specific.

Signed-off-by: Koni Marti <koni.marti@gmail.com>
Tested-by: Moritz Poldrack <moritz@poldrack.dev>
This commit is contained in:
Koni Marti 2022-03-21 22:18:51 +01:00 committed by Robin Jarry
parent e56648029f
commit feecc09b73
4 changed files with 67 additions and 46 deletions

View file

@ -5,6 +5,7 @@ import (
"strings" "strings"
"git.sr.ht/~rjarry/aerc/commands" "git.sr.ht/~rjarry/aerc/commands"
"git.sr.ht/~rjarry/aerc/lib/statusline"
"git.sr.ht/~rjarry/aerc/widgets" "git.sr.ht/~rjarry/aerc/widgets"
) )
@ -52,6 +53,7 @@ func (ChangeFolder) Execute(aerc *widgets.Aerc, args []string) error {
store := acct.Store() store := acct.Store()
if store != nil { if store != nil {
store.ApplyClear() store.ApplyClear()
acct.SetStatus(statusline.SearchFilterClear())
} }
return nil return nil
} }

View file

@ -0,0 +1,28 @@
package statusline
type folderState struct {
Search string
Filter string
FilterActivity string
Threading string
}
func (fs *folderState) State() []string {
var line []string
if fs.FilterActivity != "" {
line = append(line, fs.FilterActivity)
} else {
if fs.Filter != "" {
line = append(line, fs.Filter)
}
}
if fs.Search != "" {
line = append(line, fs.Search)
}
if fs.Threading != "" {
line = append(line, fs.Threading)
}
return line
}

View file

@ -14,19 +14,17 @@ type State struct {
ConnActivity string ConnActivity string
Connected bool Connected bool
Search string
Filter string
FilterActivity string
Threading string
Passthrough string Passthrough string
fs map[string]*folderState
} }
func NewState(name string, multipleAccts bool, sep string) *State { func NewState(name string, multipleAccts bool, sep string) *State {
return &State{Name: name, Multiple: multipleAccts, Separator: sep} return &State{Name: name, Multiple: multipleAccts, Separator: sep,
fs: make(map[string]*folderState)}
} }
func (s *State) String() string { func (s *State) StatusLine(folder string) string {
var line []string var line []string
if s.Connection != "" || s.ConnActivity != "" { if s.Connection != "" || s.ConnActivity != "" {
conn := s.Connection conn := s.Connection
@ -40,30 +38,27 @@ func (s *State) String() string {
} }
} }
if s.Connected { if s.Connected {
if s.FilterActivity != "" {
line = append(line, s.FilterActivity)
} else {
if s.Filter != "" {
line = append(line, s.Filter)
}
}
if s.Search != "" {
line = append(line, s.Search)
}
if s.Threading != "" {
line = append(line, s.Threading)
}
if s.Passthrough != "" { if s.Passthrough != "" {
line = append(line, s.Passthrough) line = append(line, s.Passthrough)
} }
if folder != "" {
line = append(line, s.folderState(folder).State()...)
}
} }
return strings.Join(line, s.Separator) return strings.Join(line, s.Separator)
} }
type SetStateFunc func(s *State) func (s *State) folderState(folder string) *folderState {
if _, ok := s.fs[folder]; !ok {
s.fs[folder] = &folderState{}
}
return s.fs[folder]
}
type SetStateFunc func(s *State, folder string)
func Connected(state bool) SetStateFunc { func Connected(state bool) SetStateFunc {
return func(s *State) { return func(s *State, folder string) {
s.ConnActivity = "" s.ConnActivity = ""
s.Connected = state s.Connected = state
if state { if state {
@ -75,29 +70,29 @@ func Connected(state bool) SetStateFunc {
} }
func ConnectionActivity(desc string) SetStateFunc { func ConnectionActivity(desc string) SetStateFunc {
return func(s *State) { return func(s *State, folder string) {
s.ConnActivity = desc s.ConnActivity = desc
} }
} }
func SearchFilterClear() SetStateFunc { func SearchFilterClear() SetStateFunc {
return func(s *State) { return func(s *State, folder string) {
s.Search = "" s.folderState(folder).Search = ""
s.FilterActivity = "" s.folderState(folder).FilterActivity = ""
s.Filter = "" s.folderState(folder).Filter = ""
} }
} }
func FilterActivity(str string) SetStateFunc { func FilterActivity(str string) SetStateFunc {
return func(s *State) { return func(s *State, folder string) {
s.FilterActivity = str s.folderState(folder).FilterActivity = str
} }
} }
func FilterResult(str string) SetStateFunc { func FilterResult(str string) SetStateFunc {
return func(s *State) { return func(s *State, folder string) {
s.FilterActivity = "" s.folderState(folder).FilterActivity = ""
s.Filter = concatFilters(s.Filter, str) s.folderState(folder).Filter = concatFilters(s.folderState(folder).Filter, str)
} }
} }
@ -109,22 +104,22 @@ func concatFilters(existing, next string) string {
} }
func Search(desc string) SetStateFunc { func Search(desc string) SetStateFunc {
return func(s *State) { return func(s *State, folder string) {
s.Search = desc s.folderState(folder).Search = desc
} }
} }
func Threading(on bool) SetStateFunc { func Threading(on bool) SetStateFunc {
return func(s *State) { return func(s *State, folder string) {
s.Threading = "" s.folderState(folder).Threading = ""
if on { if on {
s.Threading = "threading" s.folderState(folder).Threading = "threading"
} }
} }
} }
func Passthrough(on bool) SetStateFunc { func Passthrough(on bool) SetStateFunc {
return func(s *State) { return func(s *State, folder string) {
s.Passthrough = "" s.Passthrough = ""
if on { if on {
s.Passthrough = "passthrough" s.Passthrough = "passthrough"

View file

@ -32,7 +32,6 @@ type AccountView struct {
msglist *MessageList msglist *MessageList
worker *types.Worker worker *types.Worker
state *statusline.State state *statusline.State
update bool
} }
func (acct *AccountView) UiConfig() config.UIConfig { func (acct *AccountView) UiConfig() config.UIConfig {
@ -112,13 +111,13 @@ func (acct *AccountView) Tick() bool {
func (acct *AccountView) SetStatus(setters ...statusline.SetStateFunc) { func (acct *AccountView) SetStatus(setters ...statusline.SetStateFunc) {
for _, fn := range setters { for _, fn := range setters {
fn(acct.state) fn(acct.state, acct.SelectedDirectory())
} }
acct.update = true acct.UpdateStatus()
} }
func (acct *AccountView) UpdateStatus() { func (acct *AccountView) UpdateStatus() {
acct.host.SetStatus(acct.state.String()) acct.host.SetStatus(acct.state.StatusLine(acct.SelectedDirectory()))
} }
func (acct *AccountView) PushStatus(status string, expiry time.Duration) { func (acct *AccountView) PushStatus(status string, expiry time.Duration) {
@ -160,10 +159,6 @@ func (acct *AccountView) Invalidate() {
} }
func (acct *AccountView) Draw(ctx *ui.Context) { func (acct *AccountView) Draw(ctx *ui.Context) {
if acct.update {
acct.UpdateStatus()
acct.update = false
}
acct.grid.Draw(ctx) acct.grid.Draw(ctx)
} }
@ -322,6 +317,7 @@ func (acct *AccountView) onMessage(msg types.WorkerMessage) {
acct.logger.Printf("%v", msg.Error) acct.logger.Printf("%v", msg.Error)
acct.PushError(msg.Error) acct.PushError(msg.Error)
} }
acct.UpdateStatus()
} }
func (acct *AccountView) getSortCriteria() []*types.SortCriterion { func (acct *AccountView) getSortCriteria() []*types.SortCriterion {