tenseleyflow/gump / 2e9132c

Browse files

feat: add generate_fortsh() for fortsh shell integration

Authored by espadonne
SHA
2e9132c0c591c245ba6ae1236516655efe0c6e44
Parents
1664f3e
Tree
c7bbd7a

1 changed file

StatusFile+-
M src/cmd/init.rs 116 0
src/cmd/init.rsmodified
@@ -8,6 +8,7 @@ pub fn run(shell: Shell, cmd: String, hook: Hook, no_cmd: bool) -> Result<()> {
88
         Shell::Bash => generate_bash(&cmd, &hook, no_cmd),
99
         Shell::Zsh => generate_zsh(&cmd, &hook, no_cmd),
1010
         Shell::Fish => generate_fish(&cmd, &hook, no_cmd),
11
+        Shell::Fortsh => generate_fortsh(&cmd, &hook, no_cmd),
1112
     };
1213
 
1314
     println!("{}", output);
@@ -375,3 +376,118 @@ bind \n __gump_execute
375376
 
376377
     output
377378
 }
379
+
380
+fn generate_fortsh(cmd: &str, hook: &Hook, no_cmd: bool) -> String {
381
+    let mut output = String::new();
382
+
383
+    // Hook function
384
+    output.push_str(r#"
385
+# gump hook - called after directory changes
386
+__gump_hook() {
387
+    command gump add -- "$PWD"
388
+}
389
+"#);
390
+
391
+    // Hook helper for pwd mode (defined before trap, used later)
392
+    if matches!(hook, Hook::Pwd) {
393
+        output.push_str(r#"
394
+# Track directory changes
395
+__gump_oldpwd="$PWD"
396
+__gump_pwd_hook() {
397
+    if [[ "$PWD" != "$__gump_oldpwd" ]]; then
398
+        __gump_oldpwd="$PWD"
399
+        __gump_hook
400
+    fi
401
+}
402
+"#);
403
+    }
404
+
405
+    // Command aliases (unless --no-cmd)
406
+    if !no_cmd {
407
+        output.push_str(&format!(
408
+            r#"
409
+# Jump function
410
+{cmd}() {{
411
+    if [[ $# -eq 0 ]]; then
412
+        command cd ~ && __gump_hook
413
+    elif [[ $# -eq 1 && "$1" == "-" ]]; then
414
+        command cd - && __gump_hook
415
+    elif [[ $# -eq 1 && -d "$1" ]]; then
416
+        command cd -- "$1" && __gump_hook
417
+    else
418
+        local result
419
+        result=$(command gump query --cwd -- "$@" 2>/dev/null)
420
+        if [[ -z "$result" ]]; then
421
+            result=$(command gump query -- "$@" 2>/dev/null)
422
+        fi
423
+        if [[ -n "$result" ]]; then
424
+            command cd -- "$result" && __gump_hook
425
+        else
426
+            echo "gump: no match found" >&2
427
+            return 1
428
+        fi
429
+    fi
430
+}}
431
+
432
+# Interactive mode with fzf
433
+{cmd}i() {{
434
+    local result
435
+    result=$(command gump query --all -- "$@" | fzf --height=40% --reverse)
436
+    if [[ -n "$result" ]]; then
437
+        command cd -- "$result" && __gump_hook
438
+    fi
439
+}}
440
+"#,
441
+            cmd = cmd
442
+        ));
443
+    }
444
+
445
+    // command_not_found_handle for no-prefix jumping
446
+    output.push_str(r#"
447
+# No-prefix directory jumping
448
+command_not_found_handle() {
449
+    # Check if it's a local directory first (exact match)
450
+    if [[ -d "$1" ]]; then
451
+        command cd -- "$1" && __gump_hook
452
+        return 0
453
+    fi
454
+
455
+    # Fuzzy match against current directory contents
456
+    local result
457
+    result=$(command gump query --cwd -- "$@" 2>/dev/null)
458
+    if [[ -n "$result" ]]; then
459
+        command cd -- "$result" && __gump_hook
460
+        return 0
461
+    fi
462
+
463
+    # Query gump database
464
+    result=$(command gump query -- "$@" 2>/dev/null)
465
+    if [[ -n "$result" ]]; then
466
+        command cd -- "$result" && __gump_hook
467
+        return 0
468
+    fi
469
+
470
+    # Fallback to default "command not found" behavior
471
+    echo "fortsh: $1: command not found" >&2
472
+    return 127
473
+}
474
+"#);
475
+
476
+    // Set trap LAST to avoid firing during function definitions above
477
+    match hook {
478
+        Hook::Prompt => {
479
+            output.push_str(r#"
480
+# Update on every prompt (via DEBUG trap)
481
+trap '__gump_hook' DEBUG
482
+"#);
483
+        }
484
+        Hook::Pwd => {
485
+            output.push_str(r#"
486
+# Update when directory changes (via DEBUG trap)
487
+trap '__gump_pwd_hook' DEBUG
488
+"#);
489
+        }
490
+    }
491
+
492
+    output
493
+}