Skip to content

Commit

Permalink
address review suggestion, update colors
Browse files Browse the repository at this point in the history
and auto wrap help text
  • Loading branch information
sectore committed Oct 25, 2024
1 parent cbba318 commit 66b8454
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 64 deletions.
55 changes: 10 additions & 45 deletions programs/ProgressBarDemo.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import Brick.Types
import Brick.Widgets.Core
( (<+>), (<=>)
, str
, strWrap
, updateAttrMap
, overrideAttr
)
Expand Down Expand Up @@ -51,62 +52,26 @@ drawUI p = [ui]
-- custom bars
cBar1 = overrideAttr P.progressCompleteAttr cDoneAttr1 $
overrideAttr P.progressIncompleteAttr cToDoAttr1
$ bar' (_x p) ('', '')
$ bar' '' '' $ _x p
cBar2 = overrideAttr P.progressCompleteAttr cDoneAttr2 $
overrideAttr P.progressIncompleteAttr cToDoAttr2
$ bar' (_y p) ('', '')
$ bar' '|' '' $ _y p
cBar3 = overrideAttr P.progressCompleteAttr cDoneAttr $
overrideAttr P.progressIncompleteAttr cToDoAttr
$ bar' (_z p) ('', '')
cBar4 = overrideAttr P.progressCompleteAttr cDoneAttr $
overrideAttr P.progressIncompleteAttr cToDoAttr
$ bar' (_x p) ('', '')
cBar5 = overrideAttr P.progressCompleteAttr cDoneAttr $
overrideAttr P.progressIncompleteAttr cToDoAttr
$ bar' (_y p) ('', '=')
cBar6 = overrideAttr P.progressCompleteAttr cDoneAttr $
overrideAttr P.progressIncompleteAttr cToDoAttr
$ bar' (_z p) ('', '')
cBar7 = overrideAttr P.progressCompleteAttr cDoneAttr $
overrideAttr P.progressIncompleteAttr cToDoAttr
$ bar' (_x p) ('', '')
cBar8 = overrideAttr P.progressCompleteAttr cDoneAttr $
overrideAttr P.progressIncompleteAttr cToDoAttr
$ bar' (_y p) ('', '')
cBar9 = overrideAttr P.progressCompleteAttr cDoneAttr $
overrideAttr P.progressIncompleteAttr cToDoAttr
$ bar' (_z p) ('', '')
cBar10 = overrideAttr P.progressCompleteAttr cDoneAttr $
overrideAttr P.progressIncompleteAttr cToDoAttr
$ bar' (_x p) ('', '')
cBar11 = overrideAttr P.progressCompleteAttr cDoneAttr $
overrideAttr P.progressIncompleteAttr cToDoAttr
$ bar' (_y p) ('|', '=')
cBar12 = overrideAttr P.progressCompleteAttr cDoneAttr $
overrideAttr P.progressIncompleteAttr cToDoAttr
$ bar' (_z p) ('', '')
$ bar' '' '' $ _z p
lbl c = if _showLabel p
then Just $ " " ++ (show $ fromEnum $ c * 100) ++ " "
else Nothing
bar v = P.progressBar (lbl v) v
bar' v chars = P.progressBar' (lbl v) v chars
bar' cc ic v = P.customProgressBar cc ic (lbl v) v
ui = (str "X: " <+> xBar) <=>
(str "Y: " <+> yBar) <=>
(str "Z: " <+> zBar) <=>
(str "X: " <+> cBar1) <=>
(str "Y: " <+> cBar2) <=>
(str "Z: " <+> cBar3) <=>
(str "X: " <+> cBar4) <=>
(str "Y: " <+> cBar5) <=>
(str "Z: " <+> cBar6) <=>
(str "X: " <+> cBar7) <=>
(str "Y: " <+> cBar8) <=>
(str "Z: " <+> cBar9) <=>
(str "X: " <+> cBar10) <=>
(str "Y: " <+> cBar11) <=>
(str "Z: " <+> cBar12) <=>
str "" <=>
str "Hit 'x', 'y', or 'z' to advance progress, 'r' to revert values, 'cmd + r' to reset values or 'q' to quit"
strWrap "Hit 'x', 'y', or 'z' to advance progress, 't' to toggle labels, 'r' to revert values, 'cmd + r' to reset values or 'q' to quit"

