Skip to content

Commit 562da04

Browse files
feat: add attach config option (#145)
* feat: add attach config option Currently, auto-attach requires using the -a flag on every smug start command. This adds an optional 'attach' field to the config file, allowing per-project control over attachment behavior. The -a flag still works and takes precedence, so this is backward compatible. Example usage in config: session: my-project attach: true windows: - name: editor commands: - vim * test: add tests for attach config option Add tests to verify: - attach: true is parsed correctly - attach defaults to false when not specified * docs: add comment for attach config field * docs: add attach option to README
1 parent 9542d4a commit 562da04

4 files changed

Lines changed: 47 additions & 2 deletions

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ xyz@localhost:~$ smug start -f ./project.yml -w window1 -w window2
151151
Configuration files can stored in the `~/.config/smug` directory in the `YAML` format, e.g `~/.config/smug/your_project.yml`.
152152
You may also create a file named `.smug.yml` in the current working directory, which will be used by default.
153153

154+
### Session-level options
155+
156+
- `attach` - Automatically attach to the session after creation (defaults to `false`). The `-a` flag can also enable attachment.
157+
154158
### Examples
155159

156160
#### Example 1
@@ -199,6 +203,7 @@ windows:
199203
200204
```yaml
201205
session: blog
206+
attach: true # Automatically attach to this session
202207

203208
root: ~/Code/blog
204209

config.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ type Window struct {
4141
type Config struct {
4242
SendKeysTimeout int `yaml:"sendkeys_timeout"`
4343
Session string `yaml:"session"`
44-
TmuxOptions `yaml:"tmux_options"`
44+
// Attach controls whether the session automatically attaches after creation.
45+
// The -a/--attach CLI flag can also enable attachment.
46+
Attach bool `yaml:"attach,omitempty"`
47+
TmuxOptions `yaml:"tmux_options"`
4548
Env map[string]string `yaml:"env"`
4649
Root string `yaml:"root"`
4750
BeforeStart []string `yaml:"before_start"`

config_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,40 @@ windows:
5858
t.Fatalf("expected %v, got %v", expected, config)
5959
}
6060
}
61+
62+
func TestParseConfigWithAttach(t *testing.T) {
63+
yaml := `
64+
session: test
65+
attach: true
66+
windows:
67+
- name: editor
68+
commands:
69+
- vim`
70+
71+
config, err := ParseConfig(yaml, map[string]string{})
72+
if err != nil {
73+
t.Fatal(err)
74+
}
75+
76+
if !config.Attach {
77+
t.Fatal("expected attach to be true, got false")
78+
}
79+
}
80+
81+
func TestParseConfigWithoutAttach(t *testing.T) {
82+
yaml := `
83+
session: test
84+
windows:
85+
- name: editor
86+
commands:
87+
- vim`
88+
89+
config, err := ParseConfig(yaml, map[string]string{})
90+
if err != nil {
91+
t.Fatal(err)
92+
}
93+
94+
if config.Attach {
95+
t.Fatal("expected attach to be false by default, got true")
96+
}
97+
}

smug.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func (smug Smug) Start(config *Config, options *Options, context Context) error
108108
sessionExists := smug.tmux.SessionExists(sessionName)
109109
sessionRoot := ExpandPath(config.Root)
110110
windows := options.Windows
111-
attach := options.Attach
111+
attach := options.Attach || config.Attach
112112

113113
if !sessionExists && !createWindowsInsideCurrSession {
114114
err := smug.execShellCommands(config.BeforeStart, sessionRoot)

0 commit comments

Comments
 (0)