aboutsummaryrefslogtreecommitdiffstats
path: root/commands
diff options
context:
space:
mode:
authorKoni Marti <koni.marti@gmail.com>2024-08-07 20:52:37 +0200
committerRobin Jarry <robin@jarry.cc>2024-08-20 12:26:47 +0200
commit11f57b5f3378f944ea54a8a9de4ca040c62f5b12 (patch)
treee00d3ff6732351d4abbe56d856fe96c1138067bc /commands
parent054b5edc002008f20210f79e384381560a4f753d (diff)
downloadaerc-11f57b5f3378f944ea54a8a9de4ca040c62f5b12.tar.gz
commands: add reload
Add the reload command that performs a reload of config files. The reload command supports reloading the binds.conf and aerc.conf config files. Reloading will reload account views (including the directory list), message viewers, and composers. Signed-off-by: Koni Marti <koni.marti@gmail.com> Tested-by: Inwit <inwit@sindominio.net> Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'commands')
-rw-r--r--commands/reload.go180
1 files changed, 180 insertions, 0 deletions
diff --git a/commands/reload.go b/commands/reload.go
new file mode 100644
index 00000000..cd32bb36
--- /dev/null
+++ b/commands/reload.go
@@ -0,0 +1,180 @@
+package commands
+
+import (
+ "os"
+
+ "git.sr.ht/~rjarry/aerc/app"
+ "git.sr.ht/~rjarry/aerc/config"
+ "git.sr.ht/~rjarry/aerc/lib/log"
+ "git.sr.ht/~rjarry/aerc/lib/ui"
+)
+
+type Reload struct {
+ Binds bool `opt:"-B"`
+ Conf bool `opt:"-C"`
+ Style string `opt:"-s" complete:"CompleteStyle"`
+}
+
+func init() {
+ Register(Reload{})
+}
+
+func (r *Reload) CompleteStyle(s string) []string {
+ var files []string
+ for _, dir := range config.Ui.StyleSetDirs {
+ entries, err := os.ReadDir(dir)
+ if err != nil {
+ log.Debugf("could not read directory '%s': %v", dir,
+ err)
+ continue
+ }
+ for _, e := range entries {
+ if e.IsDir() {
+ continue
+ }
+ files = append(files, e.Name())
+ }
+ }
+ return FilterList(files, s, nil)
+}
+
+func (Reload) Context() CommandContext {
+ return GLOBAL
+}
+
+func (Reload) Aliases() []string {
+ return []string{"reload"}
+}
+
+func (r Reload) Execute(args []string) error {
+ reconfigure := false
+
+ if r.Binds {
+ f, err := config.ReloadBinds()
+ if err != nil {
+ return err
+ }
+ app.PushSuccess("Binds reloaded: " + f)
+ }
+
+ if r.Conf {
+ f, err := config.ReloadConf()
+ if err != nil {
+ return err
+ }
+ app.PushSuccess("Conf reloaded: " + f)
+ reconfigure = true
+ }
+
+ if r.Style != "" {
+ config.Ui.ClearCache()
+ config.Ui.StyleSetName = r.Style
+ err := config.Ui.LoadStyle()
+ if err != nil {
+ return err
+ }
+ app.PushSuccess("Styleset: " + r.Style)
+ reconfigure = true
+ }
+
+ if !reconfigure {
+ return nil
+ }
+
+ // reload account views and message stores
+ for _, name := range app.AccountNames() {
+
+ // rebuild account view
+ view, err := app.Account(name)
+ if err != nil {
+ continue
+ }
+
+ dirlist := view.Directories()
+ if dirlist == nil {
+ continue
+ }
+
+ wantTree := config.Ui.ForAccount(name).DirListTree
+ dirlist = adjustDirlist(dirlist, wantTree)
+ view.SetDirectories(dirlist)
+
+ // now rebuild grid with correct dirlist
+ view.Configure()
+
+ // reconfigure the message stores
+ for _, dir := range dirlist.List() {
+ store, ok := dirlist.MsgStore(dir)
+ if !ok {
+ continue
+ }
+ uiConf := dirlist.UiConfig(dir)
+ store.Configure(view.SortCriteria(uiConf))
+ }
+ ui.Invalidate()
+ }
+
+ // reload message viewers
+ doTabs(func(tab *ui.Tab) {
+ mv, ok := tab.Content.(*app.MessageViewer)
+ if !ok {
+ return
+ }
+ reloaded := app.NewMessageViewer(
+ mv.SelectedAccount(),
+ mv.MessageView(),
+ )
+ app.ReplaceTab(mv, reloaded, tab.Name, false)
+ })
+
+ // reload composers
+ doTabs(func(tab *ui.Tab) {
+ c, ok := tab.Content.(*app.Composer)
+ if !ok {
+ return
+ }
+ _ = c.SwitchAccount(c.Account())
+ })
+
+ return nil
+}
+
+func adjustDirlist(d app.DirectoryLister, wantTree bool) app.DirectoryLister {
+ switch d := d.(type) {
+ case *app.DirectoryList:
+ if wantTree {
+ log.Tracef("dirlist: build tree")
+ tree := app.NewDirectoryTree(d)
+ tree.SelectedMsgStore()
+ return tree
+ }
+ log.Tracef("dirlist: already dirlist")
+ return d
+ case *app.DirectoryTree:
+ if !wantTree {
+ log.Tracef("dirtree: get dirlist")
+ return d.DirectoryList
+ }
+ log.Tracef("dirtree: already tree")
+ return d
+ default:
+ return d
+ }
+}
+
+func doTabs(do func(*ui.Tab)) {
+ var tabname string
+ if t := app.SelectedTab(); t != nil {
+ tabname = t.Name
+ }
+ for i := range app.TabNames() {
+ tab := app.GetTab(i)
+ if tab == nil {
+ continue
+ }
+ do(tab)
+ }
+ if tabname != "" {
+ app.SelectTab(tabname)
+ }
+}