Skip to content

Commit 7a74eb9

Browse files
chitacanclaudehappy-otter
committed
Add --list option to template command and clean orphan tmux windows
- Add -l/--list option to explicitly list templates - Remove tmux windows not defined in the template after applying Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
1 parent c51917d commit 7a74eb9

3 files changed

Lines changed: 22 additions & 2 deletions

File tree

completions/wttw.fish

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ complete -f -c wttw -n '__fish_wttw_using_command tmux; or __fish_wttw_using_com
8080

8181
# template command
8282
complete -f -c wttw -n '__fish_wttw_using_command template; or __fish_wttw_using_command tp' -a '(__fish_wttw_templates)'
83+
complete -f -c wttw -n '__fish_wttw_using_command template; or __fish_wttw_using_command tp' -d "list templates" -s l -l list
8384

8485
# open command
8586
complete -f -c wttw -n '__fish_wttw_using_command open; or __fish_wttw_using_command o' -a '(__fish_wttw_worktrees)'

lib/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const {
2626
getTmuxWindowId,
2727
createSession,
2828
switchSession,
29+
cleanUnknownWindows,
2930
} = require('./tmux');
3031
const {
3132
checks,
@@ -393,6 +394,7 @@ cmd.command('open [branch]')
393394
cmd.command('template [template-name]')
394395
.alias('tp')
395396
.description('list templates or create tmux windows from a template')
397+
.option('-l, --list', 'list templates')
396398
.on('--help', () => {
397399
console.log('');
398400
console.log(' Template file: ~/.config/wttw/<template-name>.json');
@@ -415,9 +417,9 @@ cmd.command('template [template-name]')
415417
console.log(' windows[].pane pane count (optional, default: config paneCount)');
416418
console.log(' windows[].cmd command to run (optional)');
417419
})
418-
.action(async (templateName, {parent: {dryRun}}) => {
420+
.action(async (templateName, {list, parent: {dryRun}}) => {
419421
try {
420-
if (!templateName) {
422+
if (list || !templateName) {
421423
const templates = listTemplates();
422424
if (templates.length === 0) {
423425
console.log('No templates found. Create one at ~/.config/wttw/<name>.json');
@@ -474,6 +476,7 @@ cmd.command('template [template-name]')
474476
}
475477

476478
if (!dryRun) {
479+
await cleanUnknownWindows(session, windows.map(w => w.name));
477480
note(`template "${templateName}" applied`);
478481
}
479482
} catch (err) {

lib/tmux.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,22 @@ exports.switchSession = async (session, window, wtd, focus = false) => {
105105
}
106106
}
107107

108+
exports.cleanUnknownWindows = async (session, knownNames) => {
109+
const known = new Set(knownNames);
110+
const {stdout} = await command(`tmux list-windows -t ${session} -F #{window_id}\t#W`);
111+
const orphans = stdout.split(EOL)
112+
.filter(Boolean)
113+
.map(l => l.split('\t'))
114+
.filter(([, name]) => !known.has(name));
115+
116+
for (const [id] of orphans) {
117+
await command(`tmux kill-window -t ${id}`);
118+
}
119+
if (orphans.length > 0) {
120+
await command(`tmux move-window -r -t ${session}`);
121+
}
122+
}
123+
108124
exports.showPopup = async (wtd) => {
109125
const taskPath = join(wtd, '.context', 'TASK.md');
110126
if (!existsSync(taskPath)) {

0 commit comments

Comments
 (0)