gdritter repos brick-table / 6234b25
Use BorderStyle to draw rich borders Getty Ritter 3 years ago
1 changed file(s) with 37 addition(s) and 10 deletion(s). Collapse all Expand all
22 ( Table(..)
33 , table
44 , renderTable
5 , renderTableWithStyle
56 -- * Event Handlers
67 , TableEvent(..)
78 , handleTableEvent
1617 ) where
1718
1819 import qualified Brick as Brick
20 import qualified Brick.Widgets.Border.Style as Brick
1921 import qualified Data.Array as A
2022 import qualified Data.Ix as Ix
2123 import qualified Data.List as L
216218 -> Brick.EventM n (Table e n)
217219 handleTableEvent e sp = return (applyEvent e sp)
218220
219 -- | Render a table to a "brick" 'Widget'.
221 -- | Render a table to a "brick" 'Widget' using the default border
222 -- style.
220223 renderTable :: Bool -> Table e n -> Brick.Widget n
221224 renderTable spFocus sp =
225 renderTableWithStyle spFocus sp Brick.defaultBorderStyle
226
227 -- | Render a table to a "brick" 'Widget' using a custom border style.
228 renderTableWithStyle :: Bool -> Table e n -> Brick.BorderStyle
229 -> Brick.Widget n
230 renderTableWithStyle spFocus sp bs =
222231 let (_, (maxX, maxY)) = A.bounds (tableContents sp)
223 in Brick.hBox $ L.intersperse (Brick.hLimit 1 (Brick.fill '│'))
224 [ Brick.vBox $ L.intersperse (Brick.vLimit 1 (Brick.fill '─'))
225 [ Brick.padLeft Brick.Max $ tableDraw sp item isFocus
226 | y <- [0..maxY]
227 , let item = tableContents sp A.! (x, y)
228 isFocus = spFocus && (tableCurIndex sp `Ix.inRange` (x, y))
229 ]
230 | x <- [0..maxX]
231 ]
232 vert = Brick.bsVertical bs
233 horz = Brick.bsHorizontal bs
234 horzDivider = Brick.vLimit 1 (Brick.fill horz)
235 vertDivider t b i = Brick.vBox $ [Brick.str [t]] ++ ls ++ [Brick.str [b]]
236 where ls = (take (2 * maxY + 1) (cycle [ Brick.str [vert]
237 , Brick.str [i]
238 ]))
239 cols x = L.intersperse horzDivider
240 [ Brick.padLeft Brick.Max $ tableDraw sp item isFocus
241 | y <- [0..maxY]
242 , let item = tableContents sp A.! (x, y)
243 isFocus = spFocus && (tableCurIndex sp `Ix.inRange` (x, y))
244 ]
245 rows = L.intersperse
246 (vertDivider (Brick.bsIntersectT bs)
247 (Brick.bsIntersectB bs)
248 (Brick.bsIntersectFull bs))
249 [ Brick.vBox $ [horzDivider] ++ cols x ++ [horzDivider]
250 | x <- [0..maxX]
251 ]
252 in Brick.hBox $ [vertDivider (Brick.bsCornerTL bs)
253 (Brick.bsCornerBL bs)
254 (Brick.bsIntersectL bs)] ++
255 rows ++
256 [vertDivider (Brick.bsCornerTR bs)
257 (Brick.bsCornerBR bs)
258 (Brick.bsIntersectR bs)]