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

Re: Detect typed input without swallowing characters



On Fri, 2022-02-11 at 10:08 +0100, Marc Cornellà wrote:
> On Fri, 11 Feb 2022 at 01:04, Philippe Troin <phil@xxxxxxxx> 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.




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