From ce18e928813526e59462e391c09e868c62facb42 Mon Sep 17 00:00:00 2001 From: Koni Marti Date: Mon, 18 Apr 2022 16:06:27 +0200 Subject: statusline: refactor to make it more customizable Refactor statusline by clearly separating the rendering part from the text display. Use printf-like format string for statusline customization. Document printf-like format string to customize the statusline. Allow to completely mute the statusline (except for push notifications) with a format specifier. Provide a display mode with unicode icons for the status elements. Implements: https://todo.sr.ht/~rjarry/aerc/34 Signed-off-by: Koni Marti Acked-by: Robin Jarry --- lib/statusline/state.go | 105 +++++++++++++++++++++++------------------------- 1 file changed, 50 insertions(+), 55 deletions(-) (limited to 'lib/statusline/state.go') diff --git a/lib/statusline/state.go b/lib/statusline/state.go index 895bb2c4..3fecd0fe 100644 --- a/lib/statusline/state.go +++ b/lib/statusline/state.go @@ -2,76 +2,80 @@ package statusline import ( "fmt" - "strings" + + "git.sr.ht/~rjarry/aerc/config" ) type State struct { - Name string - Multiple bool - Separator string + separator string + renderer renderFunc + acct *accountState + fldr map[string]*folderState + width int +} - Connection string +type accountState struct { + Name string + Multiple bool ConnActivity string Connected bool + Passthrough bool +} - Passthrough string - - fs map[string]*folderState +type folderState struct { + Name string + Search string + Filter string + FilterActivity string + Sorting bool + Threading bool } -func NewState(name string, multipleAccts bool, sep string) *State { - return &State{Name: name, Multiple: multipleAccts, Separator: sep, - fs: make(map[string]*folderState)} +func NewState(name string, multipleAccts bool, conf config.StatuslineConfig) *State { + return &State{separator: conf.Separator, + renderer: newRenderer(conf.RenderFormat, conf.DisplayMode), + acct: &accountState{Name: name, Multiple: multipleAccts}, + fldr: make(map[string]*folderState), + } } func (s *State) StatusLine(folder string) string { - 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) - } - if folder != "" { - line = append(line, s.folderState(folder).State()...) - } - } - return strings.Join(line, s.Separator) + return s.renderer(renderParams{ + width: s.width, + sep: s.separator, + acct: s.acct, + fldr: s.folderState(folder), + }) } func (s *State) folderState(folder string) *folderState { - if _, ok := s.fs[folder]; !ok { - s.fs[folder] = &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 s.fs[folder] + return changeState } type SetStateFunc func(s *State, folder string) func Connected(state bool) SetStateFunc { return func(s *State, folder string) { - s.ConnActivity = "" - s.Connected = state - if state { - s.Connection = "Connected" - } else { - s.Connection = "Disconnected" - } + s.acct.ConnActivity = "" + s.acct.Connected = state } } func ConnectionActivity(desc string) SetStateFunc { return func(s *State, folder string) { - s.ConnActivity = desc + s.acct.ConnActivity = desc } } @@ -111,27 +115,18 @@ func Search(desc string) SetStateFunc { func Sorting(on bool) SetStateFunc { return func(s *State, folder string) { - s.folderState(folder).Sorting = "" - if on { - s.folderState(folder).Sorting = "sorting" - } + s.folderState(folder).Sorting = on } } func Threading(on bool) SetStateFunc { return func(s *State, folder string) { - s.folderState(folder).Threading = "" - if on { - s.folderState(folder).Threading = "threading" - } + s.folderState(folder).Threading = on } } func Passthrough(on bool) SetStateFunc { return func(s *State, folder string) { - s.Passthrough = "" - if on { - s.Passthrough = "passthrough" - } + s.acct.Passthrough = on } } -- cgit