Skip to content

Commit f67bf03

Browse files
authored
Merge pull request #68 from mightybyte/centerText
Export centerText function
2 parents a67fcbf + e4f91de commit f67bf03

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

ChangeLog.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* Added a new `scrollable` widget in `Reflex.Vty.Widget.Scroll` that allows vertical scrolling when an `Image` is taller than the widget's height.
1111
* Add `ctrlc`, a convenience function that returns an event that fires when a Ctrl+c keypress is detected
1212
* Fix several issues with wide chars, cursor position and word wrapping in Zipper.hs
13+
* Add `centerText` function to Reflex.Vty.Widget.Box
1314

1415
## 0.4.1.1
1516

src/Reflex/Vty/Widget/Box.hs

+18-11
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ boxTitle boxStyle title child = do
7979
sides =
8080
[ withinImage (Region (left + 1) top (width - 2) 1) $
8181
V.text' attr $
82-
hPadText title' (_boxStyle_n style) (width - 2)
82+
centerText title' (_boxStyle_n style) (width - 2)
8383
, withinImage (Region right (top + 1) 1 (height - 2)) $
8484
V.charFill attr (_boxStyle_e style) 1 (height - 2)
8585
, withinImage (Region (left + 1) bottom (width - 2) 1) $
@@ -98,16 +98,23 @@ boxTitle boxStyle title child = do
9898
V.char attr (_boxStyle_sw style)
9999
]
100100
in sides ++ if width > 1 && height > 1 then corners else []
101-
hPadText :: T.Text -> Char -> Int -> T.Text
102-
hPadText t c l = if lt >= l
103-
then t
104-
else left <> t <> right
105-
where
106-
lt = T.length t
107-
delta = l - lt
108-
mkHalf n = T.replicate (n `div` 2) (T.singleton c)
109-
left = mkHalf $ delta + 1
110-
right = mkHalf delta
101+
102+
-- | Pad text on the left and right with the given character so that it is
103+
-- centered
104+
centerText
105+
:: T.Text -- ^ Text to center
106+
-> Char -- ^ Padding character
107+
-> Int -- ^ Width
108+
-> T.Text -- ^ Padded text
109+
centerText t c l = if lt >= l
110+
then t
111+
else left <> t <> right
112+
where
113+
lt = T.length t
114+
delta = l - lt
115+
mkHalf n = T.replicate (n `div` 2) (T.singleton c)
116+
left = mkHalf $ delta + 1
117+
right = mkHalf delta
111118

112119
-- | A box without a title
113120
box :: (MonadFix m, MonadHold t m, HasDisplayRegion t m, HasImageWriter t m, HasInput t m, HasFocusReader t m, HasTheme t m)

0 commit comments

Comments
 (0)