Clean up attribute management combinators, use attribute names instead of attributes

This commit is contained in:
Jonathan Daugherty 2015-06-28 22:08:54 -07:00
parent e02192e38a
commit 52bf9ec288
8 changed files with 28 additions and 34 deletions

View File

@ -64,7 +64,7 @@ borderMappings =
colorDemo :: Widget
colorDemo =
withAttrMappings borderMappings $
updateAttrMap (applyAttrMappings borderMappings) $
borderWithLabel "title" $
hLimit 20 $
vLimit 5 $

View File

@ -52,7 +52,7 @@ appEvent e l =
listDrawElement :: Bool -> Int -> Widget
listDrawElement sel i =
let selStr s = if sel
then withAttrName customAttr (str $ "<" <> s <> ">")
then withAttr customAttr (str $ "<" <> s <> ">")
else str s
in (hCenterWith (Just ' ') $ vBox $ for [1..i+1] $ \j ->
"Item " <+> (selStr $ show i) <+> " Line " <+> (str $ show j)) <=> hBorder

View File

@ -48,7 +48,7 @@ editHighlightedKw2Attr :: AttrName
editHighlightedKw2Attr = editAttr <> "kw2"
kw :: Widget -> Widget
kw = withAttrName keywordAttr
kw = withAttr keywordAttr
highlightWord :: (Eq a) => String -> a -> Markup a -> Markup a
highlightWord w att mk = assignAttrs 0 chunks mk

View File

@ -67,12 +67,12 @@ border_ label wrapped =
$ vLimit (c^.availH - 2)
$ wrapped
let top = (withAttrName tlCornerAttr $ str [bsCornerTL bs])
let top = (withAttr tlCornerAttr $ str [bsCornerTL bs])
<+> hBorder_ label <+>
(withAttrName trCornerAttr $ str [bsCornerTR bs])
bottom = (withAttrName blCornerAttr $ str [bsCornerBL bs])
(withAttr trCornerAttr $ str [bsCornerTR bs])
bottom = (withAttr blCornerAttr $ str [bsCornerBL bs])
<+> hBorder <+>
(withAttrName brCornerAttr $ str [bsCornerBR bs])
(withAttr brCornerAttr $ str [bsCornerBR bs])
middle = vBorder <+> (Widget Fixed Fixed $ return middleResult) <+> vBorder
total = top <=> middle <=> bottom
@ -90,12 +90,12 @@ hBorder_ :: Maybe Widget -> Widget
hBorder_ label =
Widget Unlimited Fixed $ do
bs <- getActiveBorderStyle
render $ vLimit 1 $ withAttrName hBorderAttr $ hCenterWith (Just $ bsHorizontal bs) msg
render $ vLimit 1 $ withAttr hBorderAttr $ hCenterWith (Just $ bsHorizontal bs) msg
where
msg = maybe (txt "") (withAttrName hBorderLabelAttr) label
msg = maybe (txt "") (withAttr hBorderLabelAttr) label
vBorder :: Widget
vBorder =
Widget Fixed Unlimited $ do
bs <- getActiveBorderStyle
render $ hLimit 1 $ withAttrName vBorderAttr $ fill (bsVertical bs)
render $ hLimit 1 $ withAttr vBorderAttr $ fill (bsVertical bs)

View File

@ -33,10 +33,9 @@ module Brick.Widgets.Core
, hLimit
, vLimit
, withDefaultAttr
, withDefaultAttrName
, withAttrName
, withAttrMappings
, withAttr
, forceAttr
, updateAttrMap
, raw
, withBorderStyle
, translateBy

View File

@ -89,7 +89,7 @@ editAttr = "edit"
renderEditor :: Editor -> Widget
renderEditor e =
let cursorLoc = Location (e^.editCursorPosL, 0)
in withAttrName editAttr $
in withAttr editAttr $
vLimit 1 $
viewport (e^.editorNameL) Horizontal $
showCursor (e^.editorNameL) cursorLoc $

View File

@ -46,10 +46,9 @@ module Brick.Widgets.Internal
, hLimit
, vLimit
, withDefaultAttr
, withDefaultAttrName
, withAttrName
, withAttrMappings
, withAttr
, forceAttr
, updateAttrMap
, raw
, translateBy
, cropLeftBy
@ -302,31 +301,27 @@ vLimit h p =
Widget (hSize p) Fixed $ do
withReaderT (& availH .~ h) $ render $ cropToContext p
withAttrName :: AttrName -> Widget -> Widget
withAttrName an p =
withAttr :: AttrName -> Widget -> Widget
withAttr an p =
Widget (hSize p) (vSize p) $ do
withReaderT (& ctxAttrName .~ an) (render p)
withAttrMappings :: [(AttrName, V.Attr)] -> Widget -> Widget
withAttrMappings ms p =
Widget (hSize p) (vSize p) $ do
withReaderT (& ctxAttrs %~ applyAttrMappings ms) (render p)
withDefaultAttrName :: AttrName -> Widget -> Widget
withDefaultAttrName an p =
withDefaultAttr :: AttrName -> Widget -> Widget
withDefaultAttr an p =
Widget (hSize p) (vSize p) $ do
c <- getContext
withReaderT (& ctxAttrs %~ (setDefault (attrMapLookup an (c^.ctxAttrs)))) (render p)
withDefaultAttr :: V.Attr -> Widget -> Widget
withDefaultAttr a p =
updateAttrMap :: (AttrMap -> AttrMap) -> Widget -> Widget
updateAttrMap f p =
Widget (hSize p) (vSize p) $ do
withReaderT (& ctxAttrs %~ (setDefault a)) (render p)
withReaderT (& ctxAttrs %~ f) (render p)
forceAttr :: V.Attr -> Widget -> Widget
forceAttr a p =
forceAttr :: AttrName -> Widget -> Widget
forceAttr an p =
Widget (hSize p) (vSize p) $ do
withReaderT (& ctxAttrs .~ (forceAttrMap a)) (render p)
c <- getContext
withReaderT (& ctxAttrs .~ (forceAttrMap (attrMapLookup an (c^.ctxAttrs)))) (render p)
raw :: V.Image -> Widget
raw img = Widget Fixed Fixed $ return $ def & image .~ img

View File

@ -64,7 +64,7 @@ list name draw es =
in List es draw selIndex name
renderList :: List e -> Widget
renderList l = withDefaultAttrName listAttr $
renderList l = withDefaultAttr listAttr $
viewport (l^.listNameL) Vertical $
vBox $
drawListElements l
@ -77,7 +77,7 @@ drawListElements l = drawnElements
let isSelected = Just i == l^.listSelectedL
elemWidget = (l^.listElementDrawL) isSelected e
makeVisible = if isSelected
then (visible . withDefaultAttrName listSelectedAttr)
then (visible . withDefaultAttr listSelectedAttr)
else id
in makeVisible elemWidget