{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE CPP #-}
-- | This module provides a progress bar widget.
module Brick.Widgets.ProgressBar
  ( progressBar
  -- * Attributes
  , progressCompleteAttr
  , progressIncompleteAttr
  )
where

import Lens.Micro ((^.))
import Data.Maybe (fromMaybe)
#if !(MIN_VERSION_base(4,11,0))
import Data.Monoid
#endif
import Graphics.Vty (safeWcswidth)

import Brick.Types
import Brick.AttrMap
import Brick.Widgets.Core

-- | The attribute of the completed portion of the progress bar.
progressCompleteAttr :: AttrName
progressCompleteAttr :: AttrName
progressCompleteAttr = String -> AttrName
attrName String
"progressComplete"

-- | The attribute of the incomplete portion of the progress bar.
progressIncompleteAttr :: AttrName
progressIncompleteAttr :: AttrName
progressIncompleteAttr = String -> AttrName
attrName String
"progressIncomplete"

-- | Draw a progress bar with the specified (optional) label and
-- progress value. This fills available horizontal space and is one row
-- high.
progressBar :: Maybe String
            -- ^ The label. If specified, this is shown in the center of
            -- the progress bar.
            -> Float
            -- ^ The progress value. Should be between 0 and 1 inclusive.
            -> Widget n
progressBar :: forall n. Maybe String -> Float -> Widget n
progressBar Maybe String
mLabel Float
progress =
    forall n. Size -> Size -> RenderM n (Result n) -> Widget n
Widget Size
Greedy Size
Fixed forall a b. (a -> b) -> a -> b
$ do
        Context n
c <- forall n. RenderM n (Context n)
getContext
        let barWidth :: Int
barWidth = Context n
cforall s a. s -> Getting a s a -> a
^.forall n. Lens' (Context n) Int
availWidthL
            label :: String
label = forall a. a -> Maybe a -> a
fromMaybe String
"" Maybe String
mLabel
            labelWidth :: Int
labelWidth = String -> Int
safeWcswidth String
label
            spacesWidth :: Int
spacesWidth = Int
barWidth forall a. Num a => a -> a -> a
- Int
labelWidth
            leftPart :: String
leftPart = forall a. Int -> a -> [a]
replicate (Int
spacesWidth forall a. Integral a => a -> a -> a
`div` Int
2) Char
' '
            rightPart :: String
rightPart = forall a. Int -> a -> [a]
replicate (Int
barWidth forall a. Num a => a -> a -> a
- (Int
labelWidth forall a. Num a => a -> a -> a
+ forall (t :: * -> *) a. Foldable t => t a -> Int
length String
leftPart)) Char
' '
            fullBar :: String
fullBar = String
leftPart forall a. Semigroup a => a -> a -> a
<> String
label forall a. Semigroup a => a -> a -> a
<> String
rightPart
            completeWidth :: Int
completeWidth = forall a b. (RealFrac a, Integral b) => a -> b
round forall a b. (a -> b) -> a -> b
$ Float
progress forall a. Num a => a -> a -> a
* forall a. Enum a => Int -> a
toEnum (forall (t :: * -> *) a. Foldable t => t a -> Int
length String
fullBar)
            adjustedCompleteWidth :: Int
adjustedCompleteWidth = if Int
completeWidth forall a. Eq a => a -> a -> Bool
== forall (t :: * -> *) a. Foldable t => t a -> Int
length String
fullBar Bool -> Bool -> Bool
&& Float
progress forall a. Ord a => a -> a -> Bool
< Float
1.0
                                    then Int
completeWidth forall a. Num a => a -> a -> a
- Int
1
                                    else if Int
completeWidth forall a. Eq a => a -> a -> Bool
== Int
0 Bool -> Bool -> Bool
&& Float
progress forall a. Ord a => a -> a -> Bool
> Float
0.0
                                         then Int
1
                                         else Int
completeWidth
            (String
completePart, String
incompletePart) = forall a. Int -> [a] -> ([a], [a])
splitAt Int
adjustedCompleteWidth String
fullBar
        forall n. Widget n -> RenderM n (Result n)
render forall a b. (a -> b) -> a -> b
$ (forall n. AttrName -> Widget n -> Widget n
withAttr AttrName
progressCompleteAttr forall a b. (a -> b) -> a -> b
$ forall n. String -> Widget n
str String
completePart) forall n. Widget n -> Widget n -> Widget n
<+>
                 (forall n. AttrName -> Widget n -> Widget n
withAttr AttrName
progressIncompleteAttr forall a b. (a -> b) -> a -> b
$ forall n. String -> Widget n
str String
incompletePart)