diff options
author | Sean Whitton <spwhitton@spwhitton.name> | 2017-02-25 21:20:19 -0700 |
---|---|---|
committer | Sean Whitton <spwhitton@spwhitton.name> | 2017-02-25 21:33:39 -0700 |
commit | 0f443804011224eace10058d8c1672d3623a3b8f (patch) | |
tree | e37ef08c2fe913df594bc801d85b8a078ebc44ce /Brick | |
parent | 0876ced7760c8866f1b90b1bbaff9bdd19a7a7d8 (diff) | |
download | sscan-0f443804011224eace10058d8c1672d3623a3b8f.tar.gz |
improve defnList & factor out of Main.hs
Diffstat (limited to 'Brick')
-rw-r--r-- | Brick/Widgets/DefnList.hs | 37 |
1 files changed, 29 insertions, 8 deletions
diff --git a/Brick/Widgets/DefnList.hs b/Brick/Widgets/DefnList.hs index 5750cdc..090428f 100644 --- a/Brick/Widgets/DefnList.hs +++ b/Brick/Widgets/DefnList.hs @@ -1,13 +1,34 @@ -module Brick.Widgets.DefnList (defnList) where +{-# LANGUAGE OverloadedStrings #-} +module Brick.Widgets.DefnList (defnList, Align(..)) where + +import Data.Monoid +import qualified Data.Text as T +import qualified Graphics.Vty as V + +import Brick.Markup (markup) import Brick.Types import Brick.Widgets.Core +import Data.Text.Markup ((@@)) + +data Align = AlignLeft | AlignRight + deriving (Eq) -defnList :: [(String, String)] -> Widget () -defnList defns = vBox $ line <$> defns +defnList :: Align -> Maybe V.Attr -> [(String, String)] -> Widget () +defnList align attr defns = vBox $ line <$> defns where - line (label, content) = str $ - label ++ sep ++ (gap label content) ++ content - gap a b = take (maxWidth - length a - length b - length sep) $ repeat ' ' - maxWidth = maximum $ map (\(x,y) -> length sep + length x + length y) defns - sep = ": " + line (label, content) = markup $ + (T.pack label @@ labelAttr) <> (T.pack sep @@ V.defAttr) + <> (if align == AlignRight + then (T.pack (gap label content) @@ V.defAttr) + else mempty + ) + <> (T.pack content @@ V.defAttr) + + gap a b = take (maxWidth - length a - length b - length sep) $ + repeat ' ' + maxWidth = maximum $ + map (\(x,y) -> length sep + length x + length y) defns + sep = if align == AlignRight then ": " else ": " + + labelAttr = maybe V.defAttr id attr |