psub: add -s, --suffix

This commit is contained in:
Eugene Sharygin 2015-02-16 15:28:11 +03:00 committed by Fabian Homborg
parent e31a93040e
commit 5db811253e
4 changed files with 69 additions and 26 deletions

View file

@ -2,7 +2,7 @@
\subsection psub-synopsis Synopsis \subsection psub-synopsis Synopsis
\fish{synopsis} \fish{synopsis}
COMMAND1 ( COMMAND2 | psub [-f] ) COMMAND1 ( COMMAND2 | psub [-f] [-s SUFFIX])
\endfish \endfish
\subsection psub-description Description \subsection psub-description Description
@ -11,10 +11,15 @@ Posix shells feature a syntax that is a mix between command substitution and pip
If the `-f` or `--file` switch is given to `psub`, `psub` will use a regular file instead of a named pipe to communicate with the calling process. This will cause `psub` to be significantly slower when large amounts of data are involved, but has the advantage that the reading process can seek in the stream. If the `-f` or `--file` switch is given to `psub`, `psub` will use a regular file instead of a named pipe to communicate with the calling process. This will cause `psub` to be significantly slower when large amounts of data are involved, but has the advantage that the reading process can seek in the stream.
If the `-s` or `---suffix` switch is given, `psub` will append SUFFIX to the filename.
\subsection psub-example Example \subsection psub-example Example
\fish \fish
diff (sort a.txt | psub) (sort b.txt | psub) diff (sort a.txt | psub) (sort b.txt | psub)
# shows the difference between the sorted versions of files `a.txt` and `b.txt`. # shows the difference between the sorted versions of files `a.txt` and `b.txt`.
source-highlight -f esc (cpp main.c | psub -s .c)
# highlights `main.c` after preprocessing as a C source.
\endfish \endfish

View file

@ -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." 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 filename
set -l funcname set -l funcname
set -l suffix
set -l use_fifo 1 set -l use_fifo 1
set -l shortopt -o hf
set -l longopt -l help,file
if getopt -T >/dev/null while count $argv >/dev/null
set longopt
end
if not getopt -n psub -Q $shortopt $longopt -- $argv >/dev/null switch $argv[1]
return 1
end
set -l tmp (getopt $shortopt $longopt -- $argv)
eval set opt $tmp
while count $opt >/dev/null
switch $opt[1]
case -h --help case -h --help
__fish_print_help psub __fish_print_help psub
return 0 return 0
case -f --file case -f --file
set use_fifo 0 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 -- case --
set -e opt[1] set -e argv[1]
break break
case "-?" "--*"
printf "psub: invalid option: '%s'\n" $argv[1]
return 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
set -e opt[1]
end end
if not status --is-command-substitution 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 # Write output to pipe. This needs to be done in the background so
# that the command substitution exits without needing to wait for # that the command substitution exits without needing to wait for
# all the commands to exit # all the commands to exit
set dir (mktemp -d "$TMPDIR[1]"/.psub.XXXXXXXXXX); or return set dirname (mktemp -d "$TMPDIR[1]"/.psub.XXXXXXXXXX); or return
set filename $dir/psub.fifo set filename $dirname/psub.fifo"$suffix"
mkfifo $filename mkfifo $filename
cat >$filename & cat >$filename &
else else if test -z $suffix
set filename (mktemp "$TMPDIR[1]"/.psub.XXXXXXXXXX) set filename (mktemp "$TMPDIR[1]"/.psub.XXXXXXXXXX)
cat >$filename cat >$filename
else
set dirname (mktemp -d "$TMPDIR[1]"/.psub.XXXXXXXXXX)
set filename $dirname/psub"$suffix"
cat >$filename
end end
# Write filename to stdout # Write filename to stdout
@ -74,8 +85,11 @@ function psub --description "Read from stdin into a file and output the filename
end end
# Make sure we erase file when caller exits # 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 command rm $filename
if count $dirname >/dev/null
command rmdir $dirname
end
functions -e $funcname functions -e $funcname
end end

View file

@ -97,6 +97,27 @@ else
echo 'psub file was deleted' echo 'psub file was deleted'
end end
if count (echo foo | psub -s .cc | grep -o '\.cc$') >/dev/null
echo 'psub filename ends with .cc'
else
echo 'psub filename does not end with .cc'
end
if count (echo foo | psub -f -s .cc | grep -o '\.cc$') >/dev/null
echo 'psub filename ends with .cc'
else
echo 'psub filename does not end with .cc'
end
set -l filename (echo foo | psub -s .fish)
if test -e (dirname $filename)
echo 'psub directory was not deleted'
else
echo 'psub directory was deleted'
end
diff -q (__fish_print_help psub | psub) (psub -hs banana | psub)
# Test support for unbalanced blocks # Test support for unbalanced blocks
function try_unbalanced_block function try_unbalanced_block
../fish -c "echo $argv | source " 2>&1 | grep "Missing end" 1>&2 ../fish -c "echo $argv | source " 2>&1 | grep "Missing end" 1>&2

View file

@ -17,6 +17,9 @@ foo
bar bar
baz baz
psub file was deleted psub file was deleted
psub filename ends with .cc
psub filename ends with .cc
psub directory was deleted
bom_test bom_test
not#a#comment not#a#comment
is is