appEvent :: T.BrickEvent () e -> T.EventM () (MyAppState ()) ()
appEvent (T.VtyEvent e) =
Expand Down Expand Up @@ -165,10 +130,10 @@ theMap = A.attrMap V.defAttr
, (zToDoAttr, V.blue `on` V.red)
, (cDoneAttr, fg V.blue)
, (cToDoAttr, fg V.blue)
, (cDoneAttr1, fg V.white)
, (cToDoAttr1, fg V.black)
, (cDoneAttr2, fg V.yellow)
, (cToDoAttr2, fg V.black)
, (cDoneAttr1, fg V.red)
, (cToDoAttr1, fg V.brightWhite)
, (cDoneAttr2, fg V.green)
, (cToDoAttr2, fg V.brightGreen)
, (P.progressIncompleteAttr, fg V.yellow)
]

Expand Down
38 changes: 19 additions & 19 deletions src/Brick/Widgets/ProgressBar.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
-- | This module provides a progress bar widget.
module Brick.Widgets.ProgressBar
( progressBar
, progressBar'
, customProgressBar
-- * Attributes
, progressCompleteAttr
, progressIncompleteAttr
Expand Down Expand Up @@ -38,24 +38,24 @@ progressBar :: Maybe String
-> Float
-- ^ The progress value. Should be between 0 and 1 inclusive.
-> Widget n
progressBar mLabel progress =
progressBar' mLabel progress (' ', ' ')
progressBar = customProgressBar ' ' ' '

-- | Draw a progress bar with the specified (optional) label,
-- progress value and a pair of characters to fill the progress.
-- progress value and custom characters to fill the progress.
-- 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.
-> (Char, Char)
-- ^ Pair of characters to fill the bar. First character is used
-- to fill the completed part, second character draws the uncompleted part.
-- Please be aware of using wide characters in Brick,
-- see [Wide Character Support and the TextWidth class](https://github.com/jtdaugherty/brick/blob/master/docs/guide.rst#wide-character-support-and-the-textwidth-class)
-> Widget n
progressBar' mLabel progress (completeChar, uncompleteChar) =
-- Please be aware of using wide characters in Brick,
-- see [Wide Character Support and the TextWidth class](https://github.com/jtdaugherty/brick/blob/master/docs/guide.rst#wide-character-support-and-the-textwidth-class)
customProgressBar :: Char
-- ^ Character to fill the completed part.
-> Char
-- ^ Character to fill the incomplete part.
-> 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
customProgressBar completeChar incompleteChar mLabel progress =
Widget Greedy Fixed $ do
c <- getContext
let barWidth = c^.availWidthL
Expand All @@ -67,11 +67,11 @@ progressBar' mLabel progress (completeChar, uncompleteChar) =
completeWidth = round $ progress * toEnum barWidth

leftCompleteWidth = min leftWidth completeWidth
leftIncompleteWidth = leftWidth - leftCompleteWidth
leftPart = replicate leftCompleteWidth completeChar ++ replicate leftIncompleteWidth uncompleteChar
leftIncompleteWidth = leftWidth - leftCompleteWidth
leftPart = replicate leftCompleteWidth completeChar ++ replicate leftIncompleteWidth incompleteChar
rightCompleteWidth = max 0 (completeWidth - labelWidth - leftWidth)
rightIncompleteWidth = rightWidth - rightCompleteWidth
rightPart = replicate rightCompleteWidth completeChar ++ replicate rightIncompleteWidth uncompleteChar
rightPart = replicate rightCompleteWidth completeChar ++ replicate rightIncompleteWidth incompleteChar

fullBar = leftPart <> label <> rightPart
adjustedCompleteWidth = if completeWidth == length fullBar && progress < 1.0
Expand Down

0 comments on commit 66b8454

Please sign in to comment.