Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic stdout in Haskell

I have simple command line interface with insertion records in DB and now it writes a lot of info to stdout, like this:

...    
record 856/1000: 85%
record 857/1000: 85%
record 858/1000: 85%
...

but I want to have 1 dynamic line with updating current string parameters

 status         |T    | C   | A   | E
 ---------------------------------------
   inserting    |1000 | 857 | 85% | 96  

How can I achieve that?

like image 522
Filip van Hoft Avatar asked Nov 20 '15 01:11

Filip van Hoft


2 Answers

If it's just one row, you can use \r to rewind the cursor to the beginning of the line.

Here's an example:

import Control.Concurrent
import Control.Monad
import Text.Printf

main :: IO ()
main = do
    forM_ [10, 9 .. 1] $ \seconds -> do
        printf "\rLaunching missiles in %2d..." (seconds :: Int)
        threadDelay $ 1 * 1000 * 1000
    putStrLn "\nBlastoff!"
like image 191
Lambda Fairy Avatar answered Oct 07 '22 19:10

Lambda Fairy


Joey Hess's concurrent-output library is designed for progress output like this (and more complex variations).

http://hackage.haskell.org/package/concurrent-output

https://joeyh.name/blog/entry/a_tiling_region_manager_for_the_console/

like image 29
drquicksilver Avatar answered Oct 07 '22 19:10

drquicksilver