Owen_R

test fishshell/isatty/pty

Apr 20th, 2021 (edited)
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. -
  2. def:
  3. function testfunc
  4. if isatty 1
  5. set_color red
  6. echo "yup, i think i'm outputting to a tty (so this is color red) (argv is $argv)"
  7. set_color normal
  8. else
  9. echo "no, i think i'm *not* outputting to a tty (no color) (argv is $argv)"
  10. end
  11. end
  12.  
  13. tests:
  14. > testfunc some args with spaces
  15. #=> yup, i think i'm outputting to a tty (so this is color red) (argv is some args with spaces)
  16.  
  17. > testfunc some args with spaces|xargs -0 echo
  18. #=> no, i think i'm *not* outputting to a tty (no color) (argv is some args with spaces)
  19.  
  20. > pty testfunc some args with spaces|xargs -0 echo
  21. #=> yup, i think i'm outputting to a tty (so this is color red) (argv is some args with spaces)
  22.  
  23.  
  24. ... wait, you mean problems like *this*, right?:
  25.  
  26. def:
  27. function testfunc
  28. if isatty 1
  29. set_color red
  30. echo "yup, i think i'm outputting to a tty (so this is color red) (args are: " (for i in (seq (count $argv)); echo -n "argv[$i]:$argv[$i] " ;end) ")"
  31. set_color normal
  32. else
  33. echo "no, i think i'm *not* outputting to a tty (no color) (args are: " (for i in (seq (count $argv)); echo -n "argv[$i]:$argv[$i] " ;end) ")"
  34. end
  35. end
  36.  
  37. tests:
  38. > testfunc "args with" 'spaces in them'
  39. #=> yup, i think i'm outputting to a tty (so this is color red) (args are: argv[1]:args with argv[2]:spaces in them )
  40. > pty testfunc "args with" 'spaces in them'|xargs -0 echo
  41. #=> yup, i think i'm outputting to a tty (so this is color red) (args are: argv[1]:args argv[2]:with argv[3]:spaces argv[4]:in argv[5]:them )
  42.  
  43.  
Advertisement
Add Comment
Please, Sign In to add comment