fish-shell/share/functions/isatty.fish
Geoff Nixon 60808a4820 Enhance/fix isatty using command test.
Presently, `isatty` only works on a handful of keywords. Here it
  is rewritten to be able to take any path, device or fd number as
  an argument, and eliminates errors printed to stdout.
  Per discussion in #1228, using `builtin test -c` within a pipe to
  test special file descriptors is not viable, so this implementation
  specifcially uses `command test`. Additionally, a note has been
  added to the documentation of `test` regarding this potential
  aberration from the expected output of the test utility under the
  'Standards' section.
2014-01-28 11:14:54 -08:00

28 lines
692 B
Fish

function isatty -d "Test if a file or file descriptor is a tty."
# Use `command test` because `builtin test` doesn't open the regular fd's.
switch "$argv"
case '-h*' '--h*'
__fish_print_help isatty
case ''
command test -c /dev/stdin
case '*'
if test -e "$argv" # The eval here is needed for symlinks. Unsure why.
command test -c "$argv"; and eval tty 0>"$argv" >/dev/null
else if test -e /dev/"$argv"
command test -c /dev/"$argv"; and tty 0>/dev/"$argv" >/dev/null
else if test -e /dev/fd/"$argv"
command test -c /dev/fd/"$argv"; and tty 0>/dev/fd/"$argv" >/dev/null
else
return 1
end
end
end