zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Ray Andrews <rayandrews@eastlink.ca>
Cc: Zsh Users <zsh-users@zsh.org>
Subject: Re: detect pipe
Date: Fri, 29 Jan 2021 16:29:16 -0800	[thread overview]
Message-ID: <CAH+w=7Zv2cvAi3bT-G-BwreRU88__snBiur2X+FaOGs4roLsnw@mail.gmail.com> (raw)
In-Reply-To: <05c3d2ba-6a00-9480-e2a8-685432e2442c@eastlink.ca>

On Thu, Jan 28, 2021 at 7:22 PM Ray Andrews <rayandrews@eastlink.ca> wrote:
>
> Sorry to waste everyone's time.

Not necessarily a waste.

A couple of more words on this, particularly on the theme of "tabs -4".

"tabs" adjusts the terminal, and pretty much by definition only for
output, so you should be able to get away with

# Only run "tabs" on terminal stdout
# Close stdin to avoid stealing input
[[ -t 1 ]] && tabs -4 <&-

The next thing to note is that you can use "anonymous" functions in an
alias definition:

alias g='function {
  [[ -t 1 ]] && tabs -4 <&-
  grep --color=always "$@"
}'

This works because words following the closing brace of an anonymous
function become its argument list.  Simpler example:

% alias q='function { print -rl -- "$@" }'
% q a b c
a
b
c
%

I'd also point out that by using "$@" instead of "$1" "$2" in "g", you
never pass an empty string as the filename to grep.  That means you
don't have to care whether standard input is a pipe; it will read
standard input if there is one argument, and read from $2 (and any
subsequent file names) if there are 2 or more arguments.  The
difference from your original definition is that you don't get "grep:
: No such file or directory" if you pass 1 argument when the standard
input is NOT a pipe.

The last note is that this inlined-function trick does NOT work with
"noglob".  You CANNOT write e.g.

noglob function { print -rl -- "$@" } a* *b

So if you want to introduce noglob, you have to use a real function.
You can still do everything else I mentioned.

_g() {
  [[ -t 1 ]] && tabs -4 <&-
  grep --color=always "$@"
}
alias g='noglob _g'


  reply	other threads:[~2021-01-30  0:30 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-27 16:45 Ray Andrews
2021-01-27 23:59 ` Bart Schaefer
2021-01-28  4:57   ` Ray Andrews
2021-01-28  6:16     ` Bart Schaefer
2021-01-28 10:08     ` Vincent Lefevre
2021-01-28 10:28       ` Vincent Lefevre
2021-01-28 15:05         ` Ray Andrews
2021-01-28 15:45           ` Ray Andrews
2021-01-28 20:58             ` Bart Schaefer
2021-01-28 20:59               ` Bart Schaefer
2021-01-29  1:17               ` Ray Andrews
2021-01-29  2:50                 ` Bart Schaefer
2021-01-29  3:21                   ` Ray Andrews
2021-01-30  0:29                     ` Bart Schaefer [this message]
2021-01-30 14:26                       ` Ray Andrews
2021-01-30 19:00                         ` Bart Schaefer
2021-01-30 19:19                           ` Ray Andrews
2021-01-31 19:12                             ` Ray Andrews
2021-01-31 21:28                               ` Bart Schaefer
2021-02-14 17:31                           ` Vincent Lefevre
2021-02-14 19:33                             ` Bart Schaefer
2021-02-14 20:33                               ` Ray Andrews
2021-02-14 21:20                                 ` Bart Schaefer
2021-02-14 22:25                                   ` Ray Andrews
2021-02-14 21:24                                 ` Vincent Lefevre
2021-02-14 21:19                               ` Vincent Lefevre
2021-02-14 21:24                                 ` Bart Schaefer
2021-02-14 21:38                                 ` Bart Schaefer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAH+w=7Zv2cvAi3bT-G-BwreRU88__snBiur2X+FaOGs4roLsnw@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --cc=rayandrews@eastlink.ca \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).