unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Read characters from without newline
@ 2021-08-28 14:09 Zelphir Kaltstahl
  2021-08-28 14:52 ` Mike Gran
  2021-08-28 15:19 ` Chris Vine
  0 siblings, 2 replies; 6+ messages in thread
From: Zelphir Kaltstahl @ 2021-08-28 14:09 UTC (permalink / raw)
  To: Guile User

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.

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?

Best regards,
Zelphir

-- 
repositories: https://notabug.org/ZelphirKaltstahl




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Read characters from without newline
  2021-08-28 14:09 Read characters from without newline Zelphir Kaltstahl
@ 2021-08-28 14:52 ` Mike Gran
  2021-08-28 20:56   ` Frank Terbeck
  2021-08-28 15:19 ` Chris Vine
  1 sibling, 1 reply; 6+ messages in thread
From: Mike Gran @ 2021-08-28 14:52 UTC (permalink / raw)
  To: Zelphir Kaltstahl; +Cc: Guile User

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





^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Read characters from without newline
  2021-08-28 14:09 Read characters from without newline Zelphir Kaltstahl
  2021-08-28 14:52 ` Mike Gran
@ 2021-08-28 15:19 ` Chris Vine
  1 sibling, 0 replies; 6+ messages in thread
From: Chris Vine @ 2021-08-28 15:19 UTC (permalink / raw)
  To: guile-user

On Sat, 28 Aug 2021 14:09:52 +0000
Zelphir Kaltstahl <zelphirkaltstahl@posteo.de> 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.
> 
> 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





^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Read characters from without newline
  2021-08-28 14:52 ` Mike Gran
@ 2021-08-28 20:56   ` Frank Terbeck
  2021-08-28 23:47     ` Tim Meehan
  0 siblings, 1 reply; 6+ messages in thread
From: Frank Terbeck @ 2021-08-28 20:56 UTC (permalink / raw)
  To: Mike Gran; +Cc: Guile User

Mike Gran wrote:
[…]
> 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.

Shameless plug: https://gitlab.com/ft/guile-termios

I sort of have this ready for guix, too. But I never got around to actu-
ally submitting it.


Regards, Frank
-- 
In protocol design, perfection has been reached not when there is
nothing left to add, but when there is nothing left to take away.
                                                  -- RFC 1925



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Read characters from without newline
  2021-08-28 20:56   ` Frank Terbeck
@ 2021-08-28 23:47     ` Tim Meehan
  2021-08-29 10:07       ` Hans-Werner Roitzsch
  0 siblings, 1 reply; 6+ messages in thread
From: Tim Meehan @ 2021-08-28 23:47 UTC (permalink / raw)
  Cc: Guile User

Second for guile-termios, I use it and like it very much (saved my bacon
with a program I was writing).

On Sat, Aug 28, 2021 at 3:56 PM Frank Terbeck <ft@bewatermyfriend.org>
wrote:

> Mike Gran wrote:
> […]
> > 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.
>
> Shameless plug: https://gitlab.com/ft/guile-termios
>
> I sort of have this ready for guix, too. But I never got around to actu-
> ally submitting it.
>
>
> Regards, Frank
> --
> In protocol design, perfection has been reached not when there is
> nothing left to add, but when there is nothing left to take away.
>                                                   -- RFC 1925
>
>


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Read characters from without newline
  2021-08-28 23:47     ` Tim Meehan
@ 2021-08-29 10:07       ` Hans-Werner Roitzsch
  0 siblings, 0 replies; 6+ messages in thread
From: Hans-Werner Roitzsch @ 2021-08-29 10:07 UTC (permalink / raw)
  To: Tim Meehan, Mike Gran, Chris Vine, ft; +Cc: Guile User

Thank you all for your pointers, hints and solutions!

I shall try them out : )

Best regards,
Zelphir

On 8/29/21 1:47 AM, Tim Meehan wrote:
> Second for guile-termios, I use it and like it very much (saved my bacon
> with a program I was writing).
>
> On Sat, Aug 28, 2021 at 3:56 PM Frank Terbeck <ft@bewatermyfriend.org>
> wrote:
>
>> Mike Gran wrote:
>> […]
>>> 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.
>> Shameless plug: https://gitlab.com/ft/guile-termios
>>
>> I sort of have this ready for guix, too. But I never got around to actu-
>> ally submitting it.
>>
>>
>> Regards, Frank
>> --
>> In protocol design, perfection has been reached not when there is
>> nothing left to add, but when there is nothing left to take away.
>>                                                   -- RFC 1925
>>
>>



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-08-29 10:07 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-28 14:09 Read characters from without newline Zelphir Kaltstahl
2021-08-28 14:52 ` Mike Gran
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

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).