@@ -111,7 +111,7 @@ import Prelude
111
111
import Control.Applicative hiding (many )
112
112
113
113
import Control.Exception (catch , IOException , Exception (.. ), throwIO )
114
- import Control.Monad (liftM , guard , void )
114
+ import Control.Monad (liftM , guard , void , when )
115
115
116
116
import qualified Data.ByteString as BS
117
117
#if !(MIN_VERSION_base(4,8,0))
@@ -132,6 +132,7 @@ import System.Directory ( getAppUserDataDirectory, doesFileExist
132
132
)
133
133
import System.Environment (lookupEnv )
134
134
import System.FilePath ((</>) , takeDirectory )
135
+ import System.IO (Handle , BufferMode (.. ), hReady , hGetBuffering , hSetBuffering , hGetChar , stdin )
135
136
import System.Posix.IO (stdInput , stdOutput )
136
137
import System.Posix.Types (Fd (.. ))
137
138
import Foreign.C.Types (CInt (.. ), CChar (.. ))
@@ -293,6 +294,7 @@ standardIOConfig = do
293
294
Nothing -> throwIO VtyMissingTermEnvVar
294
295
Just t -> do
295
296
mcolorMode <- detectColorMode t
297
+ flushInput stdin
296
298
return defaultConfig
297
299
{ vmin = Just 1
298
300
, mouseMode = Just False
@@ -486,3 +488,21 @@ addConfigWidthMap configPath term tablePath = do
486
488
Nothing -> do
487
489
appendFile configPath directive
488
490
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