mirror of
https://github.com/fish-shell/fish-shell
synced 2024-11-10 23:24:39 +00:00
Added a seq function that defers to the seq command if present
https://github.com/fish-shell/fish-shell/issues/137
This commit is contained in:
parent
373cca0bf6
commit
dc37a8079e
6 changed files with 90 additions and 55 deletions
|
@ -293,6 +293,11 @@ char *wcs2str(const wchar_t *in)
|
|||
return wcs2str_internal(in, out);
|
||||
}
|
||||
|
||||
char *wcs2str(const wcstring &in)
|
||||
{
|
||||
return wcs2str(in.c_str());
|
||||
}
|
||||
|
||||
/* This function is distinguished from wcs2str_internal in that it allows embedded null bytes */
|
||||
std::string wcs2string(const wcstring &input)
|
||||
{
|
||||
|
|
1
common.h
1
common.h
|
@ -226,6 +226,7 @@ wcstring str2wcstring(const std::string &in);
|
|||
way using the private use area.
|
||||
*/
|
||||
char *wcs2str(const wchar_t *in);
|
||||
char *wcs2str(const wcstring &in);
|
||||
std::string wcs2string(const wcstring &input);
|
||||
|
||||
/** Test if a string prefixes another. Returns true if a is a prefix of b */
|
||||
|
|
85
seq.in
85
seq.in
|
@ -1,52 +1,47 @@
|
|||
#!/usr/bin/env fish
|
||||
#
|
||||
# Fallback implementation of the seq command
|
||||
#
|
||||
# @configure_input@
|
||||
# If seq is not installed, then define a function that invokes __fish_fallback_seq
|
||||
if begin ; test -x /usr/bin/seq ; or type --no-functions seq > /dev/null; end
|
||||
function seq --description "Print sequences of numbers"
|
||||
__fish_fallback_seq $argv
|
||||
end
|
||||
end
|
||||
|
||||
set -l from 1
|
||||
set -l step 1
|
||||
set -l to 1
|
||||
|
||||
function _ -d "Alias for the gettext command"
|
||||
printf "%s" $argv
|
||||
end
|
||||
if test 1 = "@HAVE_GETTEXT@"
|
||||
if which gettext ^/dev/null >/dev/null
|
||||
function _ -d "Alias for the gettext command"
|
||||
gettext fish $argv
|
||||
function __fish_fallback_seq --description "Fallback implementation of the seq command"
|
||||
|
||||
set -l from 1
|
||||
set -l step 1
|
||||
set -l to 1
|
||||
|
||||
switch (count $argv)
|
||||
case 1
|
||||
set to $argv[1]
|
||||
|
||||
case 2
|
||||
set from $argv[1]
|
||||
set to $argv[2]
|
||||
|
||||
case 3
|
||||
set from $argv[1]
|
||||
set step $argv[2]
|
||||
set to $argv[3]
|
||||
|
||||
case '*'
|
||||
printf (_ "%s: Expected 1, 2 or 3 arguments, got %d\n") seq (count $argv)
|
||||
exit 1
|
||||
|
||||
end
|
||||
|
||||
for i in $from $step $to
|
||||
if not echo $i | grep -E '^-?[0-9]*([0-9]*|\.[0-9]+)$' >/dev/null
|
||||
printf (_ "%s: '%s' is not a number\n") seq $i
|
||||
exit 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
switch (count $argv)
|
||||
case 1
|
||||
set to $argv[1]
|
||||
|
||||
case 2
|
||||
set from $argv[1]
|
||||
set to $argv[2]
|
||||
|
||||
case 3
|
||||
set from $argv[1]
|
||||
set step $argv[2]
|
||||
set to $argv[3]
|
||||
|
||||
case '*'
|
||||
printf (_ "%s: Expected 1, 2 or 3 arguments, got %d\n") seq (count $argv)
|
||||
exit 1
|
||||
|
||||
end
|
||||
|
||||
for i in $from $step $to
|
||||
if not echo $i | grep -E '^-?[0-9]*([0-9]*|\.[0-9]+)$' >/dev/null
|
||||
printf (_ "%s: '%s' is not a number\n") seq $i
|
||||
exit 1
|
||||
|
||||
if [ $step -ge 0 ]
|
||||
echo "for( i=$from; i<=$to ; i+=$step ) i;" | bc
|
||||
else
|
||||
echo "for( i=$from; i>=$to ; i+=$step ) i;" | bc
|
||||
end
|
||||
end
|
||||
|
||||
if [ $step -ge 0 ]
|
||||
echo "for( i=$from; i<=$to ; i+=$step ) i;" | bc
|
||||
else
|
||||
echo "for( i=$from; i>=$to ; i+=$step ) i;" | bc
|
||||
end
|
||||
|
|
|
@ -5,25 +5,44 @@ function type --description "Print the type of a command"
|
|||
set -l res 1
|
||||
set -l mode normal
|
||||
set -l selection all
|
||||
|
||||
|
||||
set -l new_getopt
|
||||
if not getopt -T > /dev/null
|
||||
set new_getopt 1
|
||||
else
|
||||
set new_getopt ''
|
||||
end
|
||||
|
||||
#
|
||||
# Get options
|
||||
#
|
||||
set -l shortopt -o tpPafh
|
||||
set -l options
|
||||
set -l shortopt tpPafh
|
||||
set -l longopt
|
||||
if not getopt -T >/dev/null
|
||||
set longopt -l type,path,force-path,all,no-functions,help
|
||||
if test $new_getopt
|
||||
# GNU getopt
|
||||
set longopt type,path,force-path,all,no-functions,help
|
||||
set options -o $shortopt -l $longopt --
|
||||
# Verify options
|
||||
if not getopt -n type $options $argv >/dev/null
|
||||
return 1
|
||||
end
|
||||
else
|
||||
# Old getopt, used on OS X
|
||||
set options $shortopt
|
||||
# Verify options
|
||||
if not getopt $options $argv >/dev/null
|
||||
return 1
|
||||
end
|
||||
end
|
||||
|
||||
if not getopt -n type -Q $shortopt $longopt -- $argv >/dev/null
|
||||
return 1
|
||||
end
|
||||
|
||||
set -l tmp (getopt $shortopt $longopt -- $argv)
|
||||
# Do the real getopt invocation
|
||||
set -l tmp (getopt $options $argv)
|
||||
|
||||
# Break tmp up into an array
|
||||
set -l opt
|
||||
eval set opt $tmp
|
||||
|
||||
|
||||
for i in $opt
|
||||
switch $i
|
||||
case -t --type
|
||||
|
|
|
@ -110,3 +110,15 @@ if not echo skip1 > /dev/null
|
|||
else if echo skip2 > /dev/null
|
||||
echo "zeta 6.2"
|
||||
end
|
||||
|
||||
echo '###'
|
||||
|
||||
# Ensure 'type' works
|
||||
# https://github.com/fish-shell/fish-shell/issues/513
|
||||
function fish_test_type_zzz
|
||||
true
|
||||
end
|
||||
# Should succeed
|
||||
type fish_test_type_zzz >/dev/null ; echo $status
|
||||
# Should fail
|
||||
type -f fish_test_type_zzz >/dev/null ; echo $status
|
||||
|
|
|
@ -25,3 +25,6 @@ delta4.1
|
|||
delta4.2
|
||||
epsilon5.2
|
||||
zeta 6.2
|
||||
###
|
||||
0
|
||||
1
|
||||
|
|
Loading…
Reference in a new issue