zsh-users
 help / color / mirror / code / Atom feed
From: Philippe Troin <phil@fifi.org>
To: "Marc Cornellà" <hello@mcornella.com>
Cc: zsh-users@zsh.org
Subject: Re: Detect typed input without swallowing characters
Date: Fri, 11 Feb 2022 08:15:47 -0800	[thread overview]
Message-ID: <23bcea0b7708755d09f1ecfc71619ab8651de55b.camel@fifi.org> (raw)
In-Reply-To: <CACn48No-sNDR84CjFgGmzeuOvRc7UMUwWm9TEcLgC0afFP=eQQ@mail.gmail.com>

On Fri, 2022-02-11 at 10:08 +0100, Marc Cornellà wrote:
> On Fri, 11 Feb 2022 at 01:04, Philippe Troin <phil@fifi.org> wrote:
> > 
> > You can use zselect, but you have to disable canonical mode with stty,
> > otherwise characters are not counted unless you press enter (the
> > terminal is set to read a whole line at once).
> > 
> > This function should work:
> > 
> >    type_ahead() {
> >      emulate -L zsh
> >      local termios=$(stty --save)
> >      stty -icanon
> >      zselect -t 0 -r 0
> >      local ret=$?
> >      stty $termios
> >      return ret
> >    }
> > 
> 
> It does work, thanks! I tried my best at using STTY to avoid having to
> reset it afterwards, but it didn't have the desired effect.
> See https://github.com/ohmyzsh/ohmyzsh/commit/dbd92a62ce1fc25a6819ae6d0a29dc8b8ec9a7dd

This is the implementation you've checked in (thank you for the
credit):

   function has_typed_input() {
     emulate -L zsh
     zmodload zsh/zselect
   
     {
       local termios=$(stty --save)
       stty -icanon
   
       zselect -t 0 -r 0
       return $?
     } always {
       stty $termios
     }
   }

Kudos on using an always block to ensure that the termios are reset on
exiting the function, but if for some reason you're not connected to a
terminal, or if 'stty --save' fails, you still end up called stty many
times (my original code had the same problem):

   % has_typed_input < /dev/null
   stty: 'standard input': Inappropriate ioctl for device
   stty: 'standard input': Inappropriate ioctl for device
   stty: 'standard input': Inappropriate ioctl for device
   % 
   
I'd suggest moving the termios assignment outside of the always block
and exiting early if it fails:

   function has_typed_input() {
     emulate -L zsh
     zmodload zsh/zselect
   
     local termios
     termios=$(stty --save) || return $?
     {
       stty -icanon
   
       zselect -t 0 -r 0
       return $?
     } always {
       stty $termios
     }
   }
   
   % has_typed_input < /dev/null
   stty: 'standard input': Inappropriate ioctl for device
   %
   
You may also want to remove the error message with 2> /dev/null:

   termios=$(stty --save 2> /dev/null) || return $?
   
Regarding your remark about STTY not working as expected: I suspect
assigning to STTY does not work for builtins.  If it worked, the
function could simply be replaced by:

   STTY=-icanon zselect -t 0 -r 0

Phil.


  reply	other threads:[~2022-02-11 16:17 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-10 21:21 Marc Cornellà
2022-02-11  0:04 ` Philippe Troin
2022-02-11  9:08   ` Marc Cornellà
2022-02-11 16:15     ` Philippe Troin [this message]
2022-02-11 19:08     ` Bart Schaefer

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=23bcea0b7708755d09f1ecfc71619ab8651de55b.camel@fifi.org \
    --to=phil@fifi.org \
    --cc=hello@mcornella.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).