{-# LANGUAGE KindSignatures #-}
module Brick.Widgets.List.Search (
IncludeCurrent
, searchListForward
, searchListBackward
, listSearchDown
, listSearchUp
, listSearchFromBeginning
, listSearchFromEnd
, listSearchPageUp
, listSearchPageDown
, Searchable(..)
) where
import Prelude hiding (take, drop)
import Lens.Micro
import qualified Data.Vector as V
import Data.Vector hiding (take, drop, modify)
import qualified Data.Sequence as S
import Data.Sequence hiding (take, drop)
import qualified Brick.Widgets.List as L
import Brick.Types
type IncludeCurrent = Bool
searchListForward :: Searchable t
=> IncludeCurrent
-> (e -> Bool)
-> L.GenericList n t e
-> Maybe Int
searchListForward :: forall (t :: * -> *) e n.
Searchable t =>
IncludeCurrent
-> (e -> IncludeCurrent) -> GenericList n t e -> Maybe Int
searchListForward IncludeCurrent
incCur e -> IncludeCurrent
test GenericList n t e
l = let
searchForward :: t -> t e -> Maybe t
searchForward t
idx t e
es = case forall (t :: * -> *) a. Searchable t => t a -> Maybe (a, t a)
viewHead t e
es of
Maybe (e, t e)
Nothing -> forall a. Maybe a
Nothing
Just (e
e, t e
es') -> if e -> IncludeCurrent
test e
e
then forall a. a -> Maybe a
Just t
idx
else t -> t e -> Maybe t
searchForward (t
idxforall a. Num a => a -> a -> a
+t
1) t e
es'
start :: Int
start = case GenericList n t e
l forall s a. s -> Getting a s a -> a
^. forall n (t :: * -> *) e. Lens' (GenericList n t e) (Maybe Int)
L.listSelectedL of
Maybe Int
Nothing -> Int
0
Just Int
i -> Int
i forall a. Num a => a -> a -> a
+ if IncludeCurrent
incCur then Int
0 else Int
1
es :: t e
es = forall (t :: * -> *) a. Searchable t => Int -> t a -> t a
drop Int
start forall a b. (a -> b) -> a -> b
$ GenericList n t e
l forall s a. s -> Getting a s a -> a
^. forall n (t1 :: * -> *) e1 (t2 :: * -> *) e2.
Lens (GenericList n t1 e1) (GenericList n t2 e2) (t1 e1) (t2 e2)
L.listElementsL
in forall {t :: * -> *} {t}.
(Searchable t, Num t) =>
t -> t e -> Maybe t
searchForward Int
start t e
es
searchListBackward :: Searchable t
=> IncludeCurrent
-> (e -> Bool)
-> L.GenericList n t e
-> Maybe Int
searchListBackward :: forall (t :: * -> *) e n.
Searchable t =>
IncludeCurrent
-> (e -> IncludeCurrent) -> GenericList n t e -> Maybe Int
searchListBackward IncludeCurrent
incCur e -> IncludeCurrent
test GenericList n t e
l = let
searchBackward :: t -> t e -> Maybe t
searchBackward t
idx t e
es = case forall (t :: * -> *) a. Searchable t => t a -> Maybe (t a, a)
viewLast t e
es of
Maybe (t e, e)
Nothing -> forall a. Maybe a
Nothing
Just (t e
es', e
e) -> if e -> IncludeCurrent
test e
e
then forall a. a -> Maybe a
Just t
idx
else t -> t e -> Maybe t
searchBackward (t
idxforall a. Num a => a -> a -> a
-t
1) t e
es'
start :: Int
start = case GenericList n t e
l forall s a. s -> Getting a s a -> a
^. forall n (t :: * -> *) e. Lens' (GenericList n t e) (Maybe Int)
L.listSelectedL of
Maybe Int
Nothing -> Int
0
Just Int
i -> Int
i forall a. Num a => a -> a -> a
+ if IncludeCurrent
incCur then Int
1 else Int
0
es :: t e
es = forall (t :: * -> *) a. Searchable t => Int -> t a -> t a
take Int
start forall a b. (a -> b) -> a -> b
$ GenericList n t e
l forall s a. s -> Getting a s a -> a
^. forall n (t1 :: * -> *) e1 (t2 :: * -> *) e2.
Lens (GenericList n t1 e1) (GenericList n t2 e2) (t1 e1) (t2 e2)
L.listElementsL
in forall {t :: * -> *} {t}.
(Searchable t, Num t) =>
t -> t e -> Maybe t
searchBackward (Int
startforall a. Num a => a -> a -> a
-Int
1) t e
es
listSearchDown :: Searchable t
=> (e -> Bool)
-> L.GenericList n t e
-> L.GenericList n t e
listSearchDown :: forall (t :: * -> *) e n.
Searchable t =>
(e -> IncludeCurrent) -> GenericList n t e -> GenericList n t e
listSearchDown e -> IncludeCurrent
test GenericList n t e
l = case forall (t :: * -> *) e n.
Searchable t =>
IncludeCurrent
-> (e -> IncludeCurrent) -> GenericList n t e -> Maybe Int
searchListForward IncludeCurrent
False e -> IncludeCurrent
test GenericList n t e
l of
Maybe Int
Nothing -> GenericList n t e
l
Just Int
idx -> GenericList n t e
l forall a b. a -> (a -> b) -> b
& forall n (t :: * -> *) e. Lens' (GenericList n t e) (Maybe Int)
L.listSelectedL forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Int
idx
listSearchUp :: Searchable t
=> (e -> Bool)
-> L.GenericList n t e
-> L.GenericList n t e
listSearchUp :: forall (t :: * -> *) e n.
Searchable t =>
(e -> IncludeCurrent) -> GenericList n t e -> GenericList n t e
listSearchUp e -> IncludeCurrent
test GenericList n t e
l = case forall (t :: * -> *) e n.
Searchable t =>
IncludeCurrent
-> (e -> IncludeCurrent) -> GenericList n t e -> Maybe Int
searchListBackward IncludeCurrent
False e -> IncludeCurrent
test GenericList n t e
l of
Maybe Int
Nothing -> GenericList n t e
l
Just Int
idx -> GenericList n t e
l forall a b. a -> (a -> b) -> b
& forall n (t :: * -> *) e. Lens' (GenericList n t e) (Maybe Int)
L.listSelectedL forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Int
idx
listSearchFromBeginning :: (Searchable t, Foldable t, L.Splittable t)
=> (e -> Bool)
-> L.GenericList n t e
-> L.GenericList n t e
listSearchFromBeginning :: forall (t :: * -> *) e n.
(Searchable t, Foldable t, Splittable t) =>
(e -> IncludeCurrent) -> GenericList n t e -> GenericList n t e
listSearchFromBeginning e -> IncludeCurrent
test GenericList n t e
l = case forall (t :: * -> *) e n.
Searchable t =>
IncludeCurrent
-> (e -> IncludeCurrent) -> GenericList n t e -> Maybe Int
searchListForward IncludeCurrent
True e -> IncludeCurrent
test (forall (t :: * -> *) n e.
(Foldable t, Splittable t) =>
GenericList n t e -> GenericList n t e
L.listMoveToBeginning GenericList n t e
l) of
Maybe Int
Nothing -> GenericList n t e
l
Just Int
idx -> GenericList n t e
l forall a b. a -> (a -> b) -> b
& forall n (t :: * -> *) e. Lens' (GenericList n t e) (Maybe Int)
L.listSelectedL forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Int
idx
listSearchFromEnd :: (Searchable t, Foldable t, L.Splittable t)
=> (e -> Bool)
-> L.GenericList n t e
-> L.GenericList n t e
listSearchFromEnd :: forall (t :: * -> *) e n.
(Searchable t, Foldable t, Splittable t) =>
(e -> IncludeCurrent) -> GenericList n t e -> GenericList n t e
listSearchFromEnd e -> IncludeCurrent
test GenericList n t e
l = case forall (t :: * -> *) e n.
Searchable t =>
IncludeCurrent
-> (e -> IncludeCurrent) -> GenericList n t e -> Maybe Int
searchListBackward IncludeCurrent
True e -> IncludeCurrent
test (forall (t :: * -> *) n e.
(Foldable t, Splittable t) =>
GenericList n t e -> GenericList n t e
L.listMoveToEnd GenericList n t e
l) of
Maybe Int
Nothing -> GenericList n t e
l
Just Int
idx -> GenericList n t e
l forall a b. a -> (a -> b) -> b
& forall n (t :: * -> *) e. Lens' (GenericList n t e) (Maybe Int)
L.listSelectedL forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Int
idx
listSearchPageUp :: (Searchable t, Foldable t, L.Splittable t, Ord n)
=> (e -> Bool)
-> EventM n (L.GenericList n t e) ()
listSearchPageUp :: forall (t :: * -> *) n e.
(Searchable t, Foldable t, Splittable t, Ord n) =>
(e -> IncludeCurrent) -> EventM n (GenericList n t e) ()
listSearchPageUp e -> IncludeCurrent
test = do
GenericList n t e
origL <- forall s (m :: * -> *). MonadState s m => m s
get
forall (t :: * -> *) n e.
(Foldable t, Splittable t, Ord n) =>
EventM n (GenericList n t e) ()
L.listMovePageUp
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify forall a b. (a -> b) -> a -> b
$ \GenericList n t e
l -> case forall (t :: * -> *) e n.
Searchable t =>
IncludeCurrent
-> (e -> IncludeCurrent) -> GenericList n t e -> Maybe Int
searchListBackward IncludeCurrent
True e -> IncludeCurrent
test GenericList n t e
l of
Maybe Int
Nothing -> case forall (t :: * -> *) e n.
Searchable t =>
IncludeCurrent
-> (e -> IncludeCurrent) -> GenericList n t e -> Maybe Int
searchListForward IncludeCurrent
True e -> IncludeCurrent
test GenericList n t e
l of
Maybe Int
Nothing -> GenericList n t e
origL
Just Int
idx -> GenericList n t e
l forall a b. a -> (a -> b) -> b
& forall n (t :: * -> *) e. Lens' (GenericList n t e) (Maybe Int)
L.listSelectedL forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Int
idx
Just Int
idx -> GenericList n t e
l forall a b. a -> (a -> b) -> b
& forall n (t :: * -> *) e. Lens' (GenericList n t e) (Maybe Int)
L.listSelectedL forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Int
idx
listSearchPageDown :: (Searchable t, Foldable t, L.Splittable t, Ord n)
=> (e -> Bool)
-> EventM n (L.GenericList n t e) ()
listSearchPageDown :: forall (t :: * -> *) n e.
(Searchable t, Foldable t, Splittable t, Ord n) =>
(e -> IncludeCurrent) -> EventM n (GenericList n t e) ()
listSearchPageDown e -> IncludeCurrent
test = do
GenericList n t e
origL <- forall s (m :: * -> *). MonadState s m => m s
get
forall (t :: * -> *) n e.
(Foldable t, Splittable t, Ord n) =>
EventM n (GenericList n t e) ()
L.listMovePageDown
forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
modify forall a b. (a -> b) -> a -> b
$ \GenericList n t e
l -> case forall (t :: * -> *) e n.
Searchable t =>
IncludeCurrent
-> (e -> IncludeCurrent) -> GenericList n t e -> Maybe Int
searchListForward IncludeCurrent
True e -> IncludeCurrent
test GenericList n t e
l of
Maybe Int
Nothing -> case forall (t :: * -> *) e n.
Searchable t =>
IncludeCurrent
-> (e -> IncludeCurrent) -> GenericList n t e -> Maybe Int
searchListBackward IncludeCurrent
True e -> IncludeCurrent
test GenericList n t e
l of
Maybe Int
Nothing -> GenericList n t e
origL
Just Int
idx -> GenericList n t e
l forall a b. a -> (a -> b) -> b
& forall n (t :: * -> *) e. Lens' (GenericList n t e) (Maybe Int)
L.listSelectedL forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Int
idx
Just Int
idx -> GenericList n t e
l forall a b. a -> (a -> b) -> b
& forall n (t :: * -> *) e. Lens' (GenericList n t e) (Maybe Int)
L.listSelectedL forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Int
idx
class Searchable (t :: * -> *) where
viewHead :: t a -> Maybe (a, t a)
viewLast :: t a -> Maybe (t a, a)
take :: Int -> t a -> t a
drop :: Int -> t a -> t a
instance Searchable Vector where
viewHead :: forall a. Vector a -> Maybe (a, Vector a)
viewHead = forall a. Vector a -> Maybe (a, Vector a)
uncons
viewLast :: forall a. Vector a -> Maybe (Vector a, a)
viewLast = forall a. Vector a -> Maybe (Vector a, a)
unsnoc
take :: forall a. Int -> Vector a -> Vector a
take = forall a. Int -> Vector a -> Vector a
V.take
drop :: forall a. Int -> Vector a -> Vector a
drop = forall a. Int -> Vector a -> Vector a
V.drop
instance Searchable Seq where
viewHead :: forall a. Seq a -> Maybe (a, Seq a)
viewHead Seq a
s = case forall a. Seq a -> ViewL a
S.viewl Seq a
s of
ViewL a
EmptyL -> forall a. Maybe a
Nothing
a
a :< Seq a
s' -> forall a. a -> Maybe a
Just (a
a, Seq a
s')
viewLast :: forall a. Seq a -> Maybe (Seq a, a)
viewLast Seq a
s = case forall a. Seq a -> ViewR a
S.viewr Seq a
s of
ViewR a
EmptyR -> forall a. Maybe a
Nothing
Seq a
s' :> a
a -> forall a. a -> Maybe a
Just (Seq a
s', a
a)
take :: forall a. Int -> Seq a -> Seq a
take = forall a. Int -> Seq a -> Seq a
S.take
drop :: forall a. Int -> Seq a -> Seq a
drop = forall a. Int -> Seq a -> Seq a
S.drop