mirror of
https://github.com/jtdaugherty/brick.git
synced 2025-01-03 19:46:39 +03:00
Markup: make markup support multi-line strings (fixes #41)
This commit is contained in:
parent
01bafb0b5b
commit
568df56630
@ -7,9 +7,12 @@ import qualified Graphics.Vty as V
|
|||||||
import Brick.Main (App(..), defaultMain, resizeOrQuit, neverShowCursor)
|
import Brick.Main (App(..), defaultMain, resizeOrQuit, neverShowCursor)
|
||||||
import Brick.Types
|
import Brick.Types
|
||||||
( Widget
|
( Widget
|
||||||
|
, Padding(..)
|
||||||
)
|
)
|
||||||
import Brick.Widgets.Core
|
import Brick.Widgets.Core
|
||||||
( (<=>)
|
( (<=>)
|
||||||
|
, (<+>)
|
||||||
|
, padLeft
|
||||||
)
|
)
|
||||||
import Brick.Util (on, fg)
|
import Brick.Util (on, fg)
|
||||||
import Brick.Markup (markup, (@?))
|
import Brick.Markup (markup, (@?))
|
||||||
@ -17,10 +20,11 @@ import Brick.AttrMap (attrMap, AttrMap)
|
|||||||
import Data.Text.Markup ((@@))
|
import Data.Text.Markup ((@@))
|
||||||
|
|
||||||
ui :: Widget
|
ui :: Widget
|
||||||
ui = m1 <=> m2
|
ui = (m1 <=> m2) <+> (padLeft (Pad 1) m3)
|
||||||
where
|
where
|
||||||
m1 = markup $ ("Hello" @@ fg V.blue) <> ", " <> ("world!" @@ fg V.red)
|
m1 = markup $ ("Hello" @@ fg V.blue) <> ", " <> ("world!" @@ fg V.red)
|
||||||
m2 = markup $ ("Hello" @? "keyword1") <> ", " <> ("world!" @? "keyword2")
|
m2 = markup $ ("Hello" @? "keyword1") <> ", " <> ("world!" @? "keyword2")
|
||||||
|
m3 = markup $ ("Hello," @? "keyword1") <> "\n" <> ("world!" @? "keyword2")
|
||||||
|
|
||||||
theMap :: AttrMap
|
theMap :: AttrMap
|
||||||
theMap = attrMap V.defAttr
|
theMap = attrMap V.defAttr
|
||||||
|
@ -17,7 +17,7 @@ import qualified Data.Text as T
|
|||||||
import Data.Text.Markup
|
import Data.Text.Markup
|
||||||
import Data.Default (def)
|
import Data.Default (def)
|
||||||
|
|
||||||
import Graphics.Vty (Attr, horizCat, string)
|
import Graphics.Vty (Attr, vertCat, horizCat, string)
|
||||||
|
|
||||||
import Brick.AttrMap
|
import Brick.AttrMap
|
||||||
import Brick.Types
|
import Brick.Types
|
||||||
@ -47,8 +47,11 @@ instance GetAttr AttrName where
|
|||||||
markup :: (Eq a, GetAttr a) => Markup a -> Widget
|
markup :: (Eq a, GetAttr a) => Markup a -> Widget
|
||||||
markup m =
|
markup m =
|
||||||
Widget Fixed Fixed $ do
|
Widget Fixed Fixed $ do
|
||||||
let pairs = markupToList m
|
let markupLines = markupToList m
|
||||||
imgs <- forM pairs $ \(t, aSrc) -> do
|
mkLine pairs = do
|
||||||
|
is <- forM pairs $ \(t, aSrc) -> do
|
||||||
a <- getAttr aSrc
|
a <- getAttr aSrc
|
||||||
return $ string a $ T.unpack t
|
return $ string a $ T.unpack t
|
||||||
return $ def & imageL .~ horizCat imgs
|
return $ horizCat is
|
||||||
|
lineImgs <- mapM mkLine markupLines
|
||||||
|
return $ def & imageL .~ vertCat lineImgs
|
||||||
|
@ -45,7 +45,7 @@ fromText = (@@ def)
|
|||||||
|
|
||||||
-- | Extract the text from markup, discarding the markup metadata.
|
-- | Extract the text from markup, discarding the markup metadata.
|
||||||
toText :: (Eq a) => Markup a -> T.Text
|
toText :: (Eq a) => Markup a -> T.Text
|
||||||
toText = T.concat . (fst <$>) . markupToList
|
toText = T.concat . (fst <$>) . concat . markupToList
|
||||||
|
|
||||||
-- | Set the metadata for a range of character positions in a piece of
|
-- | Set the metadata for a range of character positions in a piece of
|
||||||
-- markup. This is useful for, e.g., syntax highlighting.
|
-- markup. This is useful for, e.g., syntax highlighting.
|
||||||
@ -59,11 +59,17 @@ markupSet (start, len) val m@(Markup l) = if start < 0 || start + len > length l
|
|||||||
(theOldEntries, theTail) = splitAt len theLongTail
|
(theOldEntries, theTail) = splitAt len theLongTail
|
||||||
theNewEntries = zip (fst <$> theOldEntries) (repeat val)
|
theNewEntries = zip (fst <$> theOldEntries) (repeat val)
|
||||||
|
|
||||||
-- | Convert markup to a list of pairs in which each pair contains the
|
-- | Convert markup to a list of lines. Each line is represented by a
|
||||||
-- longest subsequence of characters having the same metadata.
|
-- list of pairs in which each pair contains the longest subsequence of
|
||||||
markupToList :: (Eq a) => Markup a -> [(T.Text, a)]
|
-- characters having the same metadata.
|
||||||
markupToList (Markup thePairs) = toList thePairs
|
markupToList :: (Eq a) => Markup a -> [[(T.Text, a)]]
|
||||||
|
markupToList (Markup thePairs) = toList <$> toLines [] [] thePairs
|
||||||
where
|
where
|
||||||
|
toLines ls cur [] = ls ++ [cur]
|
||||||
|
toLines ls cur ((ch, val):rest)
|
||||||
|
| ch == '\n' = toLines (ls ++ [cur]) [] rest
|
||||||
|
| otherwise = toLines ls (cur ++ [(ch, val)]) rest
|
||||||
|
|
||||||
toList [] = []
|
toList [] = []
|
||||||
toList ((ch, val):rest) = (T.pack $ ch : (fst <$> matching), val) : toList remaining
|
toList ((ch, val):rest) = (T.pack $ ch : (fst <$> matching), val) : toList remaining
|
||||||
where
|
where
|
||||||
|
Loading…
Reference in New Issue
Block a user