summary refs log tree commit diff stats
path: root/user/settings/fish/functions/tmp.fish
blob: af87c325170af525dfb6708e5b80d2b69cd273be (plain)
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
# source: https://github.com/ansemjo/dotfiles/blob/debfb84d2574985daeac0d85a66002081b59a569/fish/functions/tmpdir.fish
function tmp --description "Create and switch into an emphemeral directory"

    # create a tmpdir and cd into it
    set -l tmpdir (mktemp --tmpdir -d tmpdir-XXXXXX)
    cd $tmpdir; and echo >&2 \
        (set_color yellow)"tmpdir:" $tmpdir "will be removed on exit" \
        (set_color normal)

    # spawn a new shell, store the exit status and return to previous dir
    fish $argv
    set -l ret $status
    cd $dirprev[-1]

    # after exit, check if there are mounts inside tmpdir
    if test -e /etc/mtab && awk '{ print $2 }' /etc/mtab | grep $tmpdir
        echo >&2 \
            (set_color red)"refusing to purge $tmpdir due to mounts!" \
            (set_color normal)
    else
        # if clear, purge directory
        echo >&2 \
            (set_color red)"purge $tmpdir ..." \
            (set_color normal)
        rm -rf $tmpdir
    end

    # return subshell exit status
    return $ret
end