Skip to content

Commit 355a7bf

Browse files
committed
Set focus right after creating windows/panes and before running any
pane commands.
1 parent fb33618 commit 355a7bf

File tree

3 files changed

+36
-24
lines changed

3 files changed

+36
-24
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@ Example showing all options for the root node of the config file
6767
```yaml
6868
name: example
6969
dir: /path/to/project
70-
pre_cmd: touch example.tmp
71-
post_cmd: rm example.tmp
70+
up_pre_cmd: (date; echo start) > run.log
71+
up_post_cmd: (date; echo done) >> run.log
72+
down_pre_cmd: touch example.tmp
73+
down_post_cmd: rm example.tmp
7274
windows:
7375
- name: code
7476
panes:

main.go

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@ type Window struct {
2929
}
3030

3131
type Project struct {
32-
Name string
33-
Dir string
34-
PreCmd string `yaml:"pre_cmd"`
35-
PostCmd string `yaml:"post_cmd"`
36-
Windows []*Window
32+
Name string
33+
Dir string
34+
UpPreCmd string `yaml:"up_pre_cmd"`
35+
UpPostCmd string `yaml:"up_post_cmd"`
36+
DownPreCmd string `yaml:"down_pre_cmd"`
37+
DownPostCmd string `yaml:"down_post_cmd"`
38+
Windows []*Window
3739
}
3840

3941
func run(format string, args ...interface{}) error {
@@ -181,8 +183,8 @@ func shellInDir(dir, cmd string) {
181183
}
182184

183185
func up(session string, project *Project) {
184-
if project.PreCmd != "" {
185-
shellInDir(project.Dir, project.PreCmd)
186+
if project.UpPreCmd != "" {
187+
shellInDir(project.Dir, project.UpPreCmd)
186188
}
187189

188190
// Spawn all the windows/panes
@@ -209,6 +211,17 @@ func up(session string, project *Project) {
209211
SelectLayout(target, w.Layout)
210212
}
211213

214+
// Set which window has focus
215+
for wi, w := range project.Windows {
216+
if w == nil {
217+
continue
218+
}
219+
if w.Focus {
220+
target := fmt.Sprintf("%s:%d", session, wi)
221+
SelectWindow(target)
222+
}
223+
}
224+
212225
// Run the commands concurrently
213226
for _, w := range project.Windows {
214227
if w == nil {
@@ -224,24 +237,21 @@ func up(session string, project *Project) {
224237
}
225238
runAll()
226239

227-
// Set which window has focus
228-
for wi, w := range project.Windows {
229-
if w == nil {
230-
continue
231-
}
232-
if w.Focus {
233-
target := fmt.Sprintf("%s:%d", session, wi)
234-
SelectWindow(target)
235-
}
236-
}
237-
238-
if project.PostCmd != "" {
239-
shellInDir(project.Dir, project.PostCmd)
240+
if project.UpPostCmd != "" {
241+
shellInDir(project.Dir, project.UpPostCmd)
240242
}
241243
}
242244

243245
func down(session string, project *Project) {
246+
if project.DownPreCmd != "" {
247+
shellInDir(project.Dir, project.DownPreCmd)
248+
}
249+
244250
KillSession(session)
251+
252+
if project.DownPostCmd != "" {
253+
shellInDir(project.Dir, project.DownPostCmd)
254+
}
245255
}
246256

247257
func main() {

test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: test
22
dir: ~/dev
3-
pre_cmd: rm tmux-compose/*.lock || true
4-
post_cmd: rm tmux-compose/*.lock || true
3+
up_pre_cmd: rm tmux-compose/*.lock || true
4+
down_post_cmd: rm tmux-compose/*.lock || true
55
windows:
66
- name: code
77
layout: main-vertical

0 commit comments

Comments
 (0)