Skip to content

Commit

Permalink
Flush stdout
Browse files Browse the repository at this point in the history
This ensures that standard output is not buffered (which it is by
default). Buffering causes the calls to `putStr*` print after the user
has already been prompted for input which is a confusing experience.

There are performance implications when turning off buffering entirely
as I'm doing here, however, they're negligible for such a small program.
  • Loading branch information
mmwtsn committed Mar 13, 2016
1 parent 83902ab commit 7bc973f
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Prompt.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Prompt where

import System.Console.ANSI
import System.IO

-- Default text formatting when user is prompted for input
promptColor :: System.Console.ANSI.SGR
Expand All @@ -12,7 +13,8 @@ resetColor = setSGR [Reset]

-- Prompt user for a line of input with a given prompt string and formatting
getTask :: System.Console.ANSI.SGR -> String -> IO String
getTask sgr prompt = setSGR [sgr] >>
getTask sgr prompt = hSetBuffering stdout NoBuffering >>
setSGR [sgr] >>
putStr prompt >>
resetColor >>
getLine
Expand Down

0 comments on commit 7bc973f

Please sign in to comment.