[client] Use the correct function for the job

Somehow, I missed this function and reimplemented it myself. Sometimes it helps
to read the documentation...
This commit is contained in:
Joscha 2020-02-28 18:30:05 +00:00
parent ab8c764329
commit 50e78cfed3

View file

@ -12,60 +12,38 @@ module Forest.Client.WidgetTree
) where ) where
import Brick import Brick
import Brick.BorderMap
import Control.Monad.Trans.Reader
import qualified Data.Text as T import qualified Data.Text as T
import qualified Graphics.Vty as Vty import qualified Graphics.Vty as Vty
import Lens.Micro import Lens.Micro
data WidgetTree n = WidgetTree (Widget n) [WidgetTree n] data WidgetTree n = WidgetTree (Widget n) [WidgetTree n]
addLoc :: Location -> Location -> Location indentWith :: AttrName -> T.Text -> T.Text -> Widget n -> Widget n
addLoc l1 l2 = -- The "left" variables are for rendering the indentation text, the "right"
let (x1, y1) = loc l1 -- variables are for the rendered wrapped widget.
(x2, y2) = loc l2 indentWith indentAttrName firstLine otherLines wrapped =
in Location (x1 + x2, y1 + y2) Widget (hSize wrapped) (vSize wrapped) $ do
let leftWidth = max (T.length firstLine) (T.length otherLines)
offsetResult :: Location -> Result n -> Result n context <- getContext
offsetResult offset result = result rightResult <- render $ hLimit (availWidth context - leftWidth) wrapped
{ cursors = map offsetCursor $ cursors result let rightImage = image rightResult
, visibilityRequests = map offsetVr $ visibilityRequests result -- Construct the Vty image containing the indentation text
, extents = map offsetExtent $ extents result height = Vty.imageHeight rightImage
, borders = translate offset $ borders result leftLines = firstLine : replicate (height - 1) otherLines
} leftAttribute = attrMapLookup indentAttrName $ context ^. ctxAttrMapL
where leftImage = Vty.vertCat $ map (Vty.text' leftAttribute) leftLines
offsetCursor c = c{cursorLocation = addLoc offset $ cursorLocation c} -- Add the indentation text to the left of the result image
offsetVr vr = vr{vrPosition = addLoc offset $ vrPosition vr} combinedImage = leftImage Vty.<|> image rightResult
offsetExtent e = e offset = Location (leftWidth, 0)
{ extentUpperLeft = addLoc offset $ extentUpperLeft e result = addResultOffset offset rightResult & imageL .~ combinedImage
, extentOffset = addLoc offset $ extentOffset e pure result
}
indentWith :: T.Text -> T.Text -> Widget n -> Widget n
indentWith firstLine otherLines wrapped = Widget
{ hSize = hSize wrapped
, vSize = vSize wrapped
, render = renderWidget
}
where
maxWidth = max (T.length firstLine) (T.length otherLines)
renderWidget = do
context <- ask
result <- render $ hLimit (availWidth context - maxWidth) wrapped
let attribute = attrMapLookup treeLineAttr $ context ^. ctxAttrMapL
resultHeight = Vty.imageHeight $ image result
textLines = firstLine : replicate (resultHeight - 1) otherLines
leftImage = Vty.vertCat $ map (Vty.text' attribute) textLines
newImage = leftImage Vty.<|> image result
newResult = offsetResult (Location (maxWidth, 0)) $ result{image=newImage}
pure newResult
indent :: IndentOptions -> [Widget n] -> Widget n indent :: IndentOptions -> [Widget n] -> Widget n
indent opts widgets = vBox $ reverse $ case reverse widgets of indent opts widgets = vBox $ reverse $ case reverse widgets of
[] -> [] [] -> []
(w:ws) -> (w:ws) ->
indentWith (lastBranch opts) (afterLastBranch opts) w : indentWith treeLineAttr (lastBranch opts) (afterLastBranch opts) w :
map (indentWith (inlineBranch opts) (noBranch opts)) ws map (indentWith treeLineAttr (inlineBranch opts) (noBranch opts)) ws
renderWidgetTree :: IndentOptions -> WidgetTree n -> Widget n renderWidgetTree :: IndentOptions -> WidgetTree n -> Widget n
renderWidgetTree opts (WidgetTree node children) = renderWidgetTree opts (WidgetTree node children) =