-- |
-- SPDX-License-Identifier: BSD-3-Clause
--
-- A special modified version of 'Brick.Widgets.List.handleListEvent'
-- to deal with skipping over separators.
module Swarm.TUI.List (handleListEventWithSeparators) where

import Brick (EventM)
import Brick.Widgets.List qualified as BL
import Brick.Widgets.List.Skip
import Graphics.Vty qualified as V

-- | Handle a list event, taking an extra predicate to identify which
--   list elements are separators; separators will be skipped if
--   possible.
handleListEventWithSeparators ::
  (Foldable t, BL.Splittable t, Ord n, Searchable t) =>
  V.Event ->
  -- | Is this element a separator?
  (e -> Bool) ->
  EventM n (BL.GenericList n t e) ()
handleListEventWithSeparators :: forall (t :: * -> *) n e.
(Foldable t, Splittable t, Ord n, Searchable t) =>
Event -> (e -> Bool) -> EventM n (GenericList n t e) ()
handleListEventWithSeparators Event
e e -> Bool
isSep =
  forall (t :: * -> *) n e.
(Foldable t, Splittable t, Searchable t, Ord n) =>
(e -> Bool) -> Move -> EventM n (GenericList n t e) ()
listSkip e -> Bool
isSep Move
movement
 where
  movement :: Move
movement = case Event
e of
    V.EvKey Key
V.KUp [] -> Amount -> Dir -> Move
Move Amount
One Dir
Bwd
    V.EvKey (V.KChar Char
'k') [] -> Amount -> Dir -> Move
Move Amount
One Dir
Bwd
    V.EvKey Key
V.KDown [] -> Amount -> Dir -> Move
Move Amount
One Dir
Fwd
    V.EvKey (V.KChar Char
'j') [] -> Amount -> Dir -> Move
Move Amount
One Dir
Fwd
    V.EvKey Key
V.KHome [] -> Amount -> Dir -> Move
Move Amount
Most Dir
Bwd
    V.EvKey Key
V.KEnd [] -> Amount -> Dir -> Move
Move Amount
Most Dir
Fwd
    V.EvKey Key
V.KPageDown [] -> Amount -> Dir -> Move
Move Amount
Page Dir
Fwd
    V.EvKey Key
V.KPageUp [] -> Amount -> Dir -> Move
Move Amount
Page Dir
Bwd
    Event
_ -> Move
NoMove