module Brick.Markup
( Markup
, markup
, (@?)
, GetAttr(..)
)
where
import Lens.Micro ((.~), (&), (^.))
import Control.Monad (forM)
import qualified Data.Text as T
import Data.Text.Markup
import Graphics.Vty (Attr, vertCat, horizCat, text', defAttr)
import Brick.AttrMap
import Brick.Types
class GetAttr a where
getAttr :: a -> RenderM n Attr
instance GetAttr Attr where
getAttr a = do
c <- getContext
return $ mergeWithDefault a (c^.ctxAttrMapL)
instance GetAttr AttrName where
getAttr = lookupAttrName
(@?) :: T.Text -> AttrName -> Markup AttrName
(@?) = (@@)
markup :: (Eq a, GetAttr a) => Markup a -> Widget n
markup m =
Widget Fixed Fixed $ do
let markupLines = markupToList m
mkLine pairs = do
is <- forM pairs $ \(t, aSrc) -> do
a <- getAttr aSrc
return $ text' a t
if null is
then do
def <- getAttr defAttr
return $ text' def $ T.singleton ' '
else return $ horizCat is
lineImgs <- mapM mkLine markupLines
return $ emptyResult & imageL .~ vertCat lineImgs