module Test.Framework.Runners.Console.ProgressBar (
Progress(..), progressBar
) where
import Text.PrettyPrint.ANSI.Leijen ( (<>), char, text, Doc )
data Progress = Progress Int Int
progressBar :: (Doc -> Doc) -> Int -> Progress -> Doc
progressBar :: (Doc -> Doc) -> Int -> Progress -> Doc
progressBar Doc -> Doc
color Int
width (Progress Int
count Int
total) = Char -> Doc
char Char
'[' Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
progress_doc Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
space_doc Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Char -> Doc
char Char
']'
where
available_width :: Int
available_width = Int
width Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
2
characters_per_tick :: Double
characters_per_tick = Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
available_width Double -> Double -> Double
forall a. Fractional a => a -> a -> a
/ Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
total :: Double
progress_chars :: Int
progress_chars = Double -> Int
forall b. Integral b => Double -> b
forall a b. (RealFrac a, Integral b) => a -> b
round (Double
characters_per_tick Double -> Double -> Double
forall a. Num a => a -> a -> a
* Int -> Double
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
count)
space_chars :: Int
space_chars = Int
available_width Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
progress_chars
progress_doc :: Doc
progress_doc = Doc -> Doc
color (String -> Doc
text (String -> String
forall a. [a] -> [a]
reverse (Int -> String -> String
forall a. Int -> [a] -> [a]
take Int
progress_chars (Char
'>' Char -> String -> String
forall a. a -> [a] -> [a]
: Char -> String
forall a. a -> [a]
repeat Char
'='))))
space_doc :: Doc
space_doc = String -> Doc
text (Int -> Char -> String
forall a. Int -> a -> [a]
replicate Int
space_chars Char
' ')