aboutsummaryrefslogtreecommitdiffhomepage
path: root/share/functions/isatty.fish
diff options
context:
space:
mode:
authorGravatar Geoff Nixon <geoff.nixon@aol.com>2014-01-07 19:57:49 -0800
committerGravatar pca <pca@pca-macbookair.roam.corp.google.com>2014-01-28 11:14:54 -0800
commit60808a4820b1714499f601ed78f0ae903bdb779f (patch)
tree5c174b2dfb606b922d0c2176537e73fa0aa36443 /share/functions/isatty.fish
parentd5cb16aa1ff20040042ac58eaa31ce1d7dc9cbd0 (diff)
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.
Diffstat (limited to 'share/functions/isatty.fish')
-rw-r--r--share/functions/isatty.fish37
1 files changed, 18 insertions, 19 deletions
diff --git a/share/functions/isatty.fish b/share/functions/isatty.fish
index d4f2c43e..f49b3aa4 100644
--- a/share/functions/isatty.fish
+++ b/share/functions/isatty.fish
@@ -1,28 +1,27 @@
+function isatty -d "Test if a file or file descriptor is a tty."
-function isatty -d "Tests if a file descriptor is a tty"
- set -l fd 0
- if count $argv >/dev/null
- switch $argv[1]
+# Use `command test` because `builtin test` doesn't open the regular fd's.
- case -h --h --he --hel --help
- __fish_print_help isatty
- return 0
+ switch "$argv"
- case stdin
- set fd 0
+ case '-h*' '--h*'
+ __fish_print_help isatty
- case stdout
- set fd 1
+ case ''
+ command test -c /dev/stdin
- case stderr
- set fd 2
+ 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
- case '*'
- set fd $argv[1]
+ else if test -e /dev/"$argv"
+ command test -c /dev/"$argv"; and tty 0>/dev/"$argv" >/dev/null
- end
- end
-
- eval "tty 0>&$fd >/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