brick-tabular-list-1.0.0.0: src/Brick/Widgets/TabularList/Internal/Common.hs
module Brick.Widgets.TabularList.Internal.Common (
AvailHeight
, visibleRowsWithStart
, visibleRows
, zipWithVisibleRowsAndIndexes
) 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
-- | Height available for 'GenericList'
type AvailHeight = Int
-- | Return visible rows and the index of the first visible row, given 'GenericList' and height available for list.
visibleRowsWithStart :: GenericList n Seq row -> AvailHeight -> ([row], Index)
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.
visibleRows :: GenericList n Seq e -> AvailHeight -> [e]
visibleRows l aH = fst $ visibleRowsWithStart l aH
-- | Zip visible rows and their row indexes with a function.
zipWithVisibleRowsAndIndexes :: GenericList n Seq e -> AvailHeight -> (e -> Index -> c) -> [c]
zipWithVisibleRowsAndIndexes l aH f = let (es, s) = visibleRowsWithStart l aH
in zipWith f es [s..]