module Brick.Widgets.TabularList.Internal.Common ( AvailHeight(..) , visibleRowIdx , sz ) 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 import Brick.Types import Brick.Widgets.Core -- | Height available for 'GenericList' newtype AvailHeight = AvlH Int deriving (Show, Eq) -- | Return visible rows and their row indexes. -- -- The visible rows are rows that are visible when the current row is either at the top or the bottom. visibleRowIdx :: GenericList n Seq e -> AvailHeight -> ([e], [Index]) visibleRowIdx l (AvlH h) = let idx = fromMaybe 0 (l^.listSelectedL) numPerHeight = case h `divMod` (l^.listItemHeightL) of (nph, 0) -> nph (nph, _) -> nph + 1 -- If the current row is at the bottom, the start should be visible. start = max 0 $ idx - numPerHeight + 1 -- If the current row is at the top, the row at start + length - 1 should be visible. length = numPerHeight * 2 rows = toList $ slice start length $ l^.listElementsL in (rows, [Ix start..]) -- | It is a shortened version of 'setAvailableSize'. sz :: (Int, Int) -> Widget n -> Widget n sz (w, h) = if w <= 0 then const emptyWidget else setAvailableSize (w, h)