zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Zach Riggle <zachriggle@gmail.com>
Cc: Zsh Users <zsh-users@zsh.org>
Subject: Re: TRAPDEBUG vs zsh -x
Date: Thu, 21 Apr 2022 11:42:05 -0700	[thread overview]
Message-ID: <CAH+w=7b_q=PePLV53CcEukom_jrO7QC85ZT=KmcW=rMGCugS7w@mail.gmail.com> (raw)
In-Reply-To: <CAH+w=7Ywp-8fciUvKFJaSnn5jstb10P-Auw+0F4Pxd9buzZ+eA@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 1308 bytes --]

On Wed, Apr 20, 2022 at 4:55 PM Bart Schaefer <schaefer@brasslantern.com> wrote:
>
> You could play around with variations on this hack:

This was actually kind of fun, except for the part where I was baffled
for an hour by sysread apparently being buggy.

I installed https://gitlab.com/saalen/highlight

Aside:  Anyone want to supply a zsh language file and completion
script for that?  The "sh" language file is OK but not perfect for
zsh.

The attached file creates a function "hlxt" plus a precmd helper which
arrange to write xtrace output through "highlight" and play it back
just before printing the next prompt.  It uses a coproc for this.
That means it won't work in a shell script, which is why I've named
the file "v1" while I play with it some more.

To use it, source the file and type "hlxt on".  To stop, "hlxt off".

Important caveats:

Capturing the trace output has to redirect stderr, which means error
output from the commands you run is also passed through it.  To avoid
that, add 2>&$stderr to the command line.

This can only handle as much trace output as will fit in the OS pipe
buffers on the input and output of the coprocess.  More than that will
cause it to hang because the output buffer is not consumed until
precmd runs.  Solving for the script problem might solve this too.

[-- Attachment #2: hlxt_v1.txt --]
[-- Type: text/plain, Size: 1458 bytes --]

#autoload

autoload add-zsh-hook
zmodload zsh/system || exit 1

typeset -g hlxtout hlxtin stderr oPS4="$PS4"
coproc highlight -S sh -O xterm256
exec {hlxtout}<&p
exec {hlxtin}>&p
exec {stderr}>&2

# We don't want the commands that are managing highlighting to appear
# in the trace output, so create them "sticky" with xtrace disabled
emulate zsh +x -c '
  hlxt_precmd() {
    local xtrace xt xc=0
    # Unfortunately this has to use a one-second timeout because
    # "highlight" does not always write quickly enough.
    #
    # Manual says combining sysread -o with a param is valid, but
    # that does not work; neither output nor parameter is written.
    # while sysread -t 1 -i $hlxtout -c xc -o $stderr xtrace
    while sysread -t 1 -i $hlxtout -c xc xtrace
    do
      [[ -n $xtrace ]] && print -n -u $stderr -r -- "${xt::=$xtrace}"
    done
    # Need to handle error?  (( $? < 4 && xc )) && [[ -n $xtrace ]]
    [[ -z $xt || $xt == *$'"'\\n'"' ]] || echo
  }
  hlxt() {
    setopt localoptions localtraps
    case $1 in
    (on|)
      add-zsh-hook precmd hlxt_precmd || exit 1
      PS4=
      exec 2>&$hlxtin
      # Bypass local scope restore of xtrace
      trap "setopt xtrace" EXIT
    ;;
    (off)
      exec 2>&$stderr
      PS4="$oPS4"
      add-zsh-hook -d precmd hlxt_precmd
      trap "unsetopt xtrace" EXIT
      # Flush leftovers
      local stderr
      hlxt_precmd {stderr}>&/dev/null
    ;;
    (*) add-zsh-hook -L
  esac
  }
'

      reply	other threads:[~2022-04-21 18:43 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-20 19:46 Zach Riggle
2022-04-20 23:55 ` Bart Schaefer
2022-04-21 18:42   ` Bart Schaefer [this message]

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=7b_q=PePLV53CcEukom_jrO7QC85ZT=KmcW=rMGCugS7w@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --cc=zachriggle@gmail.com \
    --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).