From: Bart Schaefer <schaefer@brasslantern.com>
To: Anthony Heading <ajrh@ajrh.net>
Cc: Zsh Users <zsh-users@zsh.org>
Subject: Re: view onto command pipeline?
Date: Sat, 16 Jul 2022 14:30:07 -0700 [thread overview]
Message-ID: <CAH+w=7Z9CO84cRiYcr8n25=e5kz72KsSGrmwKGsdg6W79ONYXw@mail.gmail.com> (raw)
In-Reply-To: <28c00da7-cfdd-4f58-a539-8deebbe5dfa5@www.fastmail.com>
On Sat, Jul 16, 2022 at 9:16 AM Anthony Heading <ajrh@ajrh.net> wrote:
>
> I'm trying to wrap 'git' in a shell function to better anticipate when I want colour output. Which is probably something like "when run from the command line and outputting to a tty or piped a pager, but not when piped to anything else".
>
> Is there a zsh way to solve this? Perhaps just by pattern matching on the command buffer to see if it has "| less" toward the end? I couldn't find any shell variables that show the current pipeline string [...]
You need to look at the preexec hook. Something like
gitpaged_hook() {
emulate -L zsh -o extendedglob
local -a match mbegin mend
if [[ $3 = (#b)git*\|[[:space:]]##(more|less)[[:space:]]#* ]]; then
export GITPAGED=$match[1]
else
unset GITPAGED
fi
}
autoload add-zsh-hook
add-zsh-hook preexec gitpaged_hook
gitwrapper() {
if [[ -o interactive ]]; then
if [[ -t 1 ]]; then
# Interactive terminal
elif [[ -n $GITPAGED ]]; then
# piped to a pager
else
# other pipeline or file output
fi
else
# not interactive
fi
}
next prev parent reply other threads:[~2022-07-16 21:31 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-07-16 16:15 Anthony Heading
2022-07-16 21:30 ` Bart Schaefer [this message]
2022-07-20 11:23 ` Anthony Heading
2022-07-20 11:37 ` Dominik Vogt
2022-07-20 12:22 ` Vincent Bernat
2022-07-20 12:35 ` Dominik Vogt
2022-07-20 19:07 ` Lawrence Velázquez
2022-07-21 0:15 ` Anthony Heading
2022-07-21 1:35 ` Lawrence Velázquez
2022-07-21 2:11 ` Anthony Heading
2022-07-21 3:15 ` Lawrence Velázquez
2022-07-21 6:43 ` Dominik Vogt
2022-08-04 21:12 ` Felipe Contreras
2022-07-20 19:27 ` Phil Pennock
2022-07-21 0:18 ` Anthony Heading
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=7Z9CO84cRiYcr8n25=e5kz72KsSGrmwKGsdg6W79ONYXw@mail.gmail.com' \
--to=schaefer@brasslantern.com \
--cc=ajrh@ajrh.net \
--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).