whi 0.5.1

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
# whi shell integration for fish (v0.5.1)

# 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 ""

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 output (__whi_run __should_auto_activate 2>/dev/null)
    set -l first (string split '\n' -- $output)[1]
    set -l auto_flag 0
    if string match -rq '^file=1' -- $first
        set auto_flag 1
    end
    set -g __WHI_AUTO_FILE $auto_flag

    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 -g __WHI_AUTO_FILE_MTIME $mtime[1]
        else
            set -g __WHI_AUTO_FILE_MTIME ""
        end
    end
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

    for line in $output
        set -l parts (string split \t -- $line)
        switch $parts[1]
            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]
                end
            case UNSET
                if test (count $parts) -ge 2
                    set -e $parts[2]
                end
        end
    end

    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_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_prompt
    if test -n "$WHI_VENV_NAME"
        echo "[$WHI_VENV_NAME] "
    end
end

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

    if test -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
    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 venv, check if we left that directory
    if test -n "$WHI_VENV_DIR"
        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 venv directory tree, deactivate
                __whi_venv_exit_fn 2>/dev/null
            end
        end
    end

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

__whi_refresh_auto_config

function whim
    if test (count $argv) -ge 1; and contains -- $argv[1] help --help -h
        echo "Usage: whim FROM TO"
        echo "  Move PATH entry from index FROM to index TO"
        return 0
    end
    if test (count $argv) -ne 2
        echo "Usage: whim FROM TO" >&2
        return 2
    end

    __whi_apply move $argv
end

function whis
    if test (count $argv) -ge 1; and contains -- $argv[1] help --help -h
        echo "Usage: whis IDX1 IDX2"
        echo "  Swap PATH entries at indices IDX1 and IDX2"
        return 0
    end
    if test (count $argv) -ne 2
        echo "Usage: whis IDX1 IDX2" >&2
        return 2
    end

    __whi_apply switch $argv
end

function whip
    if test (count $argv) -ge 1; and contains -- $argv[1] help --help -h
        echo "Usage: whip [NAME] TARGET [PATTERN...]"
        echo "  Add path to PATH or prefer executable at target"
        return 0
    end
    if test (count $argv) -lt 1
        echo "Usage: whip [NAME] TARGET [PATTERN...]" >&2
        return 2
    end

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

function whic
    if test (count $argv) -ge 1; and contains -- $argv[1] help --help -h
        echo "Usage: whic"
        echo "  Remove duplicate entries from PATH"
        return 0
    end
    if test (count $argv) -ne 0
        echo "Usage: whic" >&2
        return 2
    end
    __whi_apply clean
end

function whid
    if test (count $argv) -ge 1; and contains -- $argv[1] help --help -h
        echo "Usage: whid TARGET [TARGET...]"
        echo "  TARGET can be index, path, or fuzzy pattern"
        return 0
    end
    if test (count $argv) -lt 1
        echo "Usage: whid TARGET [TARGET...]" >&2
        return 2
    end

    __whi_apply delete $argv
end

function whia
    __whi_run --all $argv
end

function whir
    if test (count $argv) -ge 1; and contains -- $argv[1] help --help -h
        echo "Usage: whir [COUNT]"
        echo "  Redo next COUNT PATH operations (default: 1)"
        return 0
    end
    if test (count $argv) -eq 0
        __whi_apply redo 1
    else if test (count $argv) -eq 1
        __whi_apply redo $argv[1]
    else
        echo "Usage: whir [COUNT]" >&2
        return 2
    end
end

function whiu
    if test (count $argv) -ge 1; and contains -- $argv[1] help --help -h
        echo "Usage: whiu [COUNT]"
        echo "  Undo last COUNT PATH operations (default: 1)"
        return 0
    end
    if test (count $argv) -eq 0
        __whi_apply undo 1
    else if test (count $argv) -eq 1
        __whi_apply undo $argv[1]
    else
        echo "Usage: whiu [COUNT]" >&2
        return 2
    end
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 load $argv[1]
end

