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

Re: Cannot invoke widget within zpty, with \C-a nor a



On Tue, 6 Nov 2018 at 18:16, Peter Stephenson <p.stephenson@xxxxxxxxxxx> wrote:
>
> On Tue, 2018-11-06 at 18:08 +0100, Sebastian Gniazdowski wrote:
> > I now see that testing the running of zle widgets is in general
> > difficult, even with zpty.
>
> For sure.  zpty just doesn't provide a robust enough platform, at least
> in its current state.  This is true for the job control tests, too, with
> the asymmetric messages popping up.
>
> I've been groping for suggestions but haven't really got any directions.

I've managed to test region_highlight altered in zle widget, triggered
by zpty -w zsh $'\C-a', with zpty-zshell running without  +Z (i.e. no
disabling of Zle). Use of Zle means that test's commands will be
echoed back, but it doesn't look bad, e.g.:

  zpty_start
  zpty_input 'rh_widget() { BUFFER="true"; region_highlight+=( "0 4
fg=#040810" ); }' nl
  zpty_input 'zle -N rh_widget' nl
  zpty_input 'bindkey "\C-a" rh_widget' nl
  zpty_input $'\C-a'
  zpty_line 6   # 3 lines echoed back + 3 empty lines
  zpty_line 1 p # o is for "preserve" – to keep colour escapes
converting them to regular strings
  zpty_stop
0:basic region_highlight with true-color (hex-triplets)
>rh_widget() { BUFFER="true"; region_highlight+=( "0 4 fg=#040810" ); }
>zle -N rh_widget
>bindkey "\C-a" rh_widget
>0m38;2;4;8;16mtrue39m
>exit

Complete ztst-file is attached. Is there a chance it will be added to upstream?
-- 
Sebastian Gniazdowski
News: https://twitter.com/ZdharmaI
IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin
Blog: http://zdharma.org
# Tests for region_highlight, true-color support, near-color support

%prep

  if [[ $OSTYPE == cygwin ]]; then
    ZTST_unimplemented='the zsh/zpty module does not work on Cygwin'
  elif zmodload zsh/zpty 2> /dev/null; then
    zpty_start() {
      export PS1= PS2=
      zpty -d
      zpty zsh "${(q)ZTST_testdir}/../Src/zsh -fiV"
    }
    zpty_input() {
      zpty -w zsh "$1" ${${(M)2:#nl}:+$'\n'}
    }
    zpty_line() {
      setopt localoptions extendedglob noshwordsplit
      local REPLY cm=$'\r'
      integer i
      for (( i = 0; i < ${1:-1}; ++i )); do
        zpty -r zsh REPLY
        # P is for "preserve", induces keeping some
        # color codes to test region_highlight, etc.
        # The color codes are then made regular text
        [[ "$2" = "p" ]] && {
            REPLY=${REPLY//$'\x1b'\[([2][0-9;]m|[JK]|\?[0-9]##(h|l))/}
            REPLY=${REPLY//(#b)$'\x1b'\[([0-9;]##m)/${match[1]}}
        } || {
            REPLY=${REPLY//$'\x1b'\[([0-9;]##m|[JK]|\?[0-9]##(h|l))/}
        }
        # Fix e^Mexit - match ((?)\r(?)), if \2 == \3, then replace with \2
        # otherwise replace with \1 stripped out of leading/trailing [[:space:]]
        REPLY=${REPLY//(#b)((?(#c0,1))$cm(?(#c0,1)))/${${${(M)match[2]:#${match[3]}}:+${match[2]}}:-${${match[1]##[[:space:]]##}%%[[:space:]]##}}}
        [[ -n "$REPLY" ]] && print -r -- ${${REPLY%%[[:space:]]##}##[[:space:]]##}
      done
    }
    zpty_stop() {
      setopt localoptions extendedglob
      local REPLY cm=$'\r'
      zpty -w zsh $'exit\n'
      # zpty gives no output when piped without these braces (?)
      # The while loop with // substitution is to convert `e^Mexit'
      # into `exit' (see zpty_line). The sed commands remove escapes
      { zpty -r zsh } | sed $'/[^[:space:]]/!d; s/\r$//; s/\x1b\\[[0-9;]*m//g; s/\x1b\\[[JK]//g; s/\x1b\\[?[0-9]*[hl]//g' | while read REPLY; do REPLY=${REPLY//(#b)((?(#c0,1))$cm(?(#c0,1)))/${${${(M)match[2]:#${match[3]}}:+${match[2]}}:-${${match[1]##[[:space:]]##}%%[[:space:]]##}}}; print -r -- "$REPLY"; done
      zpty -d
      :
    }
  else
    ZTST_unimplemented='the zsh/zpty module is not available'
  fi

%test

  zpty_start
  zpty_input 'rh_widget() { BUFFER="true"; region_highlight+=( "0 4 fg=green" ); }' nl
  zpty_input 'zle -N rh_widget' nl
  zpty_input 'bindkey "\C-a" rh_widget' nl
  zpty_input $'\C-a'
  zpty_line 6   # 3 lines echoed back + 3 empty lines
  zpty_line 1 p
  zpty_stop
0:basic region_highlight with 8 colors
>rh_widget() { BUFFER="true"; region_highlight+=( "0 4 fg=green" ); }
>zle -N rh_widget
>bindkey "\C-a" rh_widget
>0m32mtrue39m
>exit

  zpty_start
  zpty_input 'rh_widget() { BUFFER="true"; region_highlight+=( "0 4 fg=#040810" ); }' nl
  zpty_input 'zle -N rh_widget' nl
  zpty_input 'bindkey "\C-a" rh_widget' nl
  zpty_input $'\C-a'
  zpty_line 6   # 3 lines echoed back + 3 empty lines
  zpty_line 1 p
  zpty_stop
0:basic region_highlight with true-color (hex-triplets)
>rh_widget() { BUFFER="true"; region_highlight+=( "0 4 fg=#040810" ); }
>zle -N rh_widget
>bindkey "\C-a" rh_widget
>0m38;2;4;8;16mtrue39m
>exit

%clean

  zmodload -ui zsh/zpty

# vim:ft=zsh


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