{-# LANGUAGE MultiWayIf #-}
module Brick.Widgets.Table
(
Table
, ColumnAlignment(..)
, RowAlignment(..)
, TableException(..)
, table
, alignLeft
, alignRight
, alignCenter
, alignTop
, alignMiddle
, alignBottom
, setColAlignment
, setRowAlignment
, setDefaultColAlignment
, setDefaultRowAlignment
, surroundingBorder
, rowBorders
, columnBorders
, renderTable
, RenderedTableCells(..)
, BorderConfiguration(..)
, tableCellLayout
, addBorders
, alignColumns
)
where
import Control.Monad (forM)
import qualified Control.Exception as E
import Data.List (transpose, intersperse, nub)
import qualified Data.Map as M
#if !(MIN_VERSION_base(4,11,0))
import Data.Monoid ((<>))
#endif
import Graphics.Vty (imageHeight, imageWidth, charFill)
import Lens.Micro ((^.))
import Brick.Types
import Brick.Widgets.Core
import Brick.Widgets.Center
import Brick.Widgets.Border
data ColumnAlignment =
AlignLeft
| AlignCenter
| AlignRight
deriving (ColumnAlignment -> ColumnAlignment -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ColumnAlignment -> ColumnAlignment -> Bool
$c/= :: ColumnAlignment -> ColumnAlignment -> Bool
== :: ColumnAlignment -> ColumnAlignment -> Bool
$c== :: ColumnAlignment -> ColumnAlignment -> Bool
Eq, Int -> ColumnAlignment -> ShowS
[ColumnAlignment] -> ShowS
ColumnAlignment -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ColumnAlignment] -> ShowS
$cshowList :: [ColumnAlignment] -> ShowS
show :: ColumnAlignment -> String
$cshow :: ColumnAlignment -> String
showsPrec :: Int -> ColumnAlignment -> ShowS
$cshowsPrec :: Int -> ColumnAlignment -> ShowS
Show, ReadPrec [ColumnAlignment]
ReadPrec ColumnAlignment
Int -> ReadS ColumnAlignment
ReadS [ColumnAlignment]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [ColumnAlignment]
$creadListPrec :: ReadPrec [ColumnAlignment]
readPrec :: ReadPrec ColumnAlignment
$creadPrec :: ReadPrec ColumnAlignment
readList :: ReadS [ColumnAlignment]
$creadList :: ReadS [ColumnAlignment]
readsPrec :: Int -> ReadS ColumnAlignment
$creadsPrec :: Int -> ReadS ColumnAlignment
Read)
data RowAlignment =
AlignTop
| AlignMiddle
| AlignBottom
deriving (RowAlignment -> RowAlignment -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: RowAlignment -> RowAlignment -> Bool
$c/= :: RowAlignment -> RowAlignment -> Bool
== :: RowAlignment -> RowAlignment -> Bool
$c== :: RowAlignment -> RowAlignment -> Bool
Eq, Int -> RowAlignment -> ShowS
[RowAlignment] -> ShowS
RowAlignment -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [RowAlignment] -> ShowS
$cshowList :: [RowAlignment] -> ShowS
show :: RowAlignment -> String
$cshow :: RowAlignment -> String
showsPrec :: Int -> RowAlignment -> ShowS
$cshowsPrec :: Int -> RowAlignment -> ShowS
Show, ReadPrec [RowAlignment]
ReadPrec RowAlignment
Int -> ReadS RowAlignment
ReadS [RowAlignment]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [RowAlignment]
$creadListPrec :: ReadPrec [RowAlignment]
readPrec :: ReadPrec RowAlignment
$creadPrec :: ReadPrec RowAlignment
readList :: ReadS [RowAlignment]
$creadList :: ReadS [RowAlignment]
readsPrec :: Int -> ReadS RowAlignment
$creadsPrec :: Int -> ReadS RowAlignment
Read)
data TableException =
TEUnequalRowSizes
| TEInvalidCellSizePolicy
deriving (TableException -> TableException -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TableException -> TableException -> Bool
$c/= :: TableException -> TableException -> Bool
== :: TableException -> TableException -> Bool
$c== :: TableException -> TableException -> Bool
Eq, Int -> TableException -> ShowS
[TableException] -> ShowS
TableException -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TableException] -> ShowS
$cshowList :: [TableException] -> ShowS
show :: TableException -> String
$cshow :: TableException -> String
showsPrec :: Int -> TableException -> ShowS
$cshowsPrec :: Int -> TableException -> ShowS
Show, ReadPrec [TableException]
ReadPrec TableException
Int -> ReadS TableException
ReadS [TableException]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [TableException]
$creadListPrec :: ReadPrec [TableException]
readPrec :: ReadPrec TableException
$creadPrec :: ReadPrec TableException
readList :: ReadS [TableException]
$creadList :: ReadS [TableException]
readsPrec :: Int -> ReadS TableException
$creadsPrec :: Int -> ReadS TableException
Read)
instance E.Exception TableException where
data Table n =
Table { forall n. Table n -> Map Int ColumnAlignment
columnAlignments :: M.Map Int ColumnAlignment
, forall n. Table n -> Map Int RowAlignment
rowAlignments :: M.Map Int RowAlignment
, forall n. Table n -> [[Widget n]]
tableRows :: [[Widget n]]
, forall n. Table n -> ColumnAlignment
defaultColumnAlignment :: ColumnAlignment
, forall n. Table n -> RowAlignment
defaultRowAlignment :: RowAlignment
, forall n. Table n -> BorderConfiguration
tableBorderConfiguration :: BorderConfiguration
}
data BorderConfiguration =
BorderConfiguration { BorderConfiguration -> Bool
drawSurroundingBorder :: Bool
, BorderConfiguration -> Bool
drawRowBorders :: Bool
, BorderConfiguration -> Bool
drawColumnBorders :: Bool
}
table :: [[Widget n]] -> Table n
table :: forall n. [[Widget n]] -> Table n
table [[Widget n]]
rows =
if | Bool -> Bool
not Bool
allFixed -> forall a e. Exception e => e -> a
E.throw TableException
TEInvalidCellSizePolicy
| Bool -> Bool
not Bool
allSameLength -> forall a e. Exception e => e -> a
E.throw TableException
TEUnequalRowSizes
| Bool
otherwise -> Table n
t
where
allSameLength :: Bool
allSameLength = forall (t :: * -> *) a. Foldable t => t a -> Int
length (forall a. Eq a => [a] -> [a]
nub (forall (t :: * -> *) a. Foldable t => t a -> Int
length forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [[Widget n]]
rows)) forall a. Ord a => a -> a -> Bool
<= Int
1
allFixed :: Bool
allFixed = forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all forall {n}. [Widget n] -> Bool
fixedRow [[Widget n]]
rows
fixedRow :: [Widget n] -> Bool
fixedRow = forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all forall {n}. Widget n -> Bool
fixedCell
fixedCell :: Widget n -> Bool
fixedCell Widget n
w = forall n. Widget n -> Size
hSize Widget n
w forall a. Eq a => a -> a -> Bool
== Size
Fixed Bool -> Bool -> Bool
&& forall n. Widget n -> Size
vSize Widget n
w forall a. Eq a => a -> a -> Bool
== Size
Fixed
t :: Table n
t = Table { columnAlignments :: Map Int ColumnAlignment
columnAlignments = forall a. Monoid a => a
mempty
, rowAlignments :: Map Int RowAlignment
rowAlignments = forall a. Monoid a => a
mempty
, tableRows :: [[Widget n]]
tableRows = [[Widget n]]
rows
, defaultColumnAlignment :: ColumnAlignment
defaultColumnAlignment = ColumnAlignment
AlignLeft
, defaultRowAlignment :: RowAlignment
defaultRowAlignment = RowAlignment
AlignTop
, tableBorderConfiguration :: BorderConfiguration
tableBorderConfiguration =
BorderConfiguration { drawSurroundingBorder :: Bool
drawSurroundingBorder = Bool
True
, drawRowBorders :: Bool
drawRowBorders = Bool
True
, drawColumnBorders :: Bool
drawColumnBorders = Bool
True
}
}
surroundingBorder :: Bool -> Table n -> Table n
surroundingBorder :: forall n. Bool -> Table n -> Table n
surroundingBorder Bool
b Table n
t =
Table n
t { tableBorderConfiguration :: BorderConfiguration
tableBorderConfiguration = (forall n. Table n -> BorderConfiguration
tableBorderConfiguration Table n
t) { drawSurroundingBorder :: Bool
drawSurroundingBorder = Bool
b } }
rowBorders :: Bool -> Table n -> Table n
rowBorders :: forall n. Bool -> Table n -> Table n
rowBorders Bool
b Table n
t =
Table n
t { tableBorderConfiguration :: BorderConfiguration
tableBorderConfiguration = (forall n. Table n -> BorderConfiguration
tableBorderConfiguration Table n
t) { drawRowBorders :: Bool
drawRowBorders = Bool
b } }
columnBorders :: Bool -> Table n -> Table n
columnBorders :: forall n. Bool -> Table n -> Table n
columnBorders Bool
b Table n
t =
Table n
t { tableBorderConfiguration :: BorderConfiguration
tableBorderConfiguration = (forall n. Table n -> BorderConfiguration
tableBorderConfiguration Table n
t) { drawColumnBorders :: Bool
drawColumnBorders = Bool
b } }
alignRight :: Int -> Table n -> Table n
alignRight :: forall n. Int -> Table n -> Table n
alignRight = forall n. ColumnAlignment -> Int -> Table n -> Table n
setColAlignment ColumnAlignment
AlignRight
alignLeft :: Int -> Table n -> Table n
alignLeft :: forall n. Int -> Table n -> Table n
alignLeft = forall n. ColumnAlignment -> Int -> Table n -> Table n
setColAlignment ColumnAlignment
AlignLeft
alignCenter :: Int -> Table n -> Table n
alignCenter :: forall n. Int -> Table n -> Table n
alignCenter = forall n. ColumnAlignment -> Int -> Table n -> Table n
setColAlignment ColumnAlignment
AlignCenter
alignTop :: Int -> Table n -> Table n
alignTop :: forall n. Int -> Table n -> Table n
alignTop = forall n. RowAlignment -> Int -> Table n -> Table n
setRowAlignment RowAlignment
AlignTop
alignMiddle :: Int -> Table n -> Table n
alignMiddle :: forall n. Int -> Table n -> Table n
alignMiddle = forall n. RowAlignment -> Int -> Table n -> Table n
setRowAlignment RowAlignment
AlignMiddle
alignBottom :: Int -> Table n -> Table n
alignBottom :: forall n. Int -> Table n -> Table n
alignBottom = forall n. RowAlignment -> Int -> Table n -> Table n
setRowAlignment RowAlignment
AlignBottom
setColAlignment :: ColumnAlignment -> Int -> Table n -> Table n
setColAlignment :: forall n. ColumnAlignment -> Int -> Table n -> Table n
setColAlignment ColumnAlignment
a Int
col Table n
t =
Table n
t { columnAlignments :: Map Int ColumnAlignment
columnAlignments = forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert Int
col ColumnAlignment
a (forall n. Table n -> Map Int ColumnAlignment
columnAlignments Table n
t) }
setRowAlignment :: RowAlignment -> Int -> Table n -> Table n
setRowAlignment :: forall n. RowAlignment -> Int -> Table n -> Table n
setRowAlignment RowAlignment
a Int
row Table n
t =
Table n
t { rowAlignments :: Map Int RowAlignment
rowAlignments = forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert Int
row RowAlignment
a (forall n. Table n -> Map Int RowAlignment
rowAlignments Table n
t) }
setDefaultColAlignment :: ColumnAlignment -> Table n -> Table n
setDefaultColAlignment :: forall n. ColumnAlignment -> Table n -> Table n
setDefaultColAlignment ColumnAlignment
a Table n
t =
Table n
t { defaultColumnAlignment :: ColumnAlignment
defaultColumnAlignment = ColumnAlignment
a }
setDefaultRowAlignment :: RowAlignment -> Table n -> Table n
setDefaultRowAlignment :: forall n. RowAlignment -> Table n -> Table n
setDefaultRowAlignment RowAlignment
a Table n
t =
Table n
t { defaultRowAlignment :: RowAlignment
defaultRowAlignment = RowAlignment
a }
renderTable :: Table n -> Widget n
renderTable :: forall n. Table n -> Widget n
renderTable Table n
t =
forall n. Widget n -> Widget n
joinBorders forall a b. (a -> b) -> a -> b
$
forall n. Size -> Size -> RenderM n (Result n) -> Widget n
Widget Size
Fixed Size
Fixed forall a b. (a -> b) -> a -> b
$ do
forall n. Table n -> RenderM n (RenderedTableCells n)
tableCellLayout Table n
t forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall n. RenderedTableCells n -> RenderM n (Widget n)
addBorders forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall n. Widget n -> RenderM n (Result n)
render
data RenderedTableCells n =
RenderedTableCells { forall n. RenderedTableCells n -> [[Widget n]]
renderedTableRows :: [[Widget n]]
, forall n. RenderedTableCells n -> [Int]
renderedTableColumnWidths :: [Int]
, forall n. RenderedTableCells n -> [Int]
renderedTableRowHeights :: [Int]
, forall n. RenderedTableCells n -> BorderConfiguration
borderConfiguration :: BorderConfiguration
}
addBorders :: RenderedTableCells n -> RenderM n (Widget n)
addBorders :: forall n. RenderedTableCells n -> RenderM n (Widget n)
addBorders RenderedTableCells n
r = do
let cfg :: BorderConfiguration
cfg = forall n. RenderedTableCells n -> BorderConfiguration
borderConfiguration RenderedTableCells n
r
rows :: [[Widget n]]
rows = forall n. RenderedTableCells n -> [[Widget n]]
renderedTableRows RenderedTableCells n
r
rowHeights :: [Int]
rowHeights = forall n. RenderedTableCells n -> [Int]
renderedTableRowHeights RenderedTableCells n
r
colWidths :: [Int]
colWidths = forall n. RenderedTableCells n -> [Int]
renderedTableColumnWidths RenderedTableCells n
r
contentWidth :: Int
contentWidth = forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Int]
colWidths
contentHeight :: Int
contentHeight = forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Int]
rowHeights
hBorderLength :: Int
hBorderLength = Int
contentWidth forall a. Num a => a -> a -> a
+ if BorderConfiguration -> Bool
drawColumnBorders BorderConfiguration
cfg
then forall a. Ord a => a -> a -> a
max (forall (t :: * -> *) a. Foldable t => t a -> Int
length [Int]
colWidths forall a. Num a => a -> a -> a
- Int
1) Int
0
else Int
0
vBorderHeight :: Int
vBorderHeight = Int
contentHeight forall a. Num a => a -> a -> a
+ if BorderConfiguration -> Bool
drawRowBorders BorderConfiguration
cfg
then forall a. Ord a => a -> a -> a
max (forall (t :: * -> *) a. Foldable t => t a -> Int
length [Int]
rowHeights forall a. Num a => a -> a -> a
- Int
1) Int
0
else Int
0
horizBorder :: Widget n
horizBorder = forall n. Int -> Widget n -> Widget n
hLimit Int
hBorderLength forall n. Widget n
hBorder
vertBorder :: Widget n
vertBorder = forall n. Int -> Widget n -> Widget n
vLimit Int
vBorderHeight forall n. Widget n
vBorder
leftBorder :: Widget n
leftBorder =
forall n. [Widget n] -> Widget n
vBox [forall n. Widget n
topLeftCorner, forall n. Widget n
vertBorder, forall n. Widget n
bottomLeftCorner]
rightBorder :: Widget n
rightBorder =
forall n. [Widget n] -> Widget n
vBox [forall n. Widget n
topRightCorner, forall n. Widget n
vertBorder, forall n. Widget n
bottomRightCorner]
maybeWrap :: (BorderConfiguration -> Bool) -> (a -> a) -> a -> a
maybeWrap BorderConfiguration -> Bool
check a -> a
f =
if BorderConfiguration -> Bool
check BorderConfiguration
cfg then a -> a
f else forall a. a -> a
id
addSurroundingBorder :: Widget n -> Widget n
addSurroundingBorder Widget n
b =
forall n. Widget n
leftBorder forall n. Widget n -> Widget n -> Widget n
<+> (forall n. Widget n
horizBorder forall n. Widget n -> Widget n -> Widget n
<=> Widget n
b forall n. Widget n -> Widget n -> Widget n
<=> forall n. Widget n
horizBorder) forall n. Widget n -> Widget n -> Widget n
<+> forall n. Widget n
rightBorder
addRowBorders :: [Widget n] -> [Widget n]
addRowBorders =
forall a. a -> [a] -> [a]
intersperse forall n. Widget n
horizBorder
rowsWithColumnBorders :: [Widget n]
rowsWithColumnBorders = (\(Int
h, [Widget n]
row) -> forall n. [Widget n] -> Widget n
hBox forall a b. (a -> b) -> a -> b
$ forall {n}. Int -> [Widget n] -> [Widget n]
maybeColumnBorders Int
h [Widget n]
row) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a b. [a] -> [b] -> [(a, b)]
zip [Int]
rowHeights [[Widget n]]
rows
maybeColumnBorders :: Int -> [Widget n] -> [Widget n]
maybeColumnBorders Int
height = forall n.
BorderConfiguration
-> (BorderConfiguration -> Bool)
-> Widget n
-> [Widget n]
-> [Widget n]
maybeIntersperse BorderConfiguration
cfg BorderConfiguration -> Bool
drawColumnBorders (forall n. Int -> Widget n -> Widget n
vLimit Int
height forall n. Widget n
vBorder)
body :: Widget n
body = forall n. [Widget n] -> Widget n
vBox forall a b. (a -> b) -> a -> b
$
forall {a}. (BorderConfiguration -> Bool) -> (a -> a) -> a -> a
maybeWrap BorderConfiguration -> Bool
drawRowBorders forall {n}. [Widget n] -> [Widget n]
addRowBorders [Widget n]
rowsWithColumnBorders
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall {a}. (BorderConfiguration -> Bool) -> (a -> a) -> a -> a
maybeWrap BorderConfiguration -> Bool
drawSurroundingBorder forall n. Widget n -> Widget n
addSurroundingBorder Widget n
body
tableCellLayout :: Table n -> RenderM n (RenderedTableCells n)
tableCellLayout :: forall n. Table n -> RenderM n (RenderedTableCells n)
tableCellLayout Table n
t = do
Context n
ctx <- forall n. RenderM n (Context n)
getContext
[[Result n]]
cellResults <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM (forall n. Table n -> [[Widget n]]
tableRows Table n
t) forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM forall n. Widget n -> RenderM n (Result n)
render
let rowHeights :: [Int]
rowHeights = forall {n}. [Result n] -> Int
rowHeight forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [[Result n]]
cellResults
colWidths :: [Int]
colWidths = forall {n}. [Result n] -> Int
colWidth forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. [[a]] -> [[a]]
transpose [[Result n]]
cellResults
numRows :: Int
numRows = forall (t :: * -> *) a. Foldable t => t a -> Int
length [Int]
rowHeights
numCols :: Int
numCols = if forall (t :: * -> *) a. Foldable t => t a -> Int
length [[Result n]]
cellResults forall a. Ord a => a -> a -> Bool
>= Int
1
then forall (t :: * -> *) a. Foldable t => t a -> Int
length ([[Result n]]
cellResults forall a. [a] -> Int -> a
!! Int
0)
else Int
0
allRowAligns :: [RowAlignment]
allRowAligns = (\Int
i -> forall k a. Ord k => a -> k -> Map k a -> a
M.findWithDefault (forall n. Table n -> RowAlignment
defaultRowAlignment Table n
t) Int
i (forall n. Table n -> Map Int RowAlignment
rowAlignments Table n
t)) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
[Int
0..Int
numRows forall a. Num a => a -> a -> a
- Int
1]
allColAligns :: [ColumnAlignment]
allColAligns = (\Int
i -> forall k a. Ord k => a -> k -> Map k a -> a
M.findWithDefault (forall n. Table n -> ColumnAlignment
defaultColumnAlignment Table n
t) Int
i (forall n. Table n -> Map Int ColumnAlignment
columnAlignments Table n
t)) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
[Int
0..Int
numCols forall a. Num a => a -> a -> a
- Int
1]
rowHeight :: [Result n] -> Int
rowHeight = forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Image -> Int
imageHeight forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall n. Result n -> Image
image)
colWidth :: [Result n] -> Int
colWidth = forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Image -> Int
imageWidth forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall n. Result n -> Image
image)
toW :: Result n -> Widget n
toW = forall n. Size -> Size -> RenderM n (Result n) -> Widget n
Widget Size
Fixed Size
Fixed forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. Monad m => a -> m a
return
fillEmptyCell :: d -> d -> Result n -> Result n
fillEmptyCell d
w d
h Result n
result =
if Image -> Int
imageWidth (forall n. Result n -> Image
image Result n
result) forall a. Eq a => a -> a -> Bool
== Int
0 Bool -> Bool -> Bool
&& Image -> Int
imageHeight (forall n. Result n -> Image
image Result n
result) forall a. Eq a => a -> a -> Bool
== Int
0
then Result n
result { image :: Image
image = forall d. Integral d => Attr -> Char -> d -> d -> Image
charFill (Context n
ctxforall s a. s -> Getting a s a -> a
^.forall r n. Getting r (Context n) Attr
attrL) Char
' ' d
w d
h }
else Result n
result
mkRow :: (RowAlignment, Int, [Result n]) -> [Widget n]
mkRow (RowAlignment
vAlign, Int
height, [Result n]
rowCells) =
let paddedCells :: [Widget n]
paddedCells = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a b. (a -> b) -> [a] -> [b]
map (forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
zip3 [ColumnAlignment]
allColAligns [Int]
colWidths [Result n]
rowCells) forall a b. (a -> b) -> a -> b
$ \(ColumnAlignment
hAlign, Int
width, Result n
cell) ->
forall n. Int -> ColumnAlignment -> Widget n -> Widget n
applyColAlignment Int
width ColumnAlignment
hAlign forall a b. (a -> b) -> a -> b
$
forall n. Int -> RowAlignment -> Widget n -> Widget n
applyRowAlignment Int
height RowAlignment
vAlign forall a b. (a -> b) -> a -> b
$
forall {n}. Result n -> Widget n
toW forall a b. (a -> b) -> a -> b
$
forall {d} {n}. Integral d => d -> d -> Result n -> Result n
fillEmptyCell Int
width Int
height Result n
cell
in [Widget n]
paddedCells
let rows :: [[Widget n]]
rows = forall {n}. (RowAlignment, Int, [Result n]) -> [Widget n]
mkRow forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
zip3 [RowAlignment]
allRowAligns [Int]
rowHeights [[Result n]]
cellResults
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ RenderedTableCells { renderedTableRows :: [[Widget n]]
renderedTableRows = [[Widget n]]
rows
, renderedTableColumnWidths :: [Int]
renderedTableColumnWidths = [Int]
colWidths
, renderedTableRowHeights :: [Int]
renderedTableRowHeights = [Int]
rowHeights
, borderConfiguration :: BorderConfiguration
borderConfiguration = forall n. Table n -> BorderConfiguration
tableBorderConfiguration Table n
t
}
maybeIntersperse :: BorderConfiguration -> (BorderConfiguration -> Bool) -> Widget n -> [Widget n] -> [Widget n]
maybeIntersperse :: forall n.
BorderConfiguration
-> (BorderConfiguration -> Bool)
-> Widget n
-> [Widget n]
-> [Widget n]
maybeIntersperse BorderConfiguration
cfg BorderConfiguration -> Bool
f Widget n
v | BorderConfiguration -> Bool
f BorderConfiguration
cfg = forall a. a -> [a] -> [a]
intersperse Widget n
v
| Bool
otherwise = forall a. a -> a
id
topLeftCorner :: Widget n
topLeftCorner :: forall n. Widget n
topLeftCorner = forall n. Edges Bool -> Widget n
joinableBorder forall a b. (a -> b) -> a -> b
$ forall a. a -> a -> a -> a -> Edges a
Edges Bool
False Bool
True Bool
False Bool
True
topRightCorner :: Widget n
topRightCorner :: forall n. Widget n
topRightCorner = forall n. Edges Bool -> Widget n
joinableBorder forall a b. (a -> b) -> a -> b
$ forall a. a -> a -> a -> a -> Edges a
Edges Bool
False Bool
True Bool
True Bool
False
bottomLeftCorner :: Widget n
bottomLeftCorner :: forall n. Widget n
bottomLeftCorner = forall n. Edges Bool -> Widget n
joinableBorder forall a b. (a -> b) -> a -> b
$ forall a. a -> a -> a -> a -> Edges a
Edges Bool
True Bool
False Bool
False Bool
True
bottomRightCorner :: Widget n
bottomRightCorner :: forall n. Widget n
bottomRightCorner = forall n. Edges Bool -> Widget n
joinableBorder forall a b. (a -> b) -> a -> b
$ forall a. a -> a -> a -> a -> Edges a
Edges Bool
True Bool
False Bool
True Bool
False
alignColumns :: [ColumnAlignment]
-> [Int]
-> [Widget n]
-> [Widget n]
alignColumns :: forall n. [ColumnAlignment] -> [Int] -> [Widget n] -> [Widget n]
alignColumns [ColumnAlignment]
as [Int]
widths [Widget n]
cells =
(\(Int
w, ColumnAlignment
a, Widget n
c) -> forall n. Int -> ColumnAlignment -> Widget n -> Widget n
applyColAlignment Int
w ColumnAlignment
a Widget n
c) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
zip3 [Int]
widths [ColumnAlignment]
as [Widget n]
cells
applyColAlignment :: Int -> ColumnAlignment -> Widget n -> Widget n
applyColAlignment :: forall n. Int -> ColumnAlignment -> Widget n -> Widget n
applyColAlignment Int
width ColumnAlignment
align Widget n
w =
forall n. Int -> Widget n -> Widget n
hLimit Int
width forall a b. (a -> b) -> a -> b
$ case ColumnAlignment
align of
ColumnAlignment
AlignLeft -> forall n. Padding -> Widget n -> Widget n
padRight Padding
Max Widget n
w
ColumnAlignment
AlignCenter -> forall n. Widget n -> Widget n
hCenter Widget n
w
ColumnAlignment
AlignRight -> forall n. Padding -> Widget n -> Widget n
padLeft Padding
Max Widget n
w
applyRowAlignment :: Int -> RowAlignment -> Widget n -> Widget n
applyRowAlignment :: forall n. Int -> RowAlignment -> Widget n -> Widget n
applyRowAlignment Int
rHeight RowAlignment
align Widget n
w =
forall n. Int -> Widget n -> Widget n
vLimit Int
rHeight forall a b. (a -> b) -> a -> b
$ case RowAlignment
align of
RowAlignment
AlignTop -> forall n. Padding -> Widget n -> Widget n
padBottom Padding
Max Widget n
w
RowAlignment
AlignMiddle -> forall n. Widget n -> Widget n
vCenter Widget n
w
RowAlignment
AlignBottom -> forall n. Padding -> Widget n -> Widget n
padTop Padding
Max Widget n
w