whi 0.6.5

Stupid simple PATH management
Documentation
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
# whi shell integration for fish (v0.6.5)

# Absolute path to the whi binary is injected by `whi init`
set -gx __WHI_BIN "__WHI_BIN__"
set -g __WHI_CONFIG_PATH "$HOME/.whi/config.toml"
set -g __WHI_AUTO_FILE 0
set -g __WHI_AUTO_FILE_MTIME ""
set -g __WHI_STAT_STYLE ""
set -g __WHI_STAT_SKIP 0

function __whi_run
    $__WHI_BIN $argv
end

function __whi_detect_stat_style
    if test -n "$__WHI_STAT_STYLE"
        return
    end

    set -l probe $HOME
    if test -z "$probe"
        set probe .
    end

    command stat -c %Y $probe > /dev/null 2>&1
    if test $status -eq 0
        set -g __WHI_STAT_STYLE gnu
        return
    end

    command stat -f %m $probe > /dev/null 2>&1
    if test $status -eq 0
        set -g __WHI_STAT_STYLE bsd
        return
    end

    set -g __WHI_STAT_STYLE none
end

function __whi_stat_mtime
    set -l path $argv[1]

    if test -z "$path"
        return 0
    end

    if not test -e "$path"
        return 0
    end

    __whi_detect_stat_style

    switch $__WHI_STAT_STYLE
        case gnu
            set -l result (command stat -c %Y $path 2>/dev/null)
            if test $status -eq 0 -a -n "$result"
                echo $result
            end
        case bsd
            set -l result (command stat -f %m $path 2>/dev/null)
            if test $status -eq 0 -a -n "$result"
                echo $result
            end
    end

    return 0
end

function __whi_refresh_auto_config
    set -l current_mtime ""

    if test -n "$__WHI_CONFIG_PATH"
        set -l mtime (__whi_stat_mtime $__WHI_CONFIG_PATH)
        if test (count $mtime) -gt 0 -a -n "$mtime[1]"
            set current_mtime $mtime[1]
        end
    end

    if set -q __WHI_AUTO_CONFIG_LOADED
        if test "$current_mtime" = "$__WHI_AUTO_FILE_MTIME"
            return 0
        end
    end

    set -l output (__whi_run __should_auto_activate 2>/dev/null)
    set -l lines (string split '\n' -- $output)
    set -l auto_flag 0
    set -l deactivate_flag 0

    for line in $lines
        if string match -rq '^file=1' -- $line
            set auto_flag 1
        else if string match -rq '^deactivate=1' -- $line
            set deactivate_flag 1
        end
    end

    set -g __WHI_AUTO_FILE $auto_flag
    set -g __WHI_AUTO_DEACTIVATE $deactivate_flag

    if test -z "$current_mtime"
        set -g __WHI_AUTO_FILE 0
    end

    set -g __WHI_AUTO_FILE_MTIME $current_mtime
    set -g __WHI_AUTO_CONFIG_LOADED 1
    set -g __WHI_STAT_SKIP 0
end

function __whi_install_fish_deactivate_guard
    if test "$_WHI_PYENV_GUARD_INSTALLED" = 1
        return
    end

    if not functions -q deactivate
        return
    end

    if functions -q __whi_original_deactivate
        functions -e __whi_original_deactivate
    end
    functions -c deactivate __whi_original_deactivate

    function deactivate --argument-names argv
        if set -q WHI_ALLOW_DEACTIVATE
            __whi_original_deactivate $argv
        else
            printf '%s\n' "environment managed by whi, please use 'whi exit' to leave" >&2
            return 1
        end
    end

    set -g _WHI_PYENV_GUARD_INSTALLED 1
end

function __whi_remove_fish_deactivate_guard
    if test "$_WHI_PYENV_GUARD_INSTALLED" != 1
        return
    end

    if functions -q deactivate
        functions -e deactivate
    end
    if functions -q __whi_original_deactivate
        functions -e __whi_original_deactivate
    end
    set -e _WHI_PYENV_GUARD_INSTALLED
