Skip to content

Instantly share code, notes, and snippets.

@judah

judah/Read.hs Secret

Created April 17, 2016 22:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save judah/5c5ad518bb43d235efba4fa8f1e9953a to your computer and use it in GitHub Desktop.
Save judah/5c5ad518bb43d235efba4fa8f1e9953a to your computer and use it in GitHub Desktop.
Simulate terminal reading
module Main where
import Control.Exception
import Control.Monad
import System.IO
main = bracket (hGetEcho stdin) (hSetEcho stdin) $ const $
bracket (hGetBuffering stdin) (hSetBuffering stdin) $ const $ do
hSetEcho stdin False
hSetBuffering stdin NoBuffering
h <- openFile "/dev/tty" ReadMode
forever $ getChars h >>= print
getChars h = do
c <- hGetChar h
loop [c]
where
loop cs = do
isReady <- hReady h
if not isReady
then return $ reverse cs
else do
c <- hGetChar h
loop (c : cs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment