Add basic support for tables with fixed-size cell contents

This commit is contained in:
Jonathan Daugherty 2021-01-31 18:56:26 -08:00
parent 4fb43e52a4
commit 477fe4d716
3 changed files with 64 additions and 0 deletions

View File

@ -105,6 +105,7 @@ library
Brick.Widgets.FileBrowser
Brick.Widgets.List
Brick.Widgets.ProgressBar
Brick.Widgets.Table
Data.IMap
Data.Text.Markup
other-modules:
@ -139,6 +140,19 @@ library
if impl(ghc < 8.0)
build-depends: semigroups
executable brick-table-demo
if !flag(demos)
Buildable: False
hs-source-dirs: programs
ghc-options: -threaded -Wall -Wcompat -O2
default-language: Haskell2010
default-extensions: CPP
main-is: TableDemo.hs
build-depends: base,
brick,
text,
vty
executable brick-tail-demo
if !flag(demos)
Buildable: False

15
programs/TableDemo.hs Normal file
View File

@ -0,0 +1,15 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE CPP #-}
module Main where
#if !(MIN_VERSION_base(4,11,0))
import Data.Monoid ((<>))
#endif
import Brick
import Brick.Widgets.Table
ui :: Widget ()
ui = table [[txt "foo", txt "bar"], [txt "stuff", txt "more things"]]
main :: IO ()
main = simpleMain ui

View File

@ -0,0 +1,35 @@
module Brick.Widgets.Table
( table
)
where
import Control.Monad (forM)
import Data.List (transpose, intersperse)
import Graphics.Vty (imageHeight, imageWidth)
import Brick.Types
import Brick.Widgets.Core
import Brick.Widgets.Border
table :: [[Widget n]] -> Widget n
table [] = emptyWidget
table rows =
joinBorders $ border $ Widget Fixed Fixed $ do
cellResults <- forM rows $ mapM render
let rowHeights = rowHeight <$> cellResults
colWidths = colWidth <$> byColumn
rowHeight = maximum . fmap (imageHeight . image)
colWidth = maximum . fmap (imageWidth . image)
byColumn = transpose cellResults
toW = Widget Fixed Fixed . return
totalHeight = sum rowHeights
mkColumn (width, colCells) = do
paddedCells <- forM (zip rowHeights colCells) $ \(height, cell) ->
render $ padBottom (Pad (height - (imageHeight $ image cell)))
(toW cell)
render $ vBox $ intersperse (hLimit width hBorder) $
toW <$> paddedCells
columns <- mapM mkColumn $ zip colWidths byColumn
render $ hBox $
intersperse (vLimit (totalHeight + (length rows - 1)) vBorder) $
toW <$> columns