Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

putStr in compiled binary from GHC, Mac OS X

With this file:

main = do
  putStr "Input: "
  s <- getLine
  putStr s

It does what I want in GHCi, which is to put the prompt and then allow input right there on the same line as the prompt. If I compile it and run the executable in the terminal I won't see the prompt until after I do my input. Something about the new lines. I'm using Mac OS 10.8.5, GHC 7.4.2.

Is there a terminal setting or GHC option that I need to switch to get the behavior I want from the executable?

like image 922
user2744010 Avatar asked Feb 15 '23 15:02

user2744010


2 Answers

You need to use hSetBuffering from System.IO

main = do
    hSetBuffering stdout NoBuffering
    putStr "Input: "
    s <- getLine
    putStr s
like image 79
bheklilr Avatar answered Feb 17 '23 05:02

bheklilr


For completeness: You could also do hFlush stdout to explicitly flush the partial line to file.

But yes, for your scenario, the accepted answer is almost certainly the most sensible way.

like image 29
MathematicalOrchid Avatar answered Feb 17 '23 05:02

MathematicalOrchid