1pub fn generate_init(shell: &str, wrap_git: bool, no_cd: bool) -> String {
2 match shell {
3 "bash" => generate_bash(wrap_git, no_cd),
4 "zsh" => generate_zsh(wrap_git, no_cd),
5 "fish" => generate_fish(wrap_git, no_cd),
6 "powershell" | "pwsh" => generate_powershell(wrap_git, no_cd),
7 _ => format!("# Unsupported shell: {}\n", shell),
8 }
9}
10
11fn generate_bash(wrap_git: bool, no_cd: bool) -> String {
12 let mut output = String::new();
13
14 output.push_str("# git-workty shell integration for bash\n\n");
15
16 if !no_cd {
17 output.push_str(r#"# wcd - fuzzy select and cd to a worktree
18wcd() {
19 local dir
20 dir="$(git workty pick 2>/dev/null)"
21 if [ -n "$dir" ] && [ -d "$dir" ]; then
22 cd "$dir" || return 1
23 fi
24}
25
26# wnew - create new worktree and cd into it
27wnew() {
28 if [ -z "$1" ]; then
29 echo "Usage: wnew <branch-name>" >&2
30 return 1
31 fi
32 local dir
33 dir="$(git workty new "$@" --print-path 2>/dev/null)"
34 if [ -n "$dir" ] && [ -d "$dir" ]; then
35 cd "$dir" || return 1
36 fi
37}
38
39# wgo - go to a worktree by name
40wgo() {
41 if [ -z "$1" ]; then
42 echo "Usage: wgo <worktree-name>" >&2
43 return 1
44 fi
45 local dir
46 dir="$(git workty go "$1" 2>/dev/null)"
47 if [ -n "$dir" ] && [ -d "$dir" ]; then
48 cd "$dir" || return 1
49 else
50 echo "Worktree not found: $1" >&2
51 return 1
52 fi
53}
54
55"#);
56 }
57
58 if wrap_git {
59 output.push_str(r#"# git wrapper that auto-cds for workty commands
60git() {
61 if [ "$1" = "workty" ]; then
62 case "$2" in
63 go)
64 local dir
65 dir="$(command git workty go "${@:3}" 2>/dev/null)"
66 if [ -n "$dir" ] && [ -d "$dir" ]; then
67 cd "$dir"
68 else
69 command git "$@"
70 fi
71 ;;
72 pick)
73 local dir
74 dir="$(command git workty pick 2>/dev/null)"
75 if [ -n "$dir" ] && [ -d "$dir" ]; then
76 cd "$dir"
77 else
78 command git "$@"
79 fi
80 ;;
81 new)
82 local dir
83 dir="$(command git workty new "${@:3}" --print-path)"
84 if [ -n "$dir" ] && [ -d "$dir" ]; then
85 cd "$dir"
86 else
87 command git "$@"
88 fi
89 ;;
90 *)
91 command git "$@"
92 ;;
93 esac
94 else
95 command git "$@"
96 fi
97}
98
99"#);
100 }
101
102 output
103}
104
105fn generate_zsh(wrap_git: bool, no_cd: bool) -> String {
106 let mut output = String::new();
107
108 output.push_str("# git-workty shell integration for zsh\n\n");
109
110 if !no_cd {
111 output.push_str(r#"# wcd - fuzzy select and cd to a worktree
112wcd() {
113 local dir
114 dir="$(git workty pick 2>/dev/null)"
115 if [[ -n "$dir" ]] && [[ -d "$dir" ]]; then
116 cd "$dir"
117 fi
118}
119
120# wnew - create new worktree and cd into it
121wnew() {
122 if [[ -z "$1" ]]; then
123 echo "Usage: wnew <branch-name>" >&2
124 return 1
125 fi
126 local dir
127 dir="$(git workty new "$@" --print-path 2>/dev/null)"
128 if [[ -n "$dir" ]] && [[ -d "$dir" ]]; then
129 cd "$dir"
130 fi
131}
132
133# wgo - go to a worktree by name
134wgo() {
135 if [[ -z "$1" ]]; then
136 echo "Usage: wgo <worktree-name>" >&2
137 return 1
138 fi
139 local dir
140 dir="$(git workty go "$1" 2>/dev/null)"
141 if [[ -n "$dir" ]] && [[ -d "$dir" ]]; then
142 cd "$dir"
143 else
144 echo "Worktree not found: $1" >&2
145 return 1
146 fi
147}
148
149"#);
150 }
151
152 if wrap_git {
153 output.push_str(r#"# git wrapper that auto-cds for workty commands
154git() {
155 if [[ "$1" == "workty" ]]; then
156 case "$2" in
157 go)
158 local dir
159 dir="$(command git workty go "${@:3}" 2>/dev/null)"
160 if [[ -n "$dir" ]] && [[ -d "$dir" ]]; then
161 cd "$dir"
162 else
163 command git "$@"
164 fi
165 ;;
166 pick)
167 local dir
168 dir="$(command git workty pick 2>/dev/null)"
169 if [[ -n "$dir" ]] && [[ -d "$dir" ]]; then
170 cd "$dir"
171 else
172 command git "$@"
173 fi
174 ;;
175 new)
176 local dir
177 dir="$(command git workty new "${@:3}" --print-path)"
178 if [[ -n "$dir" ]] && [[ -d "$dir" ]]; then
179 cd "$dir"
180 else
181 command git "$@"
182 fi
183 ;;
184 *)
185 command git "$@"
186 ;;
187 esac
188 else
189 command git "$@"
190 fi
191}
192
193"#);
194 }
195
196 output
197}
198
199fn generate_fish(wrap_git: bool, no_cd: bool) -> String {
200 let mut output = String::new();
201
202 output.push_str("# git-workty shell integration for fish\n\n");
203
204 if !no_cd {
205 output.push_str(r#"# wcd - fuzzy select and cd to a worktree
206function wcd
207 set -l dir (git workty pick 2>/dev/null)
208 if test -n "$dir" -a -d "$dir"
209 cd "$dir"
210 end
211end
212
213# wnew - create new worktree and cd into it
214function wnew
215 if test (count $argv) -eq 0
216 echo "Usage: wnew <branch-name>" >&2
217 return 1
218 end
219 set -l dir (git workty new $argv --print-path 2>/dev/null)
220 if test -n "$dir" -a -d "$dir"
221 cd "$dir"
222 end
223end
224
225# wgo - go to a worktree by name
226function wgo
227 if test (count $argv) -eq 0
228 echo "Usage: wgo <worktree-name>" >&2
229 return 1
230 end
231 set -l dir (git workty go $argv[1] 2>/dev/null)
232 if test -n "$dir" -a -d "$dir"
233 cd "$dir"
234 else
235 echo "Worktree not found: $argv[1]" >&2
236 return 1
237 end
238end
239
240"#);
241 }
242
243 if wrap_git {
244 output.push_str(r#"# git wrapper that auto-cds for workty commands
245function git --wraps git
246 if test "$argv[1]" = "workty"
247 switch $argv[2]
248 case go
249 set -l dir (command git workty go $argv[3..] 2>/dev/null)
250 if test -n "$dir" -a -d "$dir"
251 cd "$dir"
252 else
253 command git $argv
254 end
255 case pick
256 set -l dir (command git workty pick 2>/dev/null)
257 if test -n "$dir" -a -d "$dir"
258 cd "$dir"
259 else
260 command git $argv
261 end
262 case new
263 set -l dir (command git workty new $argv[3..] --print-path)
264 if test -n "$dir" -a -d "$dir"
265 cd "$dir"
266 else
267 command git $argv
268 end
269 case '*'
270 command git $argv
271 end
272 else
273 command git $argv
274 end
275end
276
277"#);
278 }
279
280 output
281}
282
283fn generate_powershell(wrap_git: bool, no_cd: bool) -> String {
284 let mut output = String::new();
285
286 output.push_str("# git-workty shell integration for PowerShell\n\n");
287
288 if !no_cd {
289 output.push_str(r#"# wcd - fuzzy select and cd to a worktree
290function wcd {
291 $dir = git workty pick 2>$null
292 if ($dir -and (Test-Path $dir)) {
293 Set-Location $dir
294 }
295}
296
297# wnew - create new worktree and cd into it
298function wnew {
299 param([Parameter(Mandatory=$true)][string]$Name)
300 $dir = git workty new $Name --print-path 2>$null
301 if ($dir -and (Test-Path $dir)) {
302 Set-Location $dir
303 }
304}
305
306# wgo - go to a worktree by name
307function wgo {
308 param([Parameter(Mandatory=$true)][string]$Name)
309 $dir = git workty go $Name 2>$null
310 if ($dir -and (Test-Path $dir)) {
311 Set-Location $dir
312 } else {
313 Write-Error "Worktree not found: $Name"
314 }
315}
316
317"#);
318 }
319
320 if wrap_git {
321 output.push_str(r#"# Note: Git wrapper for PowerShell requires more complex setup.
322# Consider using the wcd, wnew, and wgo functions directly.
323
324"#);
325 }
326
327 output
328}
329
330#[allow(dead_code)]
331pub fn supported_shells() -> &'static [&'static str] {
332 &["bash", "zsh", "fish", "powershell"]
333}