unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Mike Gran <spk121@yahoo.com>
To: Zelphir Kaltstahl <zelphirkaltstahl@posteo.de>
Cc: Guile User <guile-user@gnu.org>
Subject: Re: Read characters from without newline
Date: Sat, 28 Aug 2021 07:52:21 -0700	[thread overview]
Message-ID: <YSpNpT1HFg120tYB@spikycactus.com> (raw)
In-Reply-To: <3ea2d7b2-019e-a226-9166-34edaec52fd4@posteo.de>

On Sat, Aug 28, 2021 at 02:09:52PM +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
> <https://stackoverflow.com/a/21659588>
> 
> I read on https://www.gnu.org/software/guile/manual/html_node/Buffering.html
> <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.

Hello. For this to work, your terminal needs to be in 'raw' mode
where it sends characters immediately.  Terminals are normally
in 'cooked' mode where it waits for a return before it sends everything.

The simple way to get into raw mode would be to run your script like

    stty raw && guile script.scm
    
But if you want to get the terminal into raw mode from within guile,
you need to get guile to call a function like 'tcsetattr' to set the
mode of your terminal. Or maybe just call the stty program from within
guile?

There is a binding of tcsetattr in the (ncurses extra) library
in guile-ncurses, but since you just need a couple of functions, you
could wrap them in FFI, I suppose.

-Mike





  reply	other threads:[~2021-08-28 14:52 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-28 14:09 Read characters from without newline Zelphir Kaltstahl
2021-08-28 14:52 ` Mike Gran [this message]
2021-08-28 20:56   ` Frank Terbeck
2021-08-28 23:47     ` Tim Meehan
2021-08-29 10:07       ` Hans-Werner Roitzsch
2021-08-28 15:19 ` Chris Vine

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

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YSpNpT1HFg120tYB@spikycactus.com \
    --to=spk121@yahoo.com \
    --cc=guile-user@gnu.org \
    --cc=zelphirkaltstahl@posteo.de \
    /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.
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 read-only IMAP folder(s) and NNTP newsgroup(s).