zsh-users
 help / color / mirror / code / Atom feed
* TRAPDEBUG vs zsh -x
@ 2022-04-20 19:46 Zach Riggle
  2022-04-20 23:55 ` Bart Schaefer
  0 siblings, 1 reply; 3+ messages in thread
From: Zach Riggle @ 2022-04-20 19:46 UTC (permalink / raw)
  To: Zsh Users

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

Hello all!

I'm quite fond of debugging with a custom script using TRAPDEBUG, but the
limitation is that the provided arguments are before the full command has
been expanded.

When using "zsh -x", we see all of the lines after they've been fully
evaluated, but there's no way to add syntax highlighting sugar to these
lines.

Are there any TRAPXXX functions which are passed the same text as "zsh -x"
will emit (sans the $PS4 expansion).

Thanks!
*Zach Riggle*

[-- Attachment #2: Type: text/html, Size: 778 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: TRAPDEBUG vs zsh -x
  2022-04-20 19:46 TRAPDEBUG vs zsh -x Zach Riggle
@ 2022-04-20 23:55 ` Bart Schaefer
  2022-04-21 18:42   ` Bart Schaefer
  0 siblings, 1 reply; 3+ messages in thread
From: Bart Schaefer @ 2022-04-20 23:55 UTC (permalink / raw)
  To: Zach Riggle; +Cc: Zsh Users

On Wed, Apr 20, 2022 at 12:46 PM Zach Riggle <zachriggle@gmail.com> wrote:
>
> When using "zsh -x", we see all of the lines after they've been fully evaluated, but there's no way to add syntax highlighting sugar to these lines.

If you have a colorizing program that you want to use, you can do

# Silly example of colorizing
alias colorizing="grep --color '[A-Z]'"
exec 2>>(colorizing)

and the xtrace output will go through that.

> Are there any TRAPXXX functions which are passed the same text as "zsh -x" will emit (sans the $PS4 expansion).

No, there aren't.

You could play around with variations on this hack:

unsetopt DEBUG_BEFORE_CMD
PS4=
emulate zsh +x -c 'exec {stderr}>&2
 preexec() {
  integer in_preexec=1
  exec 2>>(colorizing)
 }
 TRAPDEBUG() {
  ((in_preexec)) || exec 2>>$stderr
 }'


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: TRAPDEBUG vs zsh -x
  2022-04-20 23:55 ` Bart Schaefer
@ 2022-04-21 18:42   ` Bart Schaefer
  0 siblings, 0 replies; 3+ messages in thread
From: Bart Schaefer @ 2022-04-21 18:42 UTC (permalink / raw)
  To: Zach Riggle; +Cc: Zsh Users

[-- 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
  }
'

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-04-21 18:43 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-20 19:46 TRAPDEBUG vs zsh -x Zach Riggle
2022-04-20 23:55 ` Bart Schaefer
2022-04-21 18:42   ` Bart Schaefer

Code repositories for project(s) associated with this 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).