Files
fish-shell/share/functions/seq.fish

69 lines
1.5 KiB
Fish
Raw Normal View History

# localization: skip(uses-apropos)
# If seq is not installed, then define a function that invokes __fish_fallback_seq
# We can't call type here because that also calls seq
2013-01-12 15:22:09 -08:00
2025-09-29 11:57:41 +02:00
if command -sq seq
exit
end
if command -sq gseq
# No seq provided by the OS, but GNU coreutils was apparently installed, fantastic
function seq
2025-09-29 11:57:41 +02:00
gseq $argv
end
2025-09-29 11:57:41 +02:00
exit
end
2025-09-29 11:57:41 +02:00
# No seq command
function seq
2025-09-29 11:57:41 +02:00
__fish_fallback_seq $argv
end
function __fish_fallback_seq
2025-09-29 11:57:41 +02:00
set -l from 1
set -l step 1
set -l to 1
2025-09-29 11:57:41 +02:00
# Remove a "--" argument if it happens first.
if test "x$argv[1]" = x--
set -e argv[1]
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 "seq: Expected 1, 2 or 3 arguments, got %d\n" (count $argv) >&2
2025-09-29 11:57:41 +02:00
return 1
end
2025-09-29 11:57:41 +02:00
for i in $from $step $to
if not string match -rq -- '^-?[0-9]*([0-9]*|\.[0-9]+)$' $i
printf "seq: '%s' is not a number\n" $i >&2
2025-09-29 11:57:41 +02:00
return 1
end
2025-09-29 11:57:41 +02:00
end
2025-09-29 11:57:41 +02:00
if test $step -ge 0
set -l i $from
while test $i -le $to
echo $i
set i (math -- $i + $step)
end
else
set -l i $from
while test $i -ge $to
echo $i
set i (math -- $i + $step)
end
end
2013-01-12 15:22:09 -08:00
end