zsh-users
 help / Atom feed
* A function to display message under the prompt without zle active
@ 2019-06-22 17:17 Sebastian Gniazdowski
  2019-06-22 17:45 ` Roman Perepelitsa
  0 siblings, 1 reply; 6+ messages in thread
From: Sebastian Gniazdowski @ 2019-06-22 17:17 UTC (permalink / raw)
  To: Zsh Users

Hello,
I've forged as I think an interesting function, which will display
given message under the prompt regardless if ran from within zle or
not. It uses the exec <(...) / zle -F trick to dispatch a callback
that does have the zle active (zle -F -w isn't needed, zle -M works
fine from no-widget callback):

# deploy-message Hello world
# deploy-message @sleep:5.5 "Hello world"
deploy-message() {
    [[ "$1" = <-> && ${#} -eq 1 ]] && { zle && {
            local alltext text IFS=$'\n' nl=$'\n'
            repeat 25; do read -u"$1" text; alltext+="${text:+$text$nl}"; done
            [[ -n "$alltext" ]] && zle -M "$alltext"
        }
        zle -F "$1"; exec {1}<&-
        return 0
    }
    local THEFD
    # The expansion is: if there is @sleep: pfx, then use what's after
    # it, otherwise substitute 0
    exec {THEFD} < <(LANG=C sleep $(( 0.01 +
${${${(M)1#@sleep:}:+${1#@sleep:}}:-0} )); print -r --
${1:#(@msg|@sleep:*)} "${@[2,-1]}")
    zle -F "$THEFD" -deploy-message
}

There's a gist with the function as well, in case of any updates:
https://gist.github.com/psprint/b6503c0c37de483f68d055e9c47981b3
-- 
Sebastian Gniazdowski
News: https://twitter.com/ZdharmaI
IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin
Blog: http://zdharma.org

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

* Re: A function to display message under the prompt without zle active
  2019-06-22 17:17 A function to display message under the prompt without zle active Sebastian Gniazdowski
@ 2019-06-22 17:45 ` Roman Perepelitsa
  2019-06-22 20:49   ` Sebastian Gniazdowski
  0 siblings, 1 reply; 6+ messages in thread
From: Roman Perepelitsa @ 2019-06-22 17:45 UTC (permalink / raw)
  To: Sebastian Gniazdowski; +Cc: Zsh Users

This should work as long as you don't write too much stuff without
giving ZLE a chance to read it.

    # After executing this, you can write stuff to file descriptor $zle_msg_fd
    # and it'll be shown in the ZLE status line.
    #
    #   echo "Hello World" >&$zle_msg_fd
    #   ( echo 'Hello'; sleep 1; echo 'World' ) >&$zle_msg_fd &!
    () {
      emulate -L zsh && setopt err_return
      local fifo && fifo=$(mktemp -u "${TMPDIR:-/tmp}"/fifo.XXXXXXXXXX)
      mkfifo $fifo
      exec {zle_msg_fd} <> $fifo
      rm $fifo
      function _zle_msg_process() {
        emulate -L zsh
        while IFS='' read -r -u $zle_msg_fd line && zle -M $line
      }
      zle -F $zle_msg_fd _zle_msg_process
    }

Roman.

On Sat, Jun 22, 2019 at 7:18 PM Sebastian Gniazdowski
<sgniazdowski@gmail.com> wrote:
>
> Hello,
> I've forged as I think an interesting function, which will display
> given message under the prompt regardless if ran from within zle or
> not. It uses the exec <(...) / zle -F trick to dispatch a callback
> that does have the zle active (zle -F -w isn't needed, zle -M works
> fine from no-widget callback):
>
> # deploy-message Hello world
> # deploy-message @sleep:5.5 "Hello world"
> deploy-message() {
>     [[ "$1" = <-> && ${#} -eq 1 ]] && { zle && {
>             local alltext text IFS=$'\n' nl=$'\n'
>             repeat 25; do read -u"$1" text; alltext+="${text:+$text$nl}"; done
>             [[ -n "$alltext" ]] && zle -M "$alltext"
>         }
>         zle -F "$1"; exec {1}<&-
>         return 0
>     }
>     local THEFD
>     # The expansion is: if there is @sleep: pfx, then use what's after
>     # it, otherwise substitute 0
>     exec {THEFD} < <(LANG=C sleep $(( 0.01 +
> ${${${(M)1#@sleep:}:+${1#@sleep:}}:-0} )); print -r --
> ${1:#(@msg|@sleep:*)} "${@[2,-1]}")
>     zle -F "$THEFD" -deploy-message
> }
>
> There's a gist with the function as well, in case of any updates:
> https://gist.github.com/psprint/b6503c0c37de483f68d055e9c47981b3
> --
> Sebastian Gniazdowski
> News: https://twitter.com/ZdharmaI
> IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin
> Blog: http://zdharma.org

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

* Re: A function to display message under the prompt without zle active
  2019-06-22 17:45 ` Roman Perepelitsa