function whi
    if test (count $argv) -gt 0
        switch $argv[1]
            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 undo
                if test (count $argv) -ge 2; and contains -- $argv[2] help --help -h
                    echo "Usage: whi undo [COUNT]"
                    echo "  Undo last COUNT PATH operations (default: 1)"
                    return 0
                end
                if test (count $argv) -eq 1
                    __whi_apply undo 1
                else if test (count $argv) -eq 2
                    __whi_apply undo $argv[2]
                else
                    echo "Usage: whi undo [COUNT]" >&2
                    return 2
                end
                return $status
            case redo
                if test (count $argv) -ge 2; and contains -- $argv[2] help --help -h
                    echo "Usage: whi redo [COUNT]"
                    echo "  Redo next COUNT PATH operations (default: 1)"
                    return 0
                end
                if test (count $argv) -eq 1
                    __whi_apply redo 1
                else if test (count $argv) -eq 2
                    __whi_apply redo $argv[2]
                else
                    echo "Usage: whi redo [COUNT]" >&2
                    return 2
                end
                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 load $argv[2]
                return $status
            case prefer
                if test (count $argv) -ge 2; and contains -- $argv[2] help --help -h
                    echo "Usage: whi prefer [NAME] TARGET [PATTERN...]"
                    echo "  Add path to PATH or prefer executable at target"
                    return 0
                end
                if test (count $argv) -lt 2
                    echo "Usage: whi prefer [NAME] TARGET [PATTERN...]" >&2
                    return 2
                end
                __whi_apply prefer $argv[2..-1]
                return $status
            case move
                if test (count $argv) -ge 2; and contains -- $argv[2] help --help -h
                    echo "Usage: whi move FROM TO"
                    echo "  Move PATH entry from index FROM to index TO"
                    return 0
                end
                if test (count $argv) -ne 3
                    echo "Usage: whi move FROM TO" >&2
                    return 2
                end
                __whi_apply move $argv[2..-1]
                return $status
            case switch
                if test (count $argv) -ge 2; and contains -- $argv[2] help --help -h
                    echo "Usage: whi switch IDX1 IDX2"
                    echo "  Swap PATH entries at indices IDX1 and IDX2"
                    return 0
                end
                if test (count $argv) -ne 3
                    echo "Usage: whi switch IDX1 IDX2" >&2
                    return 2
                end
                __whi_apply switch $argv[2..-1]
                return $status
            case clean
                if test (count $argv) -ge 2; and contains -- $argv[2] help --help -h
                    echo "Usage: whi clean"
                    echo "  Remove duplicate entries from PATH"
                    return 0
                end
                if test (count $argv) -ne 1
                    echo "Usage: whi clean" >&2
                    return 2
                end
                __whi_apply clean
                return $status
            case delete
                if test (count $argv) -ge 2; and contains -- $argv[2] help --help -h
                    echo "Usage: whi delete TARGET [TARGET...]"
                    echo "  TARGET can be index, path, or fuzzy pattern"
                    return 0
                end
                if test (count $argv) -lt 2
                    echo "Usage: whi delete TARGET [TARGET...]" >&2
                    return 2
                end
                __whi_apply delete $argv[2..-1]
                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
        end
    end

    __whi_run $argv
end

if not set -q __whi_rprompt_installed
    set -g __whi_rprompt_installed 1

    if not functions -q __whi_original_fish_right_prompt
        if functions -q fish_right_prompt
            functions -c fish_right_prompt __whi_original_fish_right_prompt
        end
    end

    function fish_right_prompt
        set -l prefix (__whi_prompt)
        if functions -q __whi_original_fish_right_prompt
            __whi_original_fish_right_prompt
        end
        if test -n "$prefix"
            printf '%s' "$prefix"
        end
    end
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

# 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.