aboutsummaryrefslogtreecommitdiffhomepage
path: root/init/functions/psub.fish
diff options
context:
space:
mode:
authorGravatar axel <axel@liljencrantz.se>2006-02-08 19:20:05 +1000
committerGravatar axel <axel@liljencrantz.se>2006-02-08 19:20:05 +1000
commit73a9c8bcb8d3d8db967fcc5cc9dd5e8db645400f (patch)
tree13862f394dca16b0c9bae427f86fbbecdfc9ac03 /init/functions/psub.fish
parent47b652c76e240e16875734f7ca251bab97ae6309 (diff)
Autoloaded functions
darcs-hash:20060208092005-ac50b-8e784f79a4e158c8c15b553fad85002dccc7bd03.gz
Diffstat (limited to 'init/functions/psub.fish')
-rw-r--r--init/functions/psub.fish54
1 files changed, 54 insertions, 0 deletions
diff --git a/init/functions/psub.fish b/init/functions/psub.fish
new file mode 100644
index 00000000..974833f2
--- /dev/null
+++ b/init/functions/psub.fish
@@ -0,0 +1,54 @@
+
+
+function psub -d "Read from stdin into a file and output the filename. Remove the file when the command that calles psub exits."
+
+ set -l filename
+ set -l funcname
+
+ if count $argv >/dev/null
+ switch $argv[1]
+ case '-h*' --h --he --hel --help
+
+ help psub
+ return 0
+
+ case '*'
+ printf (_ "%s: Unknown argument '%s'\n") psub $argv[1]
+ return 1
+ end
+ end
+
+ if not status --is-command-substitution
+ echo psub: Not inside of command substitution >&2
+ return
+ end
+
+ # Find unique file name for writing output to
+ while true
+ set filename /tmp/.psub.(echo %self).(random);
+ if not test -e $filename
+ break;
+ end
+ end
+
+ # Write output to pipe. This needs to be done in the background so
+ # that the command substitution exits without needing to wait for
+ # all the commands to exit
+ mkfifo $filename
+ cat >$filename &
+
+ # Write filename to stdout
+ echo $filename
+
+ # Find unique function name
+ while true
+ set funcname __fish_psub_(random);
+ if not functions $funcname >/dev/null ^/dev/null
+ break;
+ end
+ end
+
+ # Make sure we erase file when caller exits
+ eval function $funcname --on-job-exit caller\; rm $filename\; functions -e $funcname\; end
+
+end