end

function __whi_apply_transition
    set -l output (__whi_run $argv)
    set -l exit_code $status
    if test $exit_code -ne 0
        return $exit_code
    end

    set -l install_guard 0
    set -l remove_guard 0

    for line in $output
        set -l parts (string split \t -- $line)
        switch $parts[1]
            case DEACTIVATE_PYENV
                # Deactivate Python venv if function exists
                if type -q deactivate
                    set -l prev_allow ""
                    set -l had_prev 0
                    if set -q WHI_ALLOW_DEACTIVATE
                        set prev_allow $WHI_ALLOW_DEACTIVATE
                        set had_prev 1
                    end
                    set -g WHI_ALLOW_DEACTIVATE 1
                    deactivate
                    if test $had_prev -eq 1
                        set -g WHI_ALLOW_DEACTIVATE $prev_allow
                    else
                        set -e WHI_ALLOW_DEACTIVATE
                    end
                end
            case PATH
                if test (count $parts) -ge 2
                    set -gx PATH (string split : -- $parts[2])
                end
            case SET
                if test (count $parts) -ge 3
                    set -gx $parts[2] $parts[3]
                    if test $parts[2] = "WHI_PYENV_MANAGED"
                        set install_guard 1
                    end
                end
            case UNSET
                if test (count $parts) -ge 2
                    set -e $parts[2]
                    if test $parts[2] = "WHI_PYENV_MANAGED"
                        set remove_guard 1
                    end
                end
            case RUN
                if test (count $parts) -ge 2
                    set -l prev_allow ""
                    set -l had_prev 0
                    if set -q WHI_ALLOW_DEACTIVATE
                        set prev_allow $WHI_ALLOW_DEACTIVATE
                        set had_prev 1
                    end
                    set -g WHI_ALLOW_DEACTIVATE 1
                    eval $parts[2]
                    if test $had_prev -eq 1
                        set -g WHI_ALLOW_DEACTIVATE $prev_allow
                    else
                        set -e WHI_ALLOW_DEACTIVATE
                    end
                end
            case SOURCE
                if test (count $parts) -ge 2 -a -f "$parts[2]"
                    # Clean up _old_fish_prompt to allow venv activate.fish to work
                    if functions -q _old_fish_prompt
                        functions -e _old_fish_prompt
                    end
                    source "$parts[2]"
                end
        end
    end

    if test $install_guard -eq 1
        __whi_install_fish_deactivate_guard
    end

    if test $remove_guard -eq 1
        __whi_remove_fish_deactivate_guard
    end

    __whi_update_prompt

    return 0
end

# Load saved PATH first (if it exists) using whi
# This restores your PATH from the previous session
if test -f ~/.whi/saved_path_fish
    set -l new_path (__whi_run __load_saved_path fish)
    if test -n "$new_path"
        set -gx PATH (string split : $new_path)
    end
end

function __whi_apply
    set -l subcmd $argv[1]
    set -l rest $argv[2..-1]
    set -l new_path (__whi_run __$subcmd $rest)
    set -l exit_code $status
    if test $exit_code -eq 0
        set -gx PATH (string split : $new_path)
    else
        return $exit_code
    end
end

function __whi_handle_move --argument-names display
    set -l args $argv[2..-1]
    if test (count $args) -ge 1
        if contains -- $args[1] help --help -h
            echo "Usage: $display FROM TO"
            echo "  Move PATH entry from index FROM to index TO"
            return 0
        end
    end

    if test (count $args) -ne 2
        echo "Usage: $display FROM TO" >&2
        echo "  Move PATH entry from index FROM to index TO" >&2
        return 2
    end

    __whi_apply move $args
    return $status
end

