How to prevent inputs being flushed into output?

♀尐吖头ヾ 提交于 2019-12-14 00:32:14

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/57446365/how-to-prevent-inputs-being-flushed-into-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!