aboutsummaryrefslogtreecommitdiffstats
path: root/commands/reload.go
blob: 0d4c489a593404713e0ba6e603689e59ed7e4dba (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
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" desc:"Reload binds.conf."`
	Conf  bool   `opt:"-C" desc:"Reload aerc.conf."`
	Style string `opt:"-s" complete:"CompleteStyle" desc:"Reload the specified styleset."`
}

func init() {
	Register(Reload{})
}

func (Reload) Description() string {
	return "Hot-reload configuration files."
}

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 {
	if !r.Binds && !r.Conf && r.Style == "" {
		r.Binds = true
		r.Conf = true
		r.Style = config.Ui.StyleSetName
	}

	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, err := app.NewMessageViewer(
			mv.SelectedAccount(),
			mv.MessageView(),
		)
		if err != nil {
			app.PushError(err.Error())
			return
		}
		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)
	}
}