[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Read characters from without newline

From: Chris Vine
Subject: Re: Read characters from without newline
Date: Sat, 28 Aug 2021 16:19:05 +0100

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:
> <>
> I read on
> <> 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:

reply via email to

[Prev in Thread] Current Thread [Next in Thread]