How to prevent inputs being flushed into output?
Asked Answered
B

1

6

I'm on Ubuntu. When I run ghci on Terminal and do this:

Prelude Control.Monad System.IO> forever $ getChar >>= print

The result is something like this:

a'a'
b'b'
C'C'
%'%'
\'\\'
1'1'
''\''
"'"'
^X'\CAN'
^?'\DEL'
^CInterrupted.

That is, the characters I type in my keyboard are being flushed into output. How can I prevent this and have only print as the writer?

Brunelle answered 10/8, 2019 at 23:32 Comment(0)
I
9

To prevent input being flushed into the output (or "echoed"), use hSetEcho stdin False.

Prelude> import System.IO
Prelude System.IO> import Control.Monad
Prelude System.IO Control.Monad> hSetEcho stdin False
Prelude System.IO Control.Monad> forever $ getChar >>= print
'a'
'\n'
'b'
'c'

This can be used to do things like read in a password.

Impeller answered 10/8, 2019 at 23:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.