2022-03-18 21:35:33 +00:00
|
|
|
package statusline
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type State struct {
|
|
|
|
Name string
|
|
|
|
Multiple bool
|
|
|
|
Separator string
|
|
|
|
|
|
|
|
Connection string
|
|
|
|
ConnActivity string
|
|
|
|
Connected bool
|
|
|
|
|
|
|
|
Passthrough string
|
2022-03-21 21:18:51 +00:00
|
|
|
|
|
|
|
fs map[string]*folderState
|
2022-03-18 21:35:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewState(name string, multipleAccts bool, sep string) *State {
|
2022-03-21 21:18:51 +00:00
|
|
|
return &State{Name: name, Multiple: multipleAccts, Separator: sep,
|
|
|
|
fs: make(map[string]*folderState)}
|
2022-03-18 21:35:33 +00:00
|
|
|
}
|
|
|
|
|
2022-03-21 21:18:51 +00:00
|
|
|
func (s *State) StatusLine(folder string) string {
|
2022-03-18 21:35:33 +00:00
|
|
|
var line []string
|
|
|
|
if s.Connection != "" || s.ConnActivity != "" {
|
|
|
|
conn := s.Connection
|
|
|
|
if s.ConnActivity != "" {
|
|
|
|
conn = s.ConnActivity
|
|
|
|
}
|
|
|
|
if s.Multiple {
|
|
|
|
line = append(line, fmt.Sprintf("[%s] %s", s.Name, conn))
|
|
|
|
} else {
|
|
|
|
line = append(line, conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if s.Connected {
|
|
|
|
if s.Passthrough != "" {
|
|
|
|
line = append(line, s.Passthrough)
|
|
|
|
}
|
2022-03-21 21:18:51 +00:00
|
|
|
if folder != "" {
|
|
|
|
line = append(line, s.folderState(folder).State()...)
|
|
|
|
}
|
2022-03-18 21:35:33 +00:00
|
|
|
}
|
|
|
|
return strings.Join(line, s.Separator)
|
|
|
|
}
|
|
|
|
|
2022-03-21 21:18:51 +00:00
|
|
|
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)
|
2022-03-18 21:35:33 +00:00
|
|
|
|
|
|
|
func Connected(state bool) SetStateFunc {
|
2022-03-21 21:18:51 +00:00
|
|
|
return func(s *State, folder string) {
|
2022-03-18 21:35:33 +00:00
|
|
|
s.ConnActivity = ""
|
|
|
|
s.Connected = state
|
|
|
|
if state {
|
|
|
|
s.Connection = "Connected"
|
|
|
|
} else {
|
|
|
|
s.Connection = "Disconnected"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ConnectionActivity(desc string) SetStateFunc {
|
2022-03-21 21:18:51 +00:00
|
|
|
return func(s *State, folder string) {
|
2022-03-18 21:35:33 +00:00
|
|
|
s.ConnActivity = desc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func SearchFilterClear() SetStateFunc {
|
2022-03-21 21:18:51 +00:00
|
|
|
return func(s *State, folder string) {
|
|
|
|
s.folderState(folder).Search = ""
|
|
|
|
s.folderState(folder).FilterActivity = ""
|
|
|
|
s.folderState(folder).Filter = ""
|
2022-03-18 21:35:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func FilterActivity(str string) SetStateFunc {
|
2022-03-21 21:18:51 +00:00
|
|
|
return func(s *State, folder string) {
|
|
|
|
s.folderState(folder).FilterActivity = str
|
2022-03-18 21:35:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func FilterResult(str string) SetStateFunc {
|
2022-03-21 21:18:51 +00:00
|
|
|
return func(s *State, folder string) {
|
|
|
|
s.folderState(folder).FilterActivity = ""
|
|
|
|
s.folderState(folder).Filter = concatFilters(s.folderState(folder).Filter, str)
|
2022-03-18 21:35:33 +00: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 21:18:51 +00:00
|
|
|
return func(s *State, folder string) {
|
|
|
|
s.folderState(folder).Search = desc
|
2022-03-18 21:35:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 22:12:16 +00:00
|
|
|
func Sorting(on bool) SetStateFunc {
|
|
|
|
return func(s *State, folder string) {
|
|
|
|
s.folderState(folder).Sorting = ""
|
|
|
|
if on {
|
|
|
|
s.folderState(folder).Sorting = "sorting"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-18 21:35:33 +00:00
|
|
|
func Threading(on bool) SetStateFunc {
|
2022-03-21 21:18:51 +00:00
|
|
|
return func(s *State, folder string) {
|
|
|
|
s.folderState(folder).Threading = ""
|
2022-03-18 21:35:33 +00:00
|
|
|
if on {
|
2022-03-21 21:18:51 +00:00
|
|
|
s.folderState(folder).Threading = "threading"
|
2022-03-18 21:35:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Passthrough(on bool) SetStateFunc {
|
2022-03-21 21:18:51 +00:00
|
|
|
return func(s *State, folder string) {
|
2022-03-18 21:35:33 +00:00
|
|
|
s.Passthrough = ""
|
|
|
|
if on {
|
|
|
|
s.Passthrough = "passthrough"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|