From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.3 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 21738 invoked from network); 11 Feb 2022 16:17:38 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 11 Feb 2022 16:17:38 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1644596258; b=FJSQW4lxqmJWwgot5tmRW/HWaUysvN+f2zN2HPhuFJkzkKgZKT5yrWB9MC81pNqwRmpfPcmPum GtiWjHoLlYYAKZxpbtzv2AHIvUsvIxwEfDkL5exypYjnG3DQykgys9OAuSaQ/yHetaAknYoM3r 4sSFoWwQQaO/CaBezId4ui/hSdjZWZKHvOmQ6GzA3er/5CtMz8OsbOIsuDpzdT8PFS9M0Gh2E+ 38u3nTHgu88PWIczVCDxqDKK5Xgx4U6pwVK3+Q6Un34Tn/nWsmTfKl0mmDN1eXO6oM7FuQ6ejc AkdK510K6JDgJNP0/mOfHNNN+9VZwdzhsrvwhUg7qEkYTA==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (granite.fifsource.com) smtp.remote-ip=173.255.216.206; dmarc=none header.from=fifi.org; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20210803; t=1644596258; bh=kq16f6l1xvgSlscsARYPVn6JGYMwK//5Z6jV72sxp0Y=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:Content-Transfer-Encoding:MIME-Version:Content-Type: References:In-Reply-To:Date:Cc:To:From:Subject:Message-ID:DKIM-Signature; b=iPEF/lrfFXUrYi8SDJW147/6h1mvOsbU1pRO1tpRLfzWPIzT5hAHAJK/gWKgTGJxO7tO38IvIe xouWKMacgB7qZSV2kSCT2wgT+fDA5uyhhRuPYe2w/LnOOexlYvWHv0gMig89p0lKVxeqmnK8tL 0HWMKzqeWH5svwFFax7q1+sFU56Ov2MqNJGIJsKJtyjfQiYISIZsSYBdOqq7PVW3iDZrUT1JXb /mbKuO8uPg9w9aCWDrs0Wh29oUx2rQ/mWvWKef3qb5Xq/diQyMGo0a0fjSNHdVtPMxCucHRe0b 2zQ1BNBlKqmxZOweFqmFALMobJzFU8ULt23pr6/ATQfn/Q==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20210803; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:Content-Transfer-Encoding: MIME-Version:Content-Type:References:In-Reply-To:Date:Cc:To:From:Subject: Message-ID:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=gPL2xrWAiQ29VrfFqDWFW/kNG1tupt1CJrqzFh+RUCM=; b=n/tujRFZ6SUS6/JE2E/tdTGr3h NWqnEMqV0TZVI+KYDtKtOErBTDlOC3YvnrXYpZFVP+8YjuIk9cYjr4bh5vgm1ixrrjQkb/PolvQt0 14BZZmxjaLSQZID5+mmmlvUDaRLd3mfuh10RW39LETOuku3v/8x2GKC624P2zx3/vZKvN/BJruh2e cbgx4VJpZF1uLtsKm8z8z3jvECaAbOKIsgKTc009FyPZFG4XqHsxcJ+TzESzW1AprETjZT6xHT5Ti klT2nSJIT6vZkxkjo5PuXGLgXxYfOayUkZE+laiO0RqMq7sSIyc6zCSlmYiKz3T12h4ShzOKDG2b2 lBouKdVQ==; Received: from authenticated user by zero.zsh.org with local id 1nIYc5-000BhL-5t; Fri, 11 Feb 2022 16:17:37 +0000 Authentication-Results: zsh.org; iprev=pass (granite.fifsource.com) smtp.remote-ip=173.255.216.206; dmarc=none header.from=fifi.org; arc=none Received: from granite.fifsource.com ([173.255.216.206]:48814) by zero.zsh.org with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) id 1nIYaL-000AwT-OV; Fri, 11 Feb 2022 16:15:54 +0000 Received: from ceramic.fifi.org (107-142-44-66.lightspeed.sntcca.sbcglobal.net [107.142.44.66]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by granite.fifsource.com (Postfix) with ESMTPSA id 213CE4073; Fri, 11 Feb 2022 08:15:48 -0800 (PST) Message-ID: <23bcea0b7708755d09f1ecfc71619ab8651de55b.camel@fifi.org> Subject: Re: Detect typed input without swallowing characters From: Philippe Troin To: Marc =?ISO-8859-1?Q?Cornell=E0?= Cc: zsh-users@zsh.org Date: Fri, 11 Feb 2022 08:15:47 -0800 In-Reply-To: References: <24180172015e6c9c3bab278c840eabcaa1a3249f.camel@fifi.org> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.42.3 (3.42.3-1.fc35) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Seq: 27514 Archived-At: X-Loop: zsh-users@zsh.org Errors-To: zsh-users-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-users-request@zsh.org X-no-archive: yes List-Id: List-Help: List-Subscribe: List-Unsubscribe: List-Post: List-Owner: List-Archive: On Fri, 2022-02-11 at 10:08 +0100, Marc Cornellà wrote: > On Fri, 11 Feb 2022 at 01:04, Philippe Troin 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.