[gambit-list] How use (read-char) to read individual characters from the console?

Mikael mikael.rcv at gmail.com
Wed Jan 2 18:05:26 EST 2013


Here's a read-char suitable for console games etc. .

(define (nice-read-char)
   (tty-mode-set! (repl-input-port) #t #f #t #f 0)
   (let ((c (read-char)))
      (tty-mode-set! (repl-input-port) #t #t #f #f 0)
      (if (eq? c #\x) (raise "Escape to REPL")) ; CTRL-C replacement
      c))

A version 2 of it would be to make it handle ctrl+c appropriately,
currently that throws the console into a state as per the previous email.

2013/1/2 Mikael

> Aha neat!
>
> Just for reference to anyone who may want to use this to make a nice
> console-based Tetris etc., while in this tty mode repl-input-port *cannot*
> be used to drive the REPL. Though, it is ctrl+c sensitive.
>
> (
> Using the REPL in this mode does not do eval when pressing enter anymore,
> does not echo input, and when using any arrow keys shows:
>
> > *** ERROR IN (console)@8.1 -- Illegal character: #\esc
>
> )
>
> So here's a nice example of 'clean' use of this feature:
>
> > (begin (tty-mode-set! (repl-input-port) #t #f #t #f 0) (write
> (read-char)) (force-output) (write (read-char)) (force-output)(write
> (read-char)) (force-output)(write (read-char)) (force-output)(write
> (read-char)) (force-output)(write (read-char)) (force-output)
> (tty-mode-set! (repl-input-port) #t #t #f #f 0))
> #\newline#\esc#\[#\A#\esc#\[>
>
>
>
> Rather for completeness, how can add hooks to the switching of activity
> from the user code to Gambit REPL (on CTRL+C or exception) and back (by ,c)?
>
>
> By adding hooks to that that switch the TTY between read-char-friendly
> mode and REPL-friendly, people can also debug their console-based
> Tetris:es nicely.
>
> With warm regards,
> Mikael
>
> 2013/1/2 Marc Feeley <feeley at iro.umontreal.ca>
>
>> Mikael, you need to change the tty mode :
>>
>> > (tty-mode-set! (repl-input-port) #t #f #t #f 0)
>>
>> The booleans indicate various settings (ctrl-c handling, input echo, raw
>> input, raw output).
>>
>> On UNIX, this changes the terminal's line discipline.  On Windows, it
>> changes the console mode.
>>
>> Marc
>>
>>
>> On 2013-01-02, at 11:59 AM, Mikael <mikael.rcv at gmail.com> wrote:
>>
>> > Hi,
>> >
>> > In using read-char I get results only on a per input line basis. I was
>> trying to switch the input console to byte/character mode instead of line
>> buffered, though I didn't get any result with this.
>> >
>> > Any thought or suggestion?
>> >
>> > Mikael
>> >
>> > _______________________________________________
>> > Gambit-list mailing list
>> > Gambit-list at iro.umontreal.ca
>> > https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.iro.umontreal.ca/pipermail/gambit-list/attachments/20130103/09808124/attachment.htm>


More information about the Gambit-list mailing list