-
-
Save judah/5c5ad518bb43d235efba4fa8f1e9953a to your computer and use it in GitHub Desktop.
Simulate terminal reading
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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