function __whi_handle_switch --argument-names display
    set -l args $argv[2..-1]
    if test (count $args) -ge 1
        if contains -- $args[1] help --help -h
            echo "Usage: $display IDX1 IDX2"
            echo "  Swap PATH entries at indices IDX1 and IDX2"
            return 0
        end
    end

    if test (count $args) -ne 2
        echo "Usage: $display IDX1 IDX2" >&2
        echo "  Swap PATH entries at indices IDX1 and IDX2" >&2
        return 2
    end

    __whi_apply switch $args
    return $status
end

function __whi_handle_clean --argument-names display
    set -l args $argv[2..-1]
    if test (count $args) -ge 1
        if contains -- $args[1] help --help -h
            echo "Usage: $display"
            echo "  Remove duplicate entries from PATH"
            return 0
        end
    end

    if test (count $args) -ne 0
        echo "Usage: $display" >&2
        echo "  Remove duplicate entries from PATH" >&2
        return 2
    end

    __whi_apply clean
    return $status
end

function __whi_handle_delete --argument-names display
    set -l args $argv[2..-1]
    if test (count $args) -ge 1
        if contains -- $args[1] help --help -h
            echo "Usage: $display TARGET [TARGET...]"
            echo "  TARGET can be index, path, or fuzzy pattern"
            echo "  Fuzzy patterns delete ALL matching entries"
            return 0
        end
    end

    if test (count $args) -lt 1
        echo "Usage: $display TARGET [TARGET...]" >&2
        echo "  TARGET can be index, path, or fuzzy pattern" >&2
        return 2
    end

    __whi_apply delete $args
    return $status
end

function __whi_handle_add --argument-names display
    set -l args $argv[2..-1]
    if test (count $args) -ge 1
        if contains -- $args[1] help --help -h
            echo "Usage: $display PATH..."
            echo "  Add one or more paths to PATH (prepends by default)"
            return 0
        end
    end

    if test (count $args) -lt 1
        echo "Usage: $display PATH..." >&2
        return 2
    end

    __whi_apply add $args
    return $status
end

function __whi_handle_prefer --argument-names display
    set -l args $argv[2..-1]
    if test (count $args) -ge 1
        if contains -- $args[1] help --help -h
            echo "Usage: $display [NAME] TARGET [PATTERN...]"
            echo "  Add path to PATH or prefer executable at target"
            echo "  TARGET can be index, path, or fuzzy pattern"
            echo "Examples:"
            printf '  %s ~/.cargo/bin           # Add path to PATH (if not present)\n' $display
            printf '  %s cargo 3                # Use cargo from PATH index 3\n' $display
            printf '  %s cargo ~/.cargo/bin     # Add/prefer ~/.cargo/bin for cargo\n' $display
            printf '  %s bat github release     # Use bat from path matching pattern\n' $display
            return 0
        end
    end

    if test (count $args) -lt 1
        echo "Usage: $display [NAME] TARGET [PATTERN...]" >&2
        echo "  TARGET can be index, path, or fuzzy pattern" >&2
        return 2
    end

    if test (count $args) -eq 1 -a (string match -qr '[/~.]' -- $args[1])
        __whi_apply prefer $args
    else
        set -l name $args[1]
        set -l rest $args[2..-1]
        __whi_apply prefer $name $rest
    end
    return $status
end

function __whi_handle_redo --argument-names display
    set -l args $argv[2..-1]
    if test (count $args) -ge 1
        if contains -- $args[1] help --help -h
            echo "Usage: $display [COUNT]"
            echo "  Redo next COUNT PATH operations (default: 1)"
            return 0
        end
    end

    if test (count $args) -eq 0
        __whi_apply redo 1
    else if test (count $args) -eq 1
        __whi_apply redo $args[1]
    else
        echo "Usage: $display [COUNT]" >&2
        echo "  Redo next COUNT PATH operations (default: 1)" >&2
        return 2
    end
    return $status
end

function __whi_handle_undo --argument-names display
    set -l args $argv[2..-1]
    if test (count $args) -ge 1
        if contains -- $args[1] help --help -h
            echo "Usage: $display [COUNT]"
            echo "  Undo last COUNT PATH operations (default: 1)"
            return 0
        end
    end

    if test (count $args) -eq 0
        __whi_apply undo 1
    else if test (count $args) -eq 1
        __whi_apply undo $args[1]
    else
        echo "Usage: $display [COUNT]" >&2
        echo "  Undo last COUNT PATH operations (default: 1)" >&2
        return 2
    end
    return $status
end

set -g __WHI_CMD_NAMES \
    move switch clean delete add prefer redo undo \
    whim whis whic whid whiad whir whiu whip
set -g __WHI_CMD_HANDLERS \
    __whi_handle_move \
    __whi_handle_switch \
    __whi_handle_clean \
    __whi_handle_delete \
    __whi_handle_add \
    __whi_handle_prefer \
    __whi_handle_redo \
    __whi_handle_undo \
    __whi_handle_move \
    __whi_handle_switch \
    __whi_handle_clean \
    __whi_handle_delete \
    __whi_handle_add \
    __whi_handle_redo \
    __whi_handle_undo \
    __whi_handle_prefer

function __whi_lookup_handler --argument-names cmd
    for idx in (seq (count $__WHI_CMD_NAMES))
        if test $__WHI_CMD_NAMES[$idx] = $cmd
            echo $__WHI_CMD_HANDLERS[$idx]
            return 0
        end
    end
    return 1
end

function __whi_dispatch --argument-names cmd
    set -l handler (__whi_lookup_handler $cmd)
    if test -z "$handler"
        return 1
    end
    set -l rest $argv[2..-1]
    $handler "whi $cmd" $rest
    return $status
end

function __whi_run_shorthand --argument-names name
    set -l handler (__whi_lookup_handler $name)
    if test -z "$handler"
        return 1
    end
    set -l rest $argv[2..-1]
    $handler $name $rest
    return $status
end

function __whi_venv_source
    set -l dir $argv[1]
    test -z "$dir"; and set dir "."

    __whi_apply_transition __venv_source "$dir"
end

function __whi_venv_exit_fn
    __whi_apply_transition __venv_exit
end


function __whi_cd_hook --on-variable PWD
    if not set -q __WHI_AUTO_CONFIG_LOADED
        __whi_refresh_auto_config
    end

    set -l check_config 1
    if set -q __WHI_AUTO_FILE
        if test $__WHI_AUTO_FILE -eq 0 -a -n "$__WHI_AUTO_FILE_MTIME"
            set -l skip $__WHI_STAT_SKIP
            if test $skip -lt 4
                set check_config 0
                set -g __WHI_STAT_SKIP (math "$skip + 1")
            else
                set -g __WHI_STAT_SKIP 0
            end
        else
            set -g __WHI_STAT_SKIP 0
        end
    end

    if test $check_config -eq 1 -a -n "$__WHI_CONFIG_PATH"
        set -l current_mtime (__whi_stat_mtime $__WHI_CONFIG_PATH)
        if test (count $current_mtime) -gt 0 -a -n "$current_mtime[1]"
            set -l current_val $current_mtime[1]
            if test "$current_val" != "$__WHI_AUTO_FILE_MTIME"
                __whi_refresh_auto_config
            end
        else if test -n "$__WHI_AUTO_FILE_MTIME"
            set -g __WHI_AUTO_FILE 0
            set -g __WHI_AUTO_FILE_MTIME ""
        end
        set -g __WHI_STAT_SKIP 0
    end

    # Check if we should auto-activate or auto-deactivate
    set -l has_file 0
    test -f "$PWD/whifile"; and set has_file 1

    # If already in a whi-managed venv, check if we left that directory
    if set -q WHI_VENV_DIR
        if test -n "$WHI_VENV_DIR" -a "$__WHI_AUTO_DEACTIVATE" -eq 1
            set -l root (string trim --right --chars='/' -- "$WHI_VENV_DIR")
            if not string match -q "$root" -- "$PWD"
                if not string match -q "$root/*" -- "$PWD"
                    # Left whi venv directory tree, deactivate
                    __whi_venv_exit_fn 2>/dev/null
                end
            end
        end
    end

    # Auto-activate if configured and not already in venv
    if test -z "$VIRTUAL_ENV_PROMPT" -a $__WHI_AUTO_FILE -eq 1 -a $has_file -eq 1
        __whi_venv_source "$PWD"
    end