@ 2019-06-22 20:49   ` Sebastian Gniazdowski
  2019-06-22 21:17     ` Roman Perepelitsa
  0 siblings, 1 reply; 6+ messages in thread
From: Sebastian Gniazdowski @ 2019-06-22 20:49 UTC (permalink / raw)
  To: Roman Perepelitsa; +Cc: Zsh Users

On Sat, 22 Jun 2019 at 19:45, Roman Perepelitsa
<roman.perepelitsa@gmail.com> wrote:
>
> This should work as long as you don't write too much stuff without
> giving ZLE a chance to read it.
>
>     # After executing this, you can write stuff to file descriptor $zle_msg_fd
>     # and it'll be shown in the ZLE status line.

The command line seems to be blcoked after first using the descriptor.

>     #   echo "Hello World" >&$zle_msg_fd
>     #   ( echo 'Hello'; sleep 1; echo 'World' ) >&$zle_msg_fd &!
>     () {
>       emulate -L zsh && setopt err_return
>       local fifo && fifo=$(mktemp -u "${TMPDIR:-/tmp}"/fifo.XXXXXXXXXX)
>       mkfifo $fifo
>       exec {zle_msg_fd} <> $fifo
>       rm $fifo
>       function _zle_msg_process() {
>         emulate -L zsh
>         while IFS='' read -r -u $zle_msg_fd line && zle -M $line
>       }
>       zle -F $zle_msg_fd _zle_msg_process
>     }
>
> Roman.
>
> On Sat, Jun 22, 2019 at 7:18 PM Sebastian Gniazdowski
> <sgniazdowski@gmail.com> wrote:
> >
> > Hello,
> > I've forged as I think an interesting function, which will display
> > given message under the prompt regardless if ran from within zle or
> > not. It uses the exec <(...) / zle -F trick to dispatch a callback
> > that does have the zle active (zle -F -w isn't needed, zle -M works
> > fine from no-widget callback):
> >
> > # deploy-message Hello world
> > # deploy-message @sleep:5.5 "Hello world"
> > deploy-message() {
> >     [[ "$1" = <-> && ${#} -eq 1 ]] && { zle && {
> >             local alltext text IFS=$'\n' nl=$'\n'
> >             repeat 25; do read -u"$1" text; alltext+="${text:+$text$nl}"; done
> >             [[ -n "$alltext" ]] && zle -M "$alltext"
> >         }
> >         zle -F "$1"; exec {1}<&-
> >         return 0
> >     }
> >     local THEFD
> >     # The expansion is: if there is @sleep: pfx, then use what's after
> >     # it, otherwise substitute 0
> >     exec {THEFD} < <(LANG=C sleep $(( 0.01 +
> > ${${${(M)1#@sleep:}:+${1#@sleep:}}:-0} )); print -r --
> > ${1:#(@msg|@sleep:*)} "${@[2,-1]}")
> >     zle -F "$THEFD" -deploy-message
> > }
> >
> > There's a gist with the function as well, in case of any updates:
> > https://gist.github.com/psprint/b6503c0c37de483f68d055e9c47981b3
> > --
> > Sebastian Gniazdowski

-- 
Sebastian Gniazdowski
News: https://twitter.com/ZdharmaI
IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin
Blog: http://zdharma.org

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

* Re: A function to display message under the prompt without zle active
  2019-06-22 20:49   ` Sebastian Gniazdowski
@ 2019-06-22 21:17     ` Roman Perepelitsa
  2019-06-22 21:19       ` Roman Perepelitsa
  2019-06-22 21:31       ` Sebastian Gniazdowski
  0 siblings, 2 replies; 6+ messages in thread
From: Roman Perepelitsa @ 2019-06-22 21:17 UTC (permalink / raw)
  To: Sebastian Gniazdowski; +Cc: Zsh Users

On Sat, Jun 22, 2019 at 10:49 PM Sebastian Gniazdowski
<sgniazdowski@gmail.com> wrote:
>
> The command line seems to be blcoked after first using the descriptor.

Silly bugs. This isn't production code, I just typed it into GMail to
give an idea. Removing `while` should fix it. There is also a missing
`local` and quotes. With these fixes in place:

    function _zle_msg_process() {
      emulate -L zsh
      local line
      IFS='' read -r -u $zle_msg_fd line && zle -M $line
    }

The rest of the code stays the same. The nice thing about this
implementation is that it's fork-free.

Depending on your needs you might also want to open the file descriptor
with `sysopen -rw -o cloexec`.

Note that zle will hang if you write something to the file descriptor
without a terminating \n.

Roman.

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

* Re: A function to display message under the prompt without zle active
  2019-06-22 21:17     ` Roman Perepelitsa
@ 2019-06-22 21:19       ` Roman Perepelitsa
  2019-06-22 21:31       ` Sebastian Gniazdowski
  1 sibling, 0 replies; 6+ messages in thread
From: Roman Perepelitsa @ 2019-06-22 21:19 UTC (permalink / raw)
  To: Sebastian Gniazdowski; +Cc: Zsh Users

On Sat, Jun 22, 2019 at 11:17 PM Roman Perepelitsa
<roman.perepelitsa@gmail.com> wrote:
>       IFS='' read -r -u $zle_msg_fd line && zle -M $line

I've lied about adding the missing quotes. $line should be "$line".

Roman.

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

* Re: A function to display message under the prompt without zle active
  2019-06-22 21:17     ` Roman Perepelitsa
  2019-06-22 21:19       ` Roman Perepelitsa
@ 2019-06-22 21:31       ` Sebastian Gniazdowski
  1 sibling, 0 replies; 6+ messages in thread
From: Sebastian Gniazdowski @ 2019-06-22 21:31 UTC (permalink / raw)
  To: Roman Perepelitsa; +Cc: Zsh Users

On Sat, 22 Jun 2019 at 23:17, Roman Perepelitsa
<roman.perepelitsa@gmail.com> wrote:
>
> On Sat, Jun 22, 2019 at 10:49 PM Sebastian Gniazdowski
> <sgniazdowski@gmail.com> wrote:
> >
> > The command line seems to be blcoked after first using the descriptor.
>
> Silly bugs. This isn't production code, I just typed it into GMail to
> give an idea. Removing `while` should fix it. There is also a missing
> `local` and quotes. With these fixes in place:
>
>     function _zle_msg_process() {
>       emulate -L zsh
>       local line
>       IFS='' read -r -u $zle_msg_fd line && zle -M $line
>     }
>
> The rest of the code stays the same. The nice thing about this
> implementation is that it's fork-free.

Thanks for the code, this distinct property might get handy in some situations.

> Depending on your needs you might also want to open the file descriptor
> with `sysopen -rw -o cloexec`.
>
> Note that zle will hang if you write something to the file descriptor
> without a terminating \n.
>
> Roman.

-- 
Sebastian Gniazdowski
News: https://twitter.com/ZdharmaI
IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin
Blog: http://zdharma.org

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

end of thread, back to index

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-06-22 17:17 A function to display message under the prompt without zle active Sebastian Gniazdowski
2019-06-22 17:45 ` Roman Perepelitsa
2019-06-22 20:49   ` Sebastian Gniazdowski
2019-06-22 21:17     ` Roman Perepelitsa
2019-06-22 21:19       ` Roman Perepelitsa
2019-06-22 21:31       ` Sebastian Gniazdowski

zsh-users

Archives are clonable: git clone --mirror http://inbox.vuxu.org/zsh-users

Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.users


AGPL code for this site: git clone https://public-inbox.org/ public-inbox