2022-03-18 22:35:33 +01:00
|
|
|
package statusline
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-04-18 16:06:27 +02:00
|
|
|
|
|
|
|
"git.sr.ht/~rjarry/aerc/config"
|
2022-03-18 22:35:33 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type State struct {
|
2022-04-18 16:06:27 +02:00
|
|
|
separator string
|
|
|
|
renderer renderFunc
|
|
|
|
acct *accountState
|
|
|
|
fldr map[string]*folderState
|
|
|
|
width int
|
|
|
|
}
|
2022-03-18 22:35:33 +01:00
|
|
|
|
2022-04-18 16:06:27 +02:00
|
|
|
type accountState struct {
|
|
|
|
Name string
|
|
|
|
Multiple bool
|
2022-03-18 22:35:33 +01:00
|
|
|
ConnActivity string
|
|
|
|
Connected bool
|
2022-04-18 16:06:27 +02:00
|
|
|
Passthrough bool
|
|
|
|
}
|
2022-03-18 22:35:33 +01:00
|
|
|
|
2022-04-18 16:06:27 +02:00
|
|
|
type folderState struct {
|
|
|
|
Name string
|
|
|
|
Search string
|
|
|
|
Filter string
|
|
|
|
FilterActivity string
|
|
|
|
Sorting bool
|
|
|
|
Threading bool
|
2022-03-18 22:35:33 +01:00
|
|
|
}
|
|
|
|
|
2022-04-18 16:06:27 +02:00
|
|
|
func NewState(name string, multipleAccts bool, conf config.StatuslineConfig) *State {
|
2022-07-31 22:16:40 +02:00
|
|
|
return &State{
|
|
|
|
separator: conf.Separator,
|
|
|
|
renderer: newRenderer(conf.RenderFormat, conf.DisplayMode),
|
|
|
|
acct: &accountState{Name: name, Multiple: multipleAccts},
|
|
|
|
fldr: make(map[string]*folderState),
|
2022-04-18 16:06:27 +02:00
|
|
|
}
|
2022-03-18 22:35:33 +01:00
|
|
|
}
|
|
|
|
|
2022-03-21 22:18:51 +01:00
|
|
|
func (s *State) StatusLine(folder string) string {
|
2022-04-18 16:06:27 +02:00
|
|
|
return s.renderer(renderParams{
|
|
|
|
width: s.width,
|
|
|
|
sep: s.separator,
|
|
|
|
acct: s.acct,
|
|
|
|
fldr: s.folderState(folder),
|
|
|
|
})
|
2022-03-18 22:35:33 +01:00
|
|
|
}
|
|
|
|
|
2022-03-21 22:18:51 +01:00
|
|
|
func (s *State) folderState(folder string) *folderState {
|
2022-04-18 16:06:27 +02:00
|
|
|
if _, ok := s.fldr[folder]; !ok {
|
|
|
|
s.fldr[folder] = &folderState{Name: folder}
|
|
|
|
}
|
|
|
|
return s.fldr[folder]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) SetWidth(w int) bool {
|
|
|
|
changeState := false
|
|
|
|
if s.width != w {
|
|
|
|
s.width = w
|
|
|
|
changeState = true
|
2022-03-21 22:18:51 +01:00
|
|
|
}
|
2022-04-18 16:06:27 +02:00
|
|
|
return changeState
|
2022-03-21 22:18:51 +01:00
|
|
|
}
|
|
|
|
|
2022-05-30 14:34:18 +02:00
|
|
|
func (s *State) Connected() bool {
|
|
|
|
return s.acct.Connected
|
|
|
|
}
|
|
|
|
|
2022-03-21 22:18:51 +01:00
|
|
|
type SetStateFunc func(s *State, folder string)
|
2022-03-18 22:35:33 +01:00
|
|
|
|
2022-05-30 14:34:18 +02:00
|
|
|
func SetConnected(state bool) SetStateFunc {
|
2022-03-21 22:18:51 +01:00
|
|
|
return func(s *State, folder string) {
|
2022-04-18 16:06:27 +02:00
|
|
|
s.acct.ConnActivity = ""
|
|
|
|
s.acct.Connected = state
|
2022-03-18 22:35:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ConnectionActivity(desc string) SetStateFunc {
|
2022-03-21 22:18:51 +01:00
|
|
|
return func(s *State, folder string) {
|
2022-04-18 16:06:27 +02:00
|
|
|
s.acct.ConnActivity = desc
|
2022-03-18 22:35:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func SearchFilterClear() SetStateFunc {
|
2022-03-21 22:18:51 +01:00
|
|
|
return func(s *State, folder string) {
|
|
|
|
s.folderState(folder).Search = ""
|
|
|
|
s.folderState(folder).FilterActivity = ""
|
|
|
|
s.folderState(folder).Filter = ""
|
2022-03-18 22:35:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func FilterActivity(str string) SetStateFunc {
|
2022-03-21 22:18:51 +01:00
|
|
|
return func(s *State, folder string) {
|
|
|
|
s.folderState(folder).FilterActivity = str
|
2022-03-18 22:35:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func FilterResult(str string) SetStateFunc {
|
2022-03-21 22:18:51 +01:00
|
|
|
return func(s *State, folder string) {
|
|
|
|
s.folderState(folder).FilterActivity = ""
|
|
|
|
s.folderState(folder).Filter = concatFilters(s.folderState(folder).Filter, str)
|
2022-03-18 22:35:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func concatFilters(existing, next string) string {
|
|
|
|
if existing == "" {
|
|
|
|
return next
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s && %s", existing, next)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Search(desc string) SetStateFunc {
|
2022-03-21 22:18:51 +01:00
|
|
|
return func(s *State, folder string) {
|
|
|
|
s.folderState(folder).Search = desc
|
2022-03-18 22:35:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 23:12:16 +01:00
|
|
|
func Sorting(on bool) SetStateFunc {
|
|
|
|
return func(s *State, folder string) {
|
2022-04-18 16:06:27 +02:00
|
|
|
s.folderState(folder).Sorting = on
|
2022-03-24 23:12:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-18 22:35:33 +01:00
|
|
|
func Threading(on bool) SetStateFunc {
|
2022-03-21 22:18:51 +01:00
|
|
|
return func(s *State, folder string) {
|
2022-04-18 16:06:27 +02:00
|
|
|
s.folderState(folder).Threading = on
|
2022-03-18 22:35:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Passthrough(on bool) SetStateFunc {
|
2022-03-21 22:18:51 +01:00
|
|
|
return func(s *State, folder string) {
|
2022-04-18 16:06:27 +02:00
|
|
|
s.acct.Passthrough = on
|
2022-03-18 22:35:33 +01:00
|
|
|
}
|
|
|
|
}
|