Use BorderStyle to draw rich borders
    
    
      
        Getty Ritter
        8 years ago
      
    
    
  
  
  | 2 | 2 | ( Table(..) | 
| 3 | 3 | , table | 
| 4 | 4 | , renderTable | 
| 5 | , renderTableWithStyle | |
| 5 | 6 | -- * Event Handlers | 
| 6 | 7 | , TableEvent(..) | 
| 7 | 8 | , handleTableEvent | 
| 16 | 17 | ) where | 
| 17 | 18 | |
| 18 | 19 | import qualified Brick as Brick | 
| 20 | import qualified Brick.Widgets.Border.Style as Brick | |
| 19 | 21 | import qualified Data.Array as A | 
| 20 | 22 | import qualified Data.Ix as Ix | 
| 21 | 23 | import qualified Data.List as L | 
| 216 | 218 | -> Brick.EventM n (Table e n) | 
| 217 | 219 | handleTableEvent e sp = return (applyEvent e sp) | 
| 218 | 220 | |
| 219 | -- | Render a table to a "brick" 'Widget' | |
| 221 | -- | Render a table to a "brick" 'Widget' using the default border | |
| 222 | -- style. | |
| 220 | 223 | renderTable :: Bool -> Table e n -> Brick.Widget n | 
| 221 | 224 | 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 = | |
| 222 | 231 | 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)] | |