From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Chris Vine Newsgroups: gmane.lisp.guile.user Subject: Re: Read characters from without newline Date: Sat, 28 Aug 2021 16:19:05 +0100 Message-ID: <20210828161905.fbee4ddcd1d46240a2b82a12@gmail.com> References: <3ea2d7b2-019e-a226-9166-34edaec52fd4@posteo.de> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="8185"; mail-complaints-to="usenet@ciao.gmane.io" To: guile-user@gnu.org Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Sat Aug 28 17:18:28 2021 Return-path: Envelope-to: guile-user@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mK06F-0001v2-Sr for guile-user@m.gmane-mx.org; Sat, 28 Aug 2021 17:18:27 +0200 Original-Received: from localhost ([::1]:49904 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mK06E-0002fo-Fd for guile-user@m.gmane-mx.org; Sat, 28 Aug 2021 11:18:26 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:36960) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mK063-0002fg-M4 for guile-user@gnu.org; Sat, 28 Aug 2021 11:18:15 -0400 Original-Received: from mail-wm1-x329.google.com ([2a00:1450:4864:20::329]:54986) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mK061-0006zF-Ui for guile-user@gnu.org; Sat, 28 Aug 2021 11:18:15 -0400 Original-Received: by mail-wm1-x329.google.com with SMTP id g138so5813437wmg.4 for ; Sat, 28 Aug 2021 08:18:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:subject:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=e95knKwjFiaQ+jSWBuwE0r3jteP6k+rNWsNBJyR0I7g=; b=GlbVDNALlo/hl+kqVmqaOQfCZyzmyq4xBnMAGPebKVm0pn7r2mjx+qMcs1FH+I1/fw dmrmeo7LUfuXf2KAczeYu0yA8NZssMFgTA+ivcJu4pIWrxDDGzCYvBuGpelGifvPGKWb VuQL70jhtFPLStCZhNQtUsnYexjhjXeOLFq2zvUw3YEAG+afJuD+PEKy1B5eBBALWVJu KZcFf68iPxXEsXX/pQ7DzDLom8lzWhhWm4mequQDbc1OV4uKIpJoTBsVjb4iSCQt/hM2 5CVKtBXr2qE92Arj2Myslsri5WKeq1V32eKkuaqYvZW7OnFS9aN8hcJ5U1jqJR9Vrllp K4rA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:subject:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=e95knKwjFiaQ+jSWBuwE0r3jteP6k+rNWsNBJyR0I7g=; b=AqeNu04isUfiA1L717v6cv8sZ9Um3F4dHU6suC4u1wMk6SFj0206JZGmH+1dVK06sf HsiAgn58eY4uGJuj3QzO6F/lRC5d7lKXIbTTrmyqTCC8jMFbHpKNcIr86goI0UliUGhs BwDf2TykJx00LhyDVRnGp3m2GW7fs8iyhDSOsvl6/rlDC+fOtCS1AqvEDdIQmRaMXkHx TY9WHVNvU1hGxNb5+Y7G6MiRSwW1cNn6EP30lkiQcVSxVoojDXBN23o8J+Nqrw3kQpWg Ncd2v8Fx/RsTLnohHqDXU9sL4fwVjWDRA2TXF/pU5SsvfPfoI3I0So09Q/HDOvMI5hO7 J8jg== X-Gm-Message-State: AOAM533Lx238sU7XnhLWNdh2psvLy8/CGO1y8Kj5/93MGoq3MthH3c/w kwOM9mn75xJ6tPIWj8q36w4KhB14nTY= X-Google-Smtp-Source: ABdhPJw6peKXiCWfYMH73GLz9uK1o2GzvbhhDnLl9fFRlvpp9Y0OQgLXIBXsbM9lWE9+1amcDLwFxw== X-Received: by 2002:a1c:cc03:: with SMTP id h3mr24215739wmb.73.1630163892125; Sat, 28 Aug 2021 08:18:12 -0700 (PDT) Original-Received: from bother.homenet ([2.27.184.161]) by smtp.gmail.com with ESMTPSA id w18sm10722521wrg.68.2021.08.28.08.18.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Aug 2021 08:18:11 -0700 (PDT) Original-Received: from bother.homenet (localhost [127.0.0.1]) by bother.homenet (Postfix) with SMTP id CFFDA268A63 for ; Sat, 28 Aug 2021 16:19:05 +0100 (BST) In-Reply-To: <3ea2d7b2-019e-a226-9166-34edaec52fd4@posteo.de> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.33; x86_64-unknown-linux-gnu) Received-SPF: pass client-ip=2a00:1450:4864:20::329; envelope-from=vine35792468@gmail.com; helo=mail-wm1-x329.google.com X-Spam_score_int: -22 X-Spam_score: -2.3 X-Spam_bar: -- X-Spam_report: (-2.3 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, NICE_REPLY_A=-0.437, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.io gmane.lisp.guile.user:17698 Archived-At: On Sat, 28 Aug 2021 14:09:52 +0000 Zelphir Kaltstahl wrote: > Hello Guile users, > > I am trying to find a way to read a character from command line or REPL, without > having to enter a newline, confirming the input. > > For example I found this for Python: https://stackoverflow.com/a/21659588 > > > I read on https://www.gnu.org/software/guile/manual/html_node/Buffering.html > about > buffering and thought, that I could simply set the port to be unbuffered using > (setvbuf port 'none) and then call (get-char port) as follows: > > ~~~~ > (import > (except (rnrs base) let-values map) > (only (guile) > ;; lambda forms > lambda* λ > ;; input output > current-input-port) > (ice-9 textual-ports) > (ice-9 optargs)) > > (let ([port (current-input-port)]) > (setvbuf port 'none) > (get-char port)) > ~~~~ > > However, this does not work. The terminal emulator might not send input > immediately to the Guile program. Even in the Guile REPL and in Emacs Geiser > this does not work. I guess it would work, if they did send each character > immediately to the Guile program, because the following works: > > ~~~~ > (display > (call-with-input-string "ab" > (λ (in-port) > (setvbuf in-port 'none) > (get-char in-port)))) > ~~~~ > > There is no newline in the string "ab" and only the "a" is displayed. > > So the question might be, how one can get into a mode, where Guile receives > input immediately, taking over control of the terminal or REPL. > > However, I am not sure how the Python solution does this. > > I have some command line program, in which I let the user choose what to do next > by entering a character and then pressing the return key to confirm the input. I > think it would be nice to not have to press return all the time and only needing > to press the character's key. > > Does anyone know how to do this in Guile? It's not guile doing the buffering, it's your terminal. By default your terminal is in canonical (aka "cooked") mode. This means amongst other things that input is not delivered from the keyboard on /dev/tty until a new line is entered. On unix-like operating systems you can change to non-canonical mode and set other terminal parameters using termios, or if you don't want to write some C you can just call up stty using system*. (system* "stty" "--file=/dev/tty" "cbreak") will probably do what you want, but you will then probably want to restore cooked mode when your program exits. This may also help you: https://en.wikibooks.org/wiki/Serial_Programming/termios