module Brick.Widgets.TabularList.Internal.Common ( visibleRows , visibleRowHdrs , visibleRowsAndRowHdrs ) where import Brick.Widgets.TabularList.Types -- base import Data.Maybe (fromMaybe, catMaybes) import Data.Foldable (toList) -- Third party libraries import Lens.Micro import Data.Sequence (Seq) -- Brick import Brick.Widgets.List type Start = Int type AvailHeight = Int visibleRowsWithStart :: GenericList n Seq row -> AvailHeight -> ([row], Start) visibleRowsWithStart l aH = let idx = fromMaybe 0 (l^.listSelectedL) numPerHeight = case aH `divMod` (l^.listItemHeightL) of (nph, 0) -> nph (nph, _) -> nph + 1 start = max 0 $ idx - numPerHeight + 1 length = numPerHeight * 2 rows = toList $ slice start length $ l^.listElementsL in (rows, start) -- | Return visible rows, given 'GenericList' and height available for list elements visibleRows :: GenericList n Seq row -> AvailHeight -> [row] visibleRows l aH = visibleRowsWithStart l aH ^. _1 -- | Return visible row headers, given 'GenericList' and height available for list elements visibleRowHdrs :: GenericList n Seq row -> AvailHeight -> (row -> RowIndex -> Maybe rowH) -> [rowH] visibleRowHdrs l aH rowH = visibleRowsAndRowHdrs l aH rowH ^. _2 -- | Return visible rows and visible row headers, given 'GenericList', available height, and row header function. visibleRowsAndRowHdrs :: GenericList n Seq row -> AvailHeight -> (row -> RowIndex -> Maybe rowH) -> ([row], [rowH]) visibleRowsAndRowHdrs l aH rowH = let (rows, start) = visibleRowsWithStart l aH in (rows, catMaybes $ zipWith rowH rows [start..])