module Summoner.Tui.GroupBorder
( groupBorder
, (|>)
) where
import Brick (Edges (..), Padding (Max), Widget, padRight, vLimit, (<+>), (<=>))
import Brick.Forms (FormFieldState, (@@=))
import Brick.Widgets.Border (hBorder, hBorderWithLabel, joinableBorder, vBorder)
import Summoner.Tui.Widget (borderLabel, borderName)
infix 4 |>
(|>) :: Int -> a -> (Int, a)
(|>) = (,)
{-# INLINE (|>) #-}
groupBorder :: String -> [(Int, s -> FormFieldState s e n)] -> [s -> FormFieldState s e n]
groupBorder groupName = \case
[] -> []
[x] -> [groupAllBorders groupName x]
(x:y:xs) -> let (mid, l) = (init $ y :| xs, last $ y :| xs) in
groupBorderTop groupName x : map groupBorderMid mid ++ [groupBorderBottom l]
groupBorderTop :: String -> (Int, s -> FormFieldState s e n) -> (s -> FormFieldState s e n)
groupBorderTop groupName (i, f) =
( vLimit i
. ((tl <=> vBorder) <+>)
. (<+> (tr <=> vBorder))
. (hBorderWithLabel (borderName groupName) <=>)
) @@= f
groupBorderBottom :: (Int, s -> FormFieldState s e n) -> (s -> FormFieldState s e n)
groupBorderBottom (i, f) =
( vLimit i
. ((vBorder <=> bl) <+>)
. (<+> (vBorder <=> br))
. (<=> hBorder)
. padRight Max
) @@= f
groupBorderMid :: (Int, s -> FormFieldState s e n) -> (s -> FormFieldState s e n)
groupBorderMid (i, f) =
( vLimit i
. (vBorder <+>)
. (<+> vBorder)
. padRight Max
) @@= f
groupAllBorders :: String -> (Int, s -> FormFieldState s e n) -> (s -> FormFieldState s e n)
groupAllBorders groupName (i, f) =
( vLimit i
. borderLabel groupName
. padRight Max
) @@= f
tl, tr, bl, br :: Widget n
tl = joinableBorder (Edges False True False True)
tr = joinableBorder (Edges False True True False)
bl = joinableBorder (Edges True False False True)
br = joinableBorder (Edges True False True False)