aboutsummaryrefslogtreecommitdiffstats
path: root/lib/state/state.go
diff options
context:
space:
mode:
authorRobin Jarry <robin@jarry.cc>2023-02-05 23:23:02 +0100
committerRobin Jarry <robin@jarry.cc>2023-02-20 14:48:42 +0100
commit6af06c9dfec03e923589d34187ba8358e3423d5c (patch)
tree3722f17464ca651ebd12d7d6d55a0e97ae72c8ec /lib/state/state.go
parent34db5942bd7b642107002b75de9d5d5c7fe90e4c (diff)
downloadaerc-6af06c9dfec03e923589d34187ba8358e3423d5c.tar.gz
statusline: move files to lib/state
These modules will not handle statusline rendering after next commit. Move them in lib/state to make next commit easier to review. Signed-off-by: Robin Jarry <robin@jarry.cc> Acked-by: Tim Culverhouse <tim@timculverhouse.com>
Diffstat (limited to 'lib/state/state.go')
-rw-r--r--lib/state/state.go135
1 files changed, 135 insertions, 0 deletions
diff --git a/lib/state/state.go b/lib/state/state.go
new file mode 100644
index 00000000..b5f925ac
--- /dev/null
+++ b/lib/state/state.go
@@ -0,0 +1,135 @@
+package state
+
+import (
+ "fmt"
+
+ "git.sr.ht/~rjarry/aerc/config"
+)
+
+type State struct {
+ renderer renderFunc
+ acct *accountState
+ fldr map[string]*folderState
+ width int
+}
+
+type accountState struct {
+ Name string
+ Multiple bool
+ ConnActivity string
+ Connected bool
+ Passthrough bool
+}
+
+type folderState struct {
+ Name string
+ Search string
+ Filter string
+ FilterActivity string
+ Sorting bool
+ Threading bool
+}
+
+func NewState(name string, multipleAccts bool) *State {
+ return &State{
+ renderer: newRenderer(),
+ acct: &accountState{Name: name, Multiple: multipleAccts},
+ fldr: make(map[string]*folderState),
+ }
+}
+
+func (s *State) StatusLine(folder string) string {
+ return s.renderer(renderParams{
+ width: s.width,
+ sep: config.Statusline.Separator,
+ acct: s.acct,
+ fldr: s.folderState(folder),
+ })
+}
+
+func (s *State) folderState(folder string) *folderState {
+ 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
+ }
+ return changeState
+}
+
+func (s *State) Connected() bool {
+ return s.acct.Connected
+}
+
+type SetStateFunc func(s *State, folder string)
+
+func SetConnected(state bool) SetStateFunc {
+ return func(s *State, folder string) {
+ s.acct.ConnActivity = ""
+ s.acct.Connected = state
+ }
+}
+
+func ConnectionActivity(desc string) SetStateFunc {
+ return func(s *State, folder string) {
+ s.acct.ConnActivity = desc
+ }
+}
+
+func SearchFilterClear() SetStateFunc {
+ return func(s *State, folder string) {
+ s.folderState(folder).Search = ""
+ s.folderState(folder).FilterActivity = ""
+ s.folderState(folder).Filter = ""
+ }
+}
+
+func FilterActivity(str string) SetStateFunc {
+ return func(s *State, folder string) {
+ s.folderState(folder).FilterActivity = str
+ }
+}
+
+func FilterResult(str string) SetStateFunc {
+ return func(s *State, folder string) {
+ s.folderState(folder).FilterActivity = ""
+ s.folderState(folder).Filter = concatFilters(s.folderState(folder).Filter, str)
+ }
+}
+
+func concatFilters(existing, next string) string {
+ if existing == "" {
+ return next
+ }
+ return fmt.Sprintf("%s && %s", existing, next)
+}
+
+func Search(desc string) SetStateFunc {
+ return func(s *State, folder string) {
+ s.folderState(folder).Search = desc
+ }
+}
+
+func Sorting(on bool) SetStateFunc {
+ return func(s *State, folder string) {
+ s.folderState(folder).Sorting = on
+ }
+}
+
+func Threading(on bool) SetStateFunc {
+ return func(s *State, folder string) {
+ s.folderState(folder).Threading = on
+ }
+}
+
+func Passthrough(on bool) SetStateFunc {
+ return func(s *State, folder string) {
+ s.acct.Passthrough = on
+ }
+}