summary refs log tree commit diff stats
path: root/user/settings/fish/functions/tmp.fish
diff options
context:
space:
mode:
Diffstat (limited to 'user/settings/fish/functions/tmp.fish')
-rw-r--r--user/settings/fish/functions/tmp.fish30
1 files changed, 30 insertions, 0 deletions
diff --git a/user/settings/fish/functions/tmp.fish b/user/settings/fish/functions/tmp.fish
new file mode 100644
index 00000000..af87c325
--- /dev/null
+++ b/user/settings/fish/functions/tmp.fish
@@ -0,0 +1,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