module Brick.Focus
( FocusRing
, focusRing
, focusNext
, focusPrev
, focusGetCurrent
, focusRingCursor
)
where
import Control.Lens ((^.))
import Data.Maybe (listToMaybe)
import Brick.Types
data FocusRing = FocusRingEmpty
| FocusRingNonempty ![Name] !Int
focusRing :: [Name] -> FocusRing
focusRing [] = FocusRingEmpty
focusRing names = FocusRingNonempty names 0
focusNext :: FocusRing -> FocusRing
focusNext FocusRingEmpty = FocusRingEmpty
focusNext (FocusRingNonempty ns i) = FocusRingNonempty ns i'
where
i' = (i + 1) `mod` (length ns)
focusPrev :: FocusRing -> FocusRing
focusPrev FocusRingEmpty = FocusRingEmpty
focusPrev (FocusRingNonempty ns i) = FocusRingNonempty ns i'
where
i' = (i + (length ns) 1) `mod` (length ns)
focusGetCurrent :: FocusRing -> Maybe Name
focusGetCurrent FocusRingEmpty = Nothing
focusGetCurrent (FocusRingNonempty ns i) = Just $ ns !! i
focusRingCursor :: (a -> FocusRing)
-> a
-> [CursorLocation]
-> Maybe CursorLocation
focusRingCursor getRing st ls =
listToMaybe $ filter isCurrent ls
where
isCurrent cl = cl^.cursorLocationNameL ==
(focusGetCurrent $ getRing st)