Skip to main content

git_workty/
shell.rs

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