Table: generalize API for default row and column alignment

This commit is contained in:
Jonathan Daugherty 2021-01-31 21:03:07 -08:00
parent 6df3642cb5
commit f569773cf8

View File

@ -15,6 +15,8 @@ module Brick.Widgets.Table
, alignBottom , alignBottom
, setColAlignment , setColAlignment
, setRowAlignment , setRowAlignment
, setDefaultColAlignment
, setDefaultRowAlignment
, surroundingBorder , surroundingBorder
, rowBorders , rowBorders
, columnBorders , columnBorders
@ -37,7 +39,7 @@ import Brick.Widgets.Border
-- | Column alignment modes. -- | Column alignment modes.
data ColumnAlignment = data ColumnAlignment =
AlignLeft AlignLeft
-- ^ Align all cells to the left (the default). -- ^ Align all cells to the left.
| AlignCenter | AlignCenter
-- ^ Center the content horizontally in all cells in the column. -- ^ Center the content horizontally in all cells in the column.
| AlignRight | AlignRight
@ -47,7 +49,7 @@ data ColumnAlignment =
-- | Row alignment modes. -- | Row alignment modes.
data RowAlignment = data RowAlignment =
AlignTop AlignTop
-- ^ Align all cells to the top (the default). -- ^ Align all cells to the top.
| AlignMiddle | AlignMiddle
-- ^ Center the content vertically in all cells in the row. -- ^ Center the content vertically in all cells in the row.
| AlignBottom | AlignBottom
@ -59,6 +61,8 @@ data Table n =
Table { columnAlignments :: M.Map Int ColumnAlignment Table { columnAlignments :: M.Map Int ColumnAlignment
, rowAlignments :: M.Map Int RowAlignment , rowAlignments :: M.Map Int RowAlignment
, tableRows :: [[Widget n]] , tableRows :: [[Widget n]]
, defaultColumnAlignment :: ColumnAlignment
, defaultRowAlignment :: RowAlignment
, drawSurroundingBorder :: Bool , drawSurroundingBorder :: Bool
, drawRowBorders :: Bool , drawRowBorders :: Bool
, drawColumnBorders :: Bool , drawColumnBorders :: Bool
@ -72,6 +76,9 @@ data Table n =
-- By default, all columns are left-aligned. Use the alignment functions -- By default, all columns are left-aligned. Use the alignment functions
-- in this module to change that behavior. -- in this module to change that behavior.
-- --
-- By default, all rows are top-aligned. Use the alignment functions in
-- this module to change that behavior.
--
-- By default, the table will draw borders between columns, between -- By default, the table will draw borders between columns, between
-- rows, and around the outside of the table. Border-drawing behavior -- rows, and around the outside of the table. Border-drawing behavior
-- can be configured with the API in this module. Note that tables -- can be configured with the API in this module. Note that tables
@ -95,6 +102,8 @@ table rows =
, drawSurroundingBorder = True , drawSurroundingBorder = True
, drawRowBorders = True , drawRowBorders = True
, drawColumnBorders = True , drawColumnBorders = True
, defaultColumnAlignment = AlignLeft
, defaultRowAlignment = AlignTop
} }
-- | Configure whether the table draws a border on its exterior. -- | Configure whether the table draws a border on its exterior.
@ -154,6 +163,18 @@ setRowAlignment :: RowAlignment -> Int -> Table n -> Table n
setRowAlignment a row t = setRowAlignment a row t =
t { rowAlignments = M.insert row a (rowAlignments t) } t { rowAlignments = M.insert row a (rowAlignments t) }
-- | Set the default column alignment for columns with no explicitly
-- configured alignment.
setDefaultColAlignment :: ColumnAlignment -> Table n -> Table n
setDefaultColAlignment a t =
t { defaultColumnAlignment = a }
-- | Set the default row alignment for rows with no explicitly
-- configured alignment.
setDefaultRowAlignment :: RowAlignment -> Table n -> Table n
setDefaultRowAlignment a t =
t { defaultRowAlignment = a }
-- | Render the table. -- | Render the table.
renderTable :: Table n -> Widget n renderTable :: Table n -> Widget n
renderTable t = renderTable t =
@ -164,9 +185,9 @@ renderTable t =
cellResults <- forM rows $ mapM render cellResults <- forM rows $ mapM render
let rowHeights = rowHeight <$> cellResults let rowHeights = rowHeight <$> cellResults
colWidths = colWidth <$> byColumn colWidths = colWidth <$> byColumn
allRowAligns = (\i -> M.findWithDefault AlignTop i (rowAlignments t)) <$> allRowAligns = (\i -> M.findWithDefault (defaultRowAlignment t) i (rowAlignments t)) <$>
[0..length rowHeights - 1] [0..length rowHeights - 1]
allColAligns = (\i -> M.findWithDefault AlignLeft i (columnAlignments t)) <$> allColAligns = (\i -> M.findWithDefault (defaultColumnAlignment t) i (columnAlignments t)) <$>
[0..length byColumn - 1] [0..length byColumn - 1]
rowHeight = maximum . fmap (imageHeight . image) rowHeight = maximum . fmap (imageHeight . image)
colWidth = maximum . fmap (imageWidth . image) colWidth = maximum . fmap (imageWidth . image)