end

function whim
    __whi_run_shorthand whim $argv
end

function whis
    __whi_run_shorthand whis $argv
end

function whip
    __whi_run_shorthand whip $argv
end

function whic
    __whi_run_shorthand whic $argv
end

function whid
    __whi_run_shorthand whid $argv
end

function whia
    __whi_run --all $argv
end

function whiad
    __whi_run_shorthand whiad $argv
end

function whir
    __whi_run_shorthand whir $argv
end

function whiu
    __whi_run_shorthand whiu $argv
end

function whiv
    if test (count $argv) -ge 1; and contains -- $argv[1] help --help -h
        echo "Usage: whiv [-f|--full] [NAME]"
        echo "  Query environment variables"
        echo ""
        echo "Options:"
        echo "  -f, --full    List all environment variables"
        echo ""
        echo "Examples:"
        echo "  whiv PATH         # Show PATH variable"
        echo "  whiv cargo        # Fuzzy search for variables matching 'cargo'"
        echo "  whiv -f           # List all variables"
        return 0
    end
    __whi_run var $argv
end

function whish
    __whi_run shorthands $argv
end

function whil
    if test (count $argv) -ge 1; and contains -- $argv[1] help --help -h
        echo "Usage: whil NAME"
        echo "  Load saved profile NAME"
        return 0
    end
    if test (count $argv) -ne 1
        echo "Usage: whil NAME" >&2
        return 2
    end
    __whi_apply_transition __load $argv[1]
end

function whi
    if test (count $argv) -gt 0
        set -l cmd $argv[1]
        switch $cmd
            case reset
                if test (count $argv) -ge 2; and contains -- $argv[2] help --help -h
                    echo "Usage: whi reset"
                    echo "  Reset PATH to initial session state"
                    return 0
                end
                if test (count $argv) -ne 1
                    echo "Usage: whi reset" >&2
                    return 2
                end
                __whi_apply reset
                return $status
            case load
                if test (count $argv) -ge 2; and contains -- $argv[2] help --help -h
                    echo "Usage: whi load NAME"
                    echo "  Load saved profile NAME"
                    return 0
                end
                if test (count $argv) -ne 2
                    echo "Usage: whi load NAME" >&2
                    return 2
                end
                __whi_apply_transition __load $argv[2]
                return $status
            case var
                __whi_run var $argv[2..]
                return $status
            case shorthands
                __whi_run shorthands $argv[2..]
                return $status
            case source
                if test (count $argv) -ge 2; and contains -- $argv[2] help --help -h
                    echo "Usage: whi source"
                    echo "  Activate venv from whifile in current directory"
                    return 0
                end
                if test (count $argv) -ne 1
                    echo "Usage: whi source" >&2
                    return 2
                end
                __whi_venv_source "$PWD"
                return $status
            case exit
                if test (count $argv) -ge 2; and contains -- $argv[2] help --help -h
                    echo "Usage: whi exit"
                    echo "  Exit active venv and restore previous PATH"
                    return 0
                end
                if test (count $argv) -ne 1
                    echo "Usage: whi exit" >&2
                    return 2
                end
                __whi_venv_exit_fn
                return $status
            case '*'
                __whi_dispatch $argv
                set -l dispatch_status $status
                if test $dispatch_status -ne 1
                    return $dispatch_status
                end
        end
    end

    __whi_run $argv
end

# Prompt integration matches virtualenv by overriding fish_prompt while a whi venv is active.

function __whi_prompt
    if set -q VIRTUAL_ENV_DISABLE_PROMPT
        if test "$VIRTUAL_ENV_DISABLE_PROMPT" = "1"
            return
        end
    end

    if set -q _OLD_FISH_PROMPT_OVERRIDE
        # Virtualenv activate.fish already wrapped the prompt, so avoid double prefixes.
        return
    end

    set -l prompt ''
    if set -q VIRTUAL_ENV_PROMPT
        set prompt "$VIRTUAL_ENV_PROMPT"
    else if set -q VIRTUAL_ENV
        set prompt (basename -- "$VIRTUAL_ENV")
    end

    if test -n "$prompt"
        printf '(%s) ' "$prompt"
    end
end

function __whi_install_prompt
    if set -q __WHI_FISH_PROMPT_OVERRIDE
        return
    end

    if set -q VIRTUAL_ENV_DISABLE_PROMPT
        if test "$VIRTUAL_ENV_DISABLE_PROMPT" = "1"
            return
        end
    end

    if functions -q _old_fish_prompt
        if not functions -q __whi_saved_old_fish_prompt
            functions -c _old_fish_prompt __whi_saved_old_fish_prompt
        end
    end

    if functions -q fish_prompt
        if not functions -q __whi_original_fish_prompt
            functions -c fish_prompt __whi_original_fish_prompt
        end
    end

    function fish_prompt
        set -l last_status $status
        set -l prefix (__whi_prompt)
        set -l prompt ''

        if functions -q __whi_original_fish_prompt
            if not functions -q _old_fish_prompt
                if functions -q __whi_saved_old_fish_prompt
                    functions -c __whi_saved_old_fish_prompt _old_fish_prompt
                end
            end
            set prompt (__whi_original_fish_prompt 2>/dev/null)
            if test $status -ne 0
                set prompt ''
            end
        end

        if test -n "$prefix"
            printf '%s' "$prefix"
        end

        if test -n "$prompt"
            string join -- \n $prompt
        end

        return $last_status
    end

    set -g __WHI_FISH_PROMPT_OVERRIDE 1
end

function __whi_remove_prompt
    if not set -q __WHI_FISH_PROMPT_OVERRIDE
        return
    end

    if functions -q fish_prompt
        functions -e fish_prompt
    end

    if functions -q __whi_original_fish_prompt
        functions -c __whi_original_fish_prompt fish_prompt
    end

    set -e __WHI_FISH_PROMPT_OVERRIDE
end

function __whi_update_prompt
    if set -q VIRTUAL_ENV_DISABLE_PROMPT
        if test "$VIRTUAL_ENV_DISABLE_PROMPT" = "1"
            __whi_remove_prompt
            return
        end
    end

    if set -q VIRTUAL_ENV_PROMPT
        if test -n "$VIRTUAL_ENV_PROMPT"
            __whi_install_prompt
            return
        end
    end

    if set -q VIRTUAL_ENV
        __whi_install_prompt
        return
    end

    __whi_remove_prompt
end


if not set -q WHI_SHELL_INITIALIZED
    set -gx WHI_SHELL_INITIALIZED 1
    set -gx WHI_SESSION_PID %self
    __whi_run __init "$WHI_SESSION_PID"
end

# Trigger auto-activation for the current directory (if configured)
if functions -q __whi_cd_hook
    __whi_cd_hook >/dev/null
end

__whi_update_prompt

# IMPORTANT: Add this to the END of your fish config (~/.config/fish/config.fish):
#   whi init fish | source
# This must be at the END so whi captures your final PATH after all modifications.
# Also remove any old "# whi: Load saved PATH" sections from your config -
# saved PATH loading is now included at the top of this integration script.
# Or run in the current shell:
#   whi init fish | source

# Prompt integration: whi automatically adds "[name]" or "[name:locked]" to the right prompt.
# Customize by editing __whi_prompt or overriding fish_right_prompt after sourcing if
# you prefer a different placement.