summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAlan Pearce2025-02-12 01:19:33 +0100
committerAlan Pearce2025-02-12 01:19:33 +0100
commit0c5475091ffe03d0b5dd1623d6a5873ada5cd652 (patch)
tree629fe74cad94516ab24891e70d87f20048247f10
parentf4e50d0d86c0e1b73894b79b9a87ba9b89c81fbe (diff)
downloadnixfiles-0c5475091ffe03d0b5dd1623d6a5873ada5cd652.tar.lz
nixfiles-0c5475091ffe03d0b5dd1623d6a5873ada5cd652.tar.zst
nixfiles-0c5475091ffe03d0b5dd1623d6a5873ada5cd652.zip
fish: add tmp function to cd to an ephemeral dir
-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