Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: view onto command pipeline?



On Sat, Jul 16, 2022 at 9:16 AM Anthony Heading <ajrh@xxxxxxxx> 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
}




Messages sorted by: Reverse Date, Date, Thread, Author