Skip to content

Commit c45c9c6

Browse files
committed
standardIOConfig: flush unused input from stdin just before initializing Vty, when stdin is the Vty input source (fixes #266)
1 parent 72fc3dc commit c45c9c6

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/Graphics/Vty/Config.hs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ import Prelude
111111
import Control.Applicative hiding (many)
112112

113113
import Control.Exception (catch, IOException, Exception(..), throwIO)
114-
import Control.Monad (liftM, guard, void)
114+
import Control.Monad (liftM, guard, void, when)
115115

116116
import qualified Data.ByteString as BS
117117
#if !(MIN_VERSION_base(4,8,0))
@@ -132,6 +132,7 @@ import System.Directory ( getAppUserDataDirectory, doesFileExist
132132
)
133133
import System.Environment (lookupEnv)
134134
import System.FilePath ((</>), takeDirectory)
135+
import System.IO (Handle, BufferMode(..), hReady, hGetBuffering, hSetBuffering, hGetChar, stdin)
135136
import System.Posix.IO (stdInput, stdOutput)
136137
import System.Posix.Types (Fd(..))
137138
import Foreign.C.Types (CInt(..), CChar(..))
@@ -293,6 +294,7 @@ standardIOConfig = do
293294
Nothing -> throwIO VtyMissingTermEnvVar
294295
Just t -> do
295296
mcolorMode <- detectColorMode t
297+
flushInput stdin
296298
return defaultConfig
297299
{ vmin = Just 1
298300
, mouseMode = Just False
@@ -486,3 +488,21 @@ addConfigWidthMap configPath term tablePath = do
486488
Nothing -> do
487489
appendFile configPath directive
488490
return ConfigurationModified
491+
492+
flushInput :: Handle -> IO ()
493+
flushInput h = do
494+
mode <- hGetBuffering h
495+
hSetBuffering h NoBuffering
496+
whileM $ consume h
497+
hSetBuffering h mode
498+
499+
whileM :: (Monad m) => m Bool -> m ()
500+
whileM act = do
501+
continue <- act
502+
when continue $ whileM act
503+
504+
consume :: Handle -> IO Bool
505+
consume h = do
506+
avail <- hReady h
507+
when avail $ void $ hGetChar h
508+
return avail

0 commit comments

Comments
 (0)