Here's a read-char suitable for console games etc. .<br><div><br></div><span style="font-family:'courier new',monospace">(define (nice-read-char)</span><br><span style="font-family:'courier new',monospace">   (tty-mode-set! (repl-input-port) #t #f #t #f 0)</span><br>

<span style="font-family:'courier new',monospace">   (let ((c (read-char)))</span><br><span style="font-family:'courier new',monospace">      (tty-mode-set! (repl-input-port) #t #t #f #f 0)</span><br><span style="font-family:'courier new',monospace">      (if (eq? c #\x) (raise "Escape to REPL")) ; CTRL-C replacement</span><br>

<div><div><font face="courier new, monospace">      c))</font></div></div><div><br></div><div>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.</div>

<div><br><div class="gmail_quote">2013/1/2 Mikael<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>Aha neat!</div><div><br></div><div>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.</div>


<div><br></div><div>(</div><div>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:</div><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px">


<div><font face="courier new, monospace">> *** ERROR IN (console)@8.1 -- Illegal character: #\esc</font></div></blockquote><div>)</div><div><br></div><div>So here's a nice example of 'clean' use of this feature:</div>


<div><br></div><div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><div><font face="courier new, monospace">> (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))</font></div>


<div><font face="courier new, monospace">#\newline#\esc#\[#\A#\esc#\[></font></div></div></blockquote></div><div><br></div><div><br></div><div>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)?</div>


<div><br></div><div><br></div><div>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.</div><div><br></div><div>With warm regards,</div>


<div>Mikael</div><div class="HOEnZb"><div class="h5"><div><br><div class="gmail_quote">2013/1/2 Marc Feeley <span dir="ltr"><<a href="mailto:feeley@iro.umontreal.ca" target="_blank">feeley@iro.umontreal.ca</a>></span><br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Mikael, you need to change the tty mode :<br>
<br>
> (tty-mode-set! (repl-input-port) #t #f #t #f 0)<br>
<br>
The booleans indicate various settings (ctrl-c handling, input echo, raw input, raw output).<br>
<br>
On UNIX, this changes the terminal's line discipline.  On Windows, it changes the console mode.<br>
<br>
Marc<br>
<div><div><br>
<br>
On 2013-01-02, at 11:59 AM, Mikael <<a href="mailto:mikael.rcv@gmail.com" target="_blank">mikael.rcv@gmail.com</a>> wrote:<br>
<br>
> Hi,<br>
><br>
> 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.<br>
><br>
> Any thought or suggestion?<br>
><br>
> Mikael<br>
><br>
</div></div>> _______________________________________________<br>
> Gambit-list mailing list<br>
> <a href="mailto:Gambit-list@iro.umontreal.ca" target="_blank">Gambit-list@iro.umontreal.ca</a><br>
> <a href="https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list" target="_blank">https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list</a><br>
<br>
</blockquote></div><br></div>
</div></div></blockquote></div><br></div>