aboutsummaryrefslogtreecommitdiffhomepage
path: root/share/functions/psub.fish
diff options
context:
space:
mode:
authorGravatar Eugene Sharygin <eush77@gmail.com>2015-02-16 15:28:11 +0300
committerGravatar Fabian Homborg <FHomborg@gmail.com>2015-11-14 13:15:30 +0100
commit5db811253e0440b372b9063d8a92a98e06a39171 (patch)
tree8c1fd59a143896a6b6dec7cd4958bb6ae596849a /share/functions/psub.fish
parente31a93040e46f32267ec3a72eca71a008e5c9e3a (diff)
psub: add -s, --suffix
Diffstat (limited to 'share/functions/psub.fish')
-rw-r--r--share/functions/psub.fish60
1 files changed, 37 insertions, 23 deletions
diff --git a/share/functions/psub.fish b/share/functions/psub.fish
index d1940449..22104531 100644
--- a/share/functions/psub.fish
+++ b/share/functions/psub.fish
@@ -1,42 +1,49 @@
function psub --description "Read from stdin into a file and output the filename. Remove the file when the command that called psub exits."
+ set -l dirname
set -l filename
set -l funcname
+ set -l suffix
set -l use_fifo 1
- set -l shortopt -o hf
- set -l longopt -l help,file
- if getopt -T >/dev/null
- set longopt
- end
-
- if not getopt -n psub -Q $shortopt $longopt -- $argv >/dev/null
- return 1
- end
-
- set -l tmp (getopt $shortopt $longopt -- $argv)
-
- eval set opt $tmp
-
- while count $opt >/dev/null
+ while count $argv >/dev/null
- switch $opt[1]
+ switch $argv[1]
case -h --help
__fish_print_help psub
return 0
case -f --file
set use_fifo 0
+ set -e argv[1]
+
+ case -s --suffix
+ if not set -q argv[2]
+ printf "psub: missing operand\n"
+ return 1
+ end
+ set suffix $argv[2]
+ set -e argv[1..2]
case --
- set -e opt[1]
+ set -e argv[1]
break
- end
+ case "-?" "--*"
+ printf "psub: invalid option: '%s'\n" $argv[1]
+ return 1
- set -e opt[1]
+ case "-*"
+ # Ungroup short options: -hfs => -h -f -s
+ set opts "-"(string sub -s 2 -- $argv[1] | string split "")
+ set -e argv[1]
+ set argv $opts $argv
+ case "*"
+ printf "psub: extra operand: '%s'\n" $argv[1]
+ return 1
+ end
end
if not status --is-command-substitution
@@ -53,13 +60,17 @@ function psub --description "Read from stdin into a file and output the filename
# 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
- set dir (mktemp -d "$TMPDIR[1]"/.psub.XXXXXXXXXX); or return
- set filename $dir/psub.fifo
+ set dirname (mktemp -d "$TMPDIR[1]"/.psub.XXXXXXXXXX); or return
+ set filename $dirname/psub.fifo"$suffix"
mkfifo $filename
cat >$filename &
- else
+ else if test -z $suffix
set filename (mktemp "$TMPDIR[1]"/.psub.XXXXXXXXXX)
cat >$filename
+ else
+ set dirname (mktemp -d "$TMPDIR[1]"/.psub.XXXXXXXXXX)
+ set filename $dirname/psub"$suffix"
+ cat >$filename
end
# Write filename to stdout
@@ -74,8 +85,11 @@ function psub --description "Read from stdin into a file and output the filename
end
# Make sure we erase file when caller exits
- function $funcname --on-job-exit caller --inherit-variable filename --inherit-variable funcname
+ function $funcname --on-job-exit caller --inherit-variable filename --inherit-variable dirname --inherit-variable funcname
command rm $filename
+ if count $dirname >/dev/null
+ command rmdir $dirname
+ end
functions -e $funcname
end