Skip to content

Commit

Permalink
ENH Avoid flushing stdout too often
Browse files Browse the repository at this point in the history
Also, do not set stdout to NoBuffering, instead use hFlush as needed.
  • Loading branch information
luispedro committed Jun 17, 2019
1 parent 80f8b66 commit 3f22340
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions NGLess/Utils/ProgressBar.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@ updateProgressBar bar _
| not (pbarActive bar) = return bar
updateProgressBar bar progress = do
when (percent progress /= percent (cur bar)) $ do
let s = drawProgressBar (width bar) progress ++ " " ++ printPercentage progress
putStr (pbarName bar)
putStr ": "
putStr s
putStr "\r"
let pmessage = drawProgressBar (width bar) progress ++ " " ++ printPercentage progress
m = pbarName bar ++ pmessage ++ "\r"
putStr m
hFlush stdout
return $ bar { cur = progress }

-- | create a new 'ProgressBar' object
Expand All @@ -44,11 +43,9 @@ updateProgressBar bar progress = do
mkProgressBar :: String -> Int -> IO ProgressBar
mkProgressBar name w = do
isTerm <- hIsTerminalDevice stdout
if isTerm
then do
hSetBuffering stdout NoBuffering
return $ ProgressBar name (-1) w True
else return noProgress
return $! if isTerm
then ProgressBar name (-1) w True
else noProgress

drawProgressBar :: Int -> Rational -> String
drawProgressBar w progress =
Expand Down

0 comments on commit 3f22340

Please sign in to comment.