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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
use anyhow::Result;
use clap::{Parser, Subcommand};
mod cli;
mod config;
mod git;
mod tmux;
mod tmux_control;
mod ui;
#[derive(Parser)]
#[command(name = "twig")]
#[command(about = "Tmux session manager with git worktree support")]
#[command(
after_long_help = "Debug: use --verbose or set TWIG_DEBUG=1 to enable verbose tmux control output on stderr."
)]
#[command(version)]
struct Cli {
/// Enable verbose tmux control output (sets TWIG_DEBUG=1)
#[arg(long, short, global = true)]
verbose: bool,
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
/// Start or attach to a session
#[command(alias = "s")]
Start {
/// Project name (interactive selection if not provided)
project: Option<String>,
},
/// List all projects
#[command(alias = "ls")]
List {
/// Focus on current TWIG_PROJECT/TWIG_WORKTREE
#[arg(long)]
focus_current: bool,
},
/// Create a new project
#[command(alias = "n")]
New {
/// Project name
name: Option<String>,
},
/// Edit project config in $EDITOR
#[command(alias = "e")]
Edit {
/// Project name
project: Option<String>,
},
/// Delete a project config
#[command(alias = "rm")]
Delete {
/// Project name
project: Option<String>,
},
/// Stop (kill) a tmux session
#[command(alias = "kill")]
Stop {
/// Session name
session: Option<String>,
},
/// Run a command in a tmux session
#[command(alias = "r")]
Run {
/// Command to run
#[arg(trailing_var_arg = true)]
command: Vec<String>,
/// Project/session name (defaults to TWIG_PROJECT when set)
#[arg(long)]
project: Option<String>,
/// Worktree branch name (defaults to TWIG_WORKTREE when set)
#[arg(long)]
tree: Option<String>,
/// Window index or name (defaults to current window if available)
#[arg(long)]
window: Option<String>,
/// Target pane index or id
#[arg(long)]
pane: Option<String>,
/// Tmux socket path to target
#[arg(long)]
socket: Option<String>,
},
/// Git worktree operations
#[command(alias = "t")]
Tree {
#[command(subcommand)]
action: TreeCommands,
},
/// Window operations
#[command(alias = "w")]
Window {
#[command(subcommand)]
action: WindowCommands,
},
}
#[derive(Subcommand)]
enum TreeCommands {
/// Create a new worktree and start a session
#[command(alias = "c")]
Create {
/// Project name
project: Option<String>,
/// Branch name
branch: Option<String>,
},
/// List worktrees for a project
#[command(alias = "ls")]
List {
/// Project name
project: Option<String>,
},
/// Delete a worktree and its session
#[command(alias = "rm")]
Delete {
/// Project name
project: Option<String>,
/// Branch name
branch: Option<String>,
},
/// Merge a worktree branch into main/master
#[command(alias = "m")]
Merge {
/// Project name
project: Option<String>,
/// Branch name
branch: Option<String>,
},
}
#[derive(Subcommand)]
enum WindowCommands {
/// Create a new window in an existing session
#[command(alias = "n")]
New {
/// Project/session name (interactive selection if not provided)
project: Option<String>,
/// Window name
name: Option<String>,
/// Tmux socket path to target
#[arg(long)]
socket: Option<String>,
},
/// List panes for a window
#[command(alias = "lp")]
ListPanes {
/// Window index or name
window: String,
/// Project/session name (defaults to current tmux session if available)
#[arg(long)]
project: Option<String>,
/// Tmux socket path to target
#[arg(long)]
socket: Option<String>,
/// Output as JSON
#[arg(long)]
json: bool,
},
}
fn main() -> Result<()> {
let cli = Cli::parse();
if cli.verbose {
std::env::set_var("TWIG_DEBUG", "1");
}
match cli.command {
Commands::Start { project } => cli::start::run(project),
Commands::List { focus_current } => cli::list::run(focus_current),
Commands::New { name } => cli::new::run(name),
Commands::Edit { project } => cli::edit::run(project),
Commands::Delete { project } => cli::delete::run(project),
Commands::Stop { session } => cli::kill::run(session),
Commands::Run {
command,
project,
tree,
window,
pane,
socket,
} => cli::window::run(project, tree, window, command, pane, socket),
Commands::Tree { action } => match action {
TreeCommands::Create { project, branch } => cli::worktree::create(project, branch),
TreeCommands::List { project } => cli::worktree::list(project),
TreeCommands::Delete { project, branch } => cli::worktree::delete(project, branch),
TreeCommands::Merge { project, branch } => cli::worktree::merge(project, branch),
},
Commands::Window { action } => match action {
WindowCommands::New {
project,
name,
socket,
} => cli::window::new(project, name, socket),
WindowCommands::ListPanes {
window,
project,
socket,
json,
} => cli::window::list_panes(project, window, socket, json),
},
}
}