5

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?

6
  • 1
    ncurses or some higher-level progressbar library like this will do, I think. Commented Nov 20, 2015 at 1:24
  • I've also heard very good things about brick. Commented Nov 20, 2015 at 1:25
  • 2
    Just print a CR - see this SO question: stackoverflow.com/q/31900566/866915 Commented Nov 20, 2015 at 1:38
  • @fjarri ascii-progress is nice! Commented Nov 20, 2015 at 2:01
  • @HaoLian brick seems too much for my needs Commented Nov 20, 2015 at 2:02

2 Answers 2

11

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!"
Sign up to request clarification or add additional context in comments.

Comments

3

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/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.