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:
ridiculousfish 2013-01-12 14:18:34 -08:00
parent 373cca0bf6
commit dc37a8079e
6 changed files with 90 additions and 55 deletions

View file

@ -293,6 +293,11 @@ char *wcs2str(const wchar_t *in)
return wcs2str_internal(in, out); 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 */ /* This function is distinguished from wcs2str_internal in that it allows embedded null bytes */
std::string wcs2string(const wcstring &input) std::string wcs2string(const wcstring &input)
{ {

View file

@ -226,6 +226,7 @@ wcstring str2wcstring(const std::string &in);
way using the private use area. way using the private use area.
*/ */
char *wcs2str(const wchar_t *in); char *wcs2str(const wchar_t *in);
char *wcs2str(const wcstring &in);
std::string wcs2string(const wcstring &input); std::string wcs2string(const wcstring &input);
/** Test if a string prefixes another. Returns true if a is a prefix of b */ /** Test if a string prefixes another. Returns true if a is a prefix of b */

47
seq.in
View file

@ -1,25 +1,18 @@
#!/usr/bin/env fish # 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
# Fallback implementation of the seq command function seq --description "Print sequences of numbers"
# __fish_fallback_seq $argv
# @configure_input@
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
end
end end
end end
switch (count $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 case 1
set to $argv[1] set to $argv[1]
@ -36,17 +29,19 @@ switch (count $argv)
printf (_ "%s: Expected 1, 2 or 3 arguments, got %d\n") seq (count $argv) printf (_ "%s: Expected 1, 2 or 3 arguments, got %d\n") seq (count $argv)
exit 1 exit 1
end end
for i in $from $step $to for i in $from $step $to
if not echo $i | grep -E '^-?[0-9]*([0-9]*|\.[0-9]+)$' >/dev/null if not echo $i | grep -E '^-?[0-9]*([0-9]*|\.[0-9]+)$' >/dev/null
printf (_ "%s: '%s' is not a number\n") seq $i printf (_ "%s: '%s' is not a number\n") seq $i
exit 1 exit 1
end 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
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

View file

@ -6,21 +6,40 @@ function type --description "Print the type of a command"
set -l mode normal set -l mode normal
set -l selection all 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 # Get options
# #
set -l shortopt -o tpPafh set -l options
set -l shortopt tpPafh
set -l longopt set -l longopt
if not getopt -T >/dev/null if test $new_getopt
set longopt -l type,path,force-path,all,no-functions,help # GNU getopt
end set longopt type,path,force-path,all,no-functions,help
set options -o $shortopt -l $longopt --
if not getopt -n type -Q $shortopt $longopt -- $argv >/dev/null # Verify options
if not getopt -n type $options $argv >/dev/null
return 1 return 1
end end
else
# Old getopt, used on OS X
set options $shortopt
# Verify options
if not getopt $options $argv >/dev/null
return 1
end
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 set -l opt
eval set opt $tmp eval set opt $tmp

View file

@ -110,3 +110,15 @@ if not echo skip1 > /dev/null
else if echo skip2 > /dev/null else if echo skip2 > /dev/null
echo "zeta 6.2" echo "zeta 6.2"
end 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

View file

@ -25,3 +25,6 @@ delta4.1
delta4.2 delta4.2
epsilon5.2 epsilon5.2
zeta 6.2 zeta 6.2
###
0
1