Hello!
I have been trying to get characters on the console without the need to press enter using the |read-char| function. The section 17.4.1 of Gambit's manual explains that setting the property *buffering *of a port should be enough to get chars without #\newline. But I wasn't able to make it work with |current-input-port| or |console-port|.
(define (getchar) (port-settings-set! (current-input-port) (list buffering: #f)) (let loop () (write-char (read-char (current-input-port))) (loop)))
I have been told that one option is to change my terminal settings to raw mode. I have also tried to use *telnet* and I was able to make it work properly with the client on *mode character*.
But, as far as I understand, there should be no need of *telnet* or raw terminal mode. What am I missing here?
Thanks in advance, Phil
Afficher les réponses par date
The buffering setting has to do with buffering within the Scheme process, whereas the tty setting has to do with buffering within the operating system. In order to get single-character reads from the terminal, you have to turn off both.
On Wed, Sep 4, 2019 at 10:22 AM Phillip Suero philsuero@gmail.com wrote:
Hello!
I have been trying to get characters on the console without the need to press enter using the |read-char| function. The section 17.4.1 of Gambit's manual explains that setting the property *buffering *of a port should be enough to get chars without #\newline. But I wasn't able to make it work with |current-input-port| or |console-port|.
(define (getchar) (port-settings-set! (current-input-port) (list buffering: #f)) (let loop () (write-char (read-char (current-input-port))) (loop)))
I have been told that one option is to change my terminal settings to raw mode. I have also tried to use *telnet* and I was able to make it work properly with the client on *mode character*.
But, as far as I understand, there should be no need of *telnet* or raw terminal mode. What am I missing here?
Thanks in advance, Phil _______________________________________________ Gambit-list mailing list Gambit-list@iro.umontreal.ca https://mailman.iro.umontreal.ca/cgi-bin/mailman/listinfo/gambit-list
Marc
On Sep 4, 2019, at 11:34 AM, John Cowan cowan@ccil.org wrote:
The buffering setting has to do with buffering within the Scheme process, whereas the tty setting has to do with buffering within the operating system. In order to get single-character reads from the terminal, you have to turn off both.
Indeed.
The tty settings can be changed by calling tty-mode-set! as shown below.
Marc
;;; File: console-read-one-char-at-a-time.scm
;; run like this: ;; ;; gsi console-read-one-char-at-a-time.scm
(define (setup port) (if (tty? port) ;; change the tty settings to read raw characters 1 at a time (tty-mode-set! port #f ;; input-allow-special #f ;; input-echo #t ;; input-raw #f ;; output-raw 0))) ;; speed
(define port (current-input-port))
(setup port)
(let loop () (let ((c (read-char port))) (pp (list 'typed c)) (loop)))
It's probably best to use dynamic-wind, turning the settings on in the before-thunk and off in the after-thunk, so you don't wind up with the terminal set wrongly when the process terminates, like this:
(dynamic-wind (lambda () (setup-port port)) (let loop ...) (lambda () (restore-port port))))
On Wed, Sep 4, 2019 at 12:05 PM Marc Feeley feeley@iro.umontreal.ca wrote:
Marc
On Sep 4, 2019, at 11:34 AM, John Cowan cowan@ccil.org wrote:
The buffering setting has to do with buffering within the Scheme
process, whereas the tty setting has to do with buffering within the operating system. In order to get single-character reads from the terminal, you have to turn off both.
Indeed.
The tty settings can be changed by calling tty-mode-set! as shown below.
Marc
;;; File: console-read-one-char-at-a-time.scm
;; run like this: ;; ;; gsi console-read-one-char-at-a-time.scm
(define (setup port) (if (tty? port) ;; change the tty settings to read raw characters 1 at a time (tty-mode-set! port #f ;; input-allow-special #f ;; input-echo #t ;; input-raw #f ;; output-raw 0))) ;; speed
(define port (current-input-port))
(setup port)
(let loop () (let ((c (read-char port))) (pp (list 'typed c)) (loop)))
On Wed, Sep 04, 2019 at 12:18:41PM -0400, John Cowan wrote:
It's probably best to use dynamic-wind, turning the settings on in the before-thunk and off in the after-thunk, so you don't wind up with the terminal set wrongly when the process terminates, like this:
(dynamic-wind (lambda () (setup-port port)) (let loop ...) (lambda () (restore-port port))))
Is there a function like tty-mode-set! that reports on the current mode-setting so that it can be restored later?
-- hendrik
On Wed, Sep 4, 2019 at 12:05 PM Marc Feeley feeley@iro.umontreal.ca wrote:
Marc
On Sep 4, 2019, at 11:34 AM, John Cowan cowan@ccil.org wrote:
The buffering setting has to do with buffering within the Scheme
process, whereas the tty setting has to do with buffering within the operating system. In order to get single-character reads from the terminal, you have to turn off both.
Indeed.
The tty settings can be changed by calling tty-mode-set! as shown below.
Marc
;;; File: console-read-one-char-at-a-time.scm
;; run like this: ;; ;; gsi console-read-one-char-at-a-time.scm
(define (setup port) (if (tty? port) ;; change the tty settings to read raw characters 1 at a time (tty-mode-set! port #f ;; input-allow-special #f ;; input-echo #t ;; input-raw #f ;; output-raw 0))) ;; speed
(define port (current-input-port))
(setup port)
(let loop () (let ((c (read-char port))) (pp (list 'typed c)) (loop)))
Gambit-list mailing list Gambit-list@iro.umontreal.ca https://mailman.iro.umontreal.ca/cgi-bin/mailman/listinfo/gambit-list
Gambit’s runtime system automatically sets the tty mode to what it was when the tty was first used. So this is not needed.
However, if the program crashes (a situation that couldn’t be caught with dynamic-wind BTW) then I hope you can spell “reset” at the shell with no echo…
Marc
On Sep 4, 2019, at 12:23 PM, Hendrik Boom hendrik@topoi.pooq.com wrote:
On Wed, Sep 04, 2019 at 12:18:41PM -0400, John Cowan wrote:
It's probably best to use dynamic-wind, turning the settings on in the before-thunk and off in the after-thunk, so you don't wind up with the terminal set wrongly when the process terminates, like this:
(dynamic-wind (lambda () (setup-port port)) (let loop ...) (lambda () (restore-port port))))
Is there a function like tty-mode-set! that reports on the current mode-setting so that it can be restored later?
-- hendrik
On Wed, Sep 4, 2019 at 12:05 PM Marc Feeley feeley@iro.umontreal.ca wrote:
Marc
On Sep 4, 2019, at 11:34 AM, John Cowan cowan@ccil.org wrote:
The buffering setting has to do with buffering within the Scheme
process, whereas the tty setting has to do with buffering within the operating system. In order to get single-character reads from the terminal, you have to turn off both.
Indeed.
The tty settings can be changed by calling tty-mode-set! as shown below.
Marc
;;; File: console-read-one-char-at-a-time.scm
;; run like this: ;; ;; gsi console-read-one-char-at-a-time.scm
(define (setup port) (if (tty? port) ;; change the tty settings to read raw characters 1 at a time (tty-mode-set! port #f ;; input-allow-special #f ;; input-echo #t ;; input-raw #f ;; output-raw 0))) ;; speed
(define port (current-input-port))
(setup port)
(let loop () (let ((c (read-char port))) (pp (list 'typed c)) (loop)))
Gambit-list mailing list Gambit-list@iro.umontreal.ca https://mailman.iro.umontreal.ca/cgi-bin/mailman/listinfo/gambit-list
Gambit-list mailing list Gambit-list@iro.umontreal.ca https://mailman.iro.umontreal.ca/cgi-bin/mailman/listinfo/gambit-list