-- | Module    : Hum.Modes.PromptMode
-- Copyright   : (c) Itai Y. Efrat 2020-2021
-- License     : GPLv2-or-later (see LICENSE)
-- Maintainer  : Itai Y. Efrat <itai3397@gmail.com>
--
-- Functions for prompts.


module Hum.Modes.PromptMode where

import           Brick.Widgets.Edit      hiding ( decodeUtf8 )
import           Brick.Widgets.List
import           Brick.Types
import           Brick.Main
import           Hum.Types
import           Hum.Utils
import           Graphics.Vty.Input.Events
import qualified Data.Text.Zipper              as Z
                                         hiding ( textZipper )
import           Control.Lens
import qualified Network.MPD                   as MPD
import qualified Data.Vector                   as V

-- | Prompt key event dispatch.
handlePromptEvent
    :: HumState -> BrickEvent Name HumEvent -> EventM Name (Next HumState)
handlePromptEvent :: HumState -> BrickEvent Name HumEvent -> EventM Name (Next HumState)
handlePromptEvent HumState
s BrickEvent Name HumEvent
e = case HumState
s HumState -> Getting PromptType HumState PromptType -> PromptType
forall s a. s -> Getting a s a -> a
^. (Prompts -> Const PromptType Prompts)
-> HumState -> Const PromptType HumState
Lens' HumState Prompts
promptsL ((Prompts -> Const PromptType Prompts)
 -> HumState -> Const PromptType HumState)
-> ((PromptType -> Const PromptType PromptType)
    -> Prompts -> Const PromptType Prompts)
-> Getting PromptType HumState PromptType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (PromptType -> Const PromptType PromptType)
-> Prompts -> Const PromptType Prompts
Lens' Prompts PromptType
currentPromptL of
  PromptType
PlSelectPrompt -> HumState -> BrickEvent Name HumEvent -> EventM Name (Next HumState)
handlePlSelectPromptEvent HumState
s BrickEvent Name HumEvent
e
  PromptType
TextPrompt -> HumState -> BrickEvent Name HumEvent -> EventM Name (Next HumState)
handleTextPromptEvent HumState
s BrickEvent Name HumEvent
e
  PromptType
YNPrompt -> HumState -> BrickEvent Name HumEvent -> EventM Name (Next HumState)
handleYNPromptEvent HumState
s BrickEvent Name HumEvent
e

-- | Handles key events for playlist select prompt.
handlePlSelectPromptEvent
    :: HumState -> BrickEvent Name HumEvent -> EventM Name (Next HumState)
handlePlSelectPromptEvent :: HumState -> BrickEvent Name HumEvent -> EventM Name (Next HumState)
handlePlSelectPromptEvent HumState
s BrickEvent Name HumEvent
e = case BrickEvent Name HumEvent
e of
  VtyEvent Event
vtye -> case Event
vtye of
    EvKey Key
KEsc [] -> HumState -> EventM Name (Next HumState)
forall s n. s -> EventM n (Next s)
continue
        (HumState -> EventM Name (Next HumState))
-> EventM Name HumState -> EventM Name (Next HumState)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< (HumState
s HumState
-> Getting
     (Bool -> HumState -> EventM Name HumState)
     HumState
     (Bool -> HumState -> EventM Name HumState)
-> Bool
-> HumState
-> EventM Name HumState
forall s a. s -> Getting a s a -> a
^. (Prompts
 -> Const (Bool -> HumState -> EventM Name HumState) Prompts)
-> HumState
-> Const (Bool -> HumState -> EventM Name HumState) HumState
Lens' HumState Prompts
promptsL ((Prompts
  -> Const (Bool -> HumState -> EventM Name HumState) Prompts)
 -> HumState
 -> Const (Bool -> HumState -> EventM Name HumState) HumState)
-> (((Bool -> HumState -> EventM Name HumState)
     -> Const
          (Bool -> HumState -> EventM Name HumState)
          (Bool -> HumState -> EventM Name HumState))
    -> Prompts
    -> Const (Bool -> HumState -> EventM Name HumState) Prompts)
-> Getting
     (Bool -> HumState -> EventM Name HumState)
     HumState
     (Bool -> HumState -> EventM Name HumState)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Bool -> HumState -> EventM Name HumState)
 -> Const
      (Bool -> HumState -> EventM Name HumState)
      (Bool -> HumState -> EventM Name HumState))
-> Prompts
-> Const (Bool -> HumState -> EventM Name HumState) Prompts
Lens' Prompts (Bool -> HumState -> EventM Name HumState)
exitPromptFuncL) Bool
False (HumState
s HumState -> (HumState -> HumState) -> HumState
forall a b. a -> (a -> b) -> b
& (Mode -> Identity Mode) -> HumState -> Identity HumState
Lens' HumState Mode
modeL ((Mode -> Identity Mode) -> HumState -> Identity HumState)
-> Mode -> HumState -> HumState
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Mode
NormalMode)
    EvKey (KChar Char
'q') [] -> HumState -> EventM Name (Next HumState)
forall s n. s -> EventM n (Next s)
continue
      (HumState -> EventM Name (Next HumState))
-> EventM Name HumState -> EventM Name (Next HumState)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< (HumState
s HumState
-> Getting
     (Bool -> HumState -> EventM Name HumState)
     HumState
     (Bool -> HumState -> EventM Name HumState)
-> Bool
-> HumState
-> EventM Name HumState
forall s a. s -> Getting a s a -> a
^. (Prompts
 -> Const (Bool -> HumState -> EventM Name HumState) Prompts)
-> HumState
-> Const (Bool -> HumState -> EventM Name HumState) HumState
Lens' HumState Prompts
promptsL ((Prompts
  -> Const (Bool -> HumState -> EventM Name HumState) Prompts)
 -> HumState
 -> Const (Bool -> HumState -> EventM Name HumState) HumState)
-> (((Bool -> HumState -> EventM Name HumState)
     -> Const
          (Bool -> HumState -> EventM Name HumState)
          (Bool -> HumState -> EventM Name HumState))
    -> Prompts
    -> Const (Bool -> HumState -> EventM Name HumState) Prompts)
-> Getting
     (Bool -> HumState -> EventM Name HumState)
     HumState
     (Bool -> HumState -> EventM Name HumState)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Bool -> HumState -> EventM Name HumState)
 -> Const
      (Bool -> HumState -> EventM Name HumState)
      (Bool -> HumState -> EventM Name HumState))
-> Prompts
-> Const (Bool -> HumState -> EventM Name HumState) Prompts
Lens' Prompts (Bool -> HumState -> EventM Name HumState)
exitPromptFuncL) Bool
False (HumState
s HumState -> (HumState -> HumState) -> HumState
forall a b. a -> (a -> b) -> b
& (Mode -> Identity Mode) -> HumState -> Identity HumState
Lens' HumState Mode
modeL ((Mode -> Identity Mode) -> HumState -> Identity HumState)
-> Mode -> HumState -> HumState
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Mode
NormalMode)
    EvKey (KChar Char
'j') [] -> do
      HumState -> EventM Name (Next HumState)
forall s n. s -> EventM n (Next s)
continue (HumState -> EventM Name (Next HumState))
-> HumState -> EventM Name (Next HumState)
forall a b. (a -> b) -> a -> b
$ HumState
s HumState -> (HumState -> HumState) -> HumState
forall a b. a -> (a -> b) -> b
& (Prompts -> Identity Prompts) -> HumState -> Identity HumState
Lens' HumState Prompts
promptsL ((Prompts -> Identity Prompts) -> HumState -> Identity HumState)
-> ((List Name (Maybe PlaylistName)
     -> Identity (List Name (Maybe PlaylistName)))
    -> Prompts -> Identity Prompts)
-> (List Name (Maybe PlaylistName)
    -> Identity (List Name (Maybe PlaylistName)))
-> HumState
-> Identity HumState
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (List Name (Maybe PlaylistName)
 -> Identity (List Name (Maybe PlaylistName)))
-> Prompts -> Identity Prompts
Lens' Prompts (List Name (Maybe PlaylistName))
plSelectPromptL ((List Name (Maybe PlaylistName)
  -> Identity (List Name (Maybe PlaylistName)))
 -> HumState -> Identity HumState)
-> (List Name (Maybe PlaylistName)
    -> List Name (Maybe PlaylistName))
-> HumState
-> HumState
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ List Name (Maybe PlaylistName) -> List Name (Maybe PlaylistName)
forall (t :: * -> *) n e.
(Foldable t, Splittable t) =>
GenericList n t e -> GenericList n t e
listMoveDown
    EvKey (KChar Char
'k') [] -> do
      HumState -> EventM Name (Next HumState)
forall s n. s -> EventM n (Next s)
continue (HumState -> EventM Name (Next HumState))
-> HumState -> EventM Name (Next HumState)
forall a b. (a -> b) -> a -> b
$ HumState
s HumState -> (HumState -> HumState) -> HumState
forall a b. a -> (a -> b) -> b
& (Prompts -> Identity Prompts) -> HumState -> Identity HumState
Lens' HumState Prompts
promptsL ((Prompts -> Identity Prompts) -> HumState -> Identity HumState)
-> ((List Name (Maybe PlaylistName)
     -> Identity (List Name (Maybe PlaylistName)))
    -> Prompts -> Identity Prompts)
-> (List Name (Maybe PlaylistName)
    -> Identity (List Name (Maybe PlaylistName)))
-> HumState
-> Identity HumState
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (List Name (Maybe PlaylistName)
 -> Identity (List Name (Maybe PlaylistName)))
-> Prompts -> Identity Prompts
Lens' Prompts (List Name (Maybe PlaylistName))
plSelectPromptL ((List Name (Maybe PlaylistName)
  -> Identity (List Name (Maybe PlaylistName)))
 -> HumState -> Identity HumState)
-> (List Name (Maybe PlaylistName)
    -> List Name (Maybe PlaylistName))
-> HumState
-> HumState
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ List Name (Maybe PlaylistName) -> List Name (Maybe PlaylistName)
forall (t :: * -> *) n e.
(Foldable t, Splittable t) =>
GenericList n t e -> GenericList n t e
listMoveUp
    EvKey (KChar Char
'l') [] -> do
      let songs :: Vector Song
songs = HumState
s HumState -> Getting SongList HumState SongList -> SongList
forall s a. s -> Getting a s a -> a
^. Getting SongList HumState SongList
Lens' HumState SongList
queueL SongList -> (SongList -> SongList) -> SongList
forall a b. a -> (a -> b) -> b
& SongList -> SongList
forall (t :: * -> *) n e.
(Filterable t, Traversable t) =>
GenericList n t (e, Bool) -> GenericList n t (e, Bool)
getHighlighted SongList -> ((Song, Bool) -> Song) -> GenericList Name Vector Song
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> (Song, Bool) -> Song
forall a b. (a, b) -> a
fst GenericList Name Vector Song
-> (GenericList Name Vector Song -> Vector Song) -> Vector Song
forall a b. a -> (a -> b) -> b
& GenericList Name Vector Song -> Vector Song
forall n (t :: * -> *) e. GenericList n t e -> t e
listElements
      case HumState
s HumState
-> Getting
     (List Name (Maybe PlaylistName))
     HumState
     (List Name (Maybe PlaylistName))
-> List Name (Maybe PlaylistName)
forall s a. s -> Getting a s a -> a
^. (Prompts -> Const (List Name (Maybe PlaylistName)) Prompts)
-> HumState -> Const (List Name (Maybe PlaylistName)) HumState
Lens' HumState Prompts
promptsL ((Prompts -> Const (List Name (Maybe PlaylistName)) Prompts)
 -> HumState -> Const (List Name (Maybe PlaylistName)) HumState)
-> ((List Name (Maybe PlaylistName)
     -> Const
          (List Name (Maybe PlaylistName)) (List Name (Maybe PlaylistName)))
    -> Prompts -> Const (List Name (Maybe PlaylistName)) Prompts)
-> Getting
     (List Name (Maybe PlaylistName))
     HumState
     (List Name (Maybe PlaylistName))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (List Name (Maybe PlaylistName)
 -> Const
      (List Name (Maybe PlaylistName)) (List Name (Maybe PlaylistName)))
-> Prompts -> Const (List Name (Maybe PlaylistName)) Prompts
Lens' Prompts (List Name (Maybe PlaylistName))
plSelectPromptL List Name (Maybe PlaylistName)
-> (List Name (Maybe PlaylistName)
    -> Maybe (Int, Maybe PlaylistName))
-> Maybe (Int, Maybe PlaylistName)
forall a b. a -> (a -> b) -> b
& List Name (Maybe PlaylistName) -> Maybe (Int, Maybe PlaylistName)
forall (t :: * -> *) n e.
(Splittable t, Foldable t) =>
GenericList n t e -> Maybe (Int, e)
listSelectedElement Maybe (Int, Maybe PlaylistName)
-> ((Int, Maybe PlaylistName) -> Maybe PlaylistName)
-> Maybe (Maybe PlaylistName)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> (Int, Maybe PlaylistName) -> Maybe PlaylistName
forall a b. (a, b) -> b
snd Maybe (Maybe PlaylistName)
-> (Maybe (Maybe PlaylistName) -> Maybe PlaylistName)
-> Maybe PlaylistName
forall a b. a -> (a -> b) -> b
& Maybe (Maybe PlaylistName) -> Maybe PlaylistName
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join of
        Maybe PlaylistName
Nothing -> HumState -> EventM Name (Next HumState)
forall s n. s -> EventM n (Next s)
continue (HumState -> EventM Name (Next HumState))
-> HumState -> EventM Name (Next HumState)
forall a b. (a -> b) -> a -> b
$ HumState
s HumState -> (HumState -> HumState) -> HumState
forall a b. a -> (a -> b) -> b
& (Prompts -> Identity Prompts) -> HumState -> Identity HumState
Lens' HumState Prompts
promptsL ((Prompts -> Identity Prompts) -> HumState -> Identity HumState)
-> ((PromptType -> Identity PromptType)
    -> Prompts -> Identity Prompts)
-> (PromptType -> Identity PromptType)
-> HumState
-> Identity HumState
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (PromptType -> Identity PromptType) -> Prompts -> Identity Prompts
Lens' Prompts PromptType
currentPromptL ((PromptType -> Identity PromptType)
 -> HumState -> Identity HumState)
-> PromptType -> HumState -> HumState
forall s t a b. ASetter s t a b -> b -> s -> t
.~ PromptType
TextPrompt
                                HumState -> (HumState -> HumState) -> HumState
forall a b. a -> (a -> b) -> b
& (Prompts -> Identity Prompts) -> HumState -> Identity HumState
Lens' HumState Prompts
promptsL ((Prompts -> Identity Prompts) -> HumState -> Identity HumState)
-> (((Bool -> HumState -> EventM Name HumState)
     -> Identity (Bool -> HumState -> EventM Name HumState))
    -> Prompts -> Identity Prompts)
-> ((Bool -> HumState -> EventM Name HumState)
    -> Identity (Bool -> HumState -> EventM Name HumState))
-> HumState
-> Identity HumState
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Bool -> HumState -> EventM Name HumState)
 -> Identity (Bool -> HumState -> EventM Name HumState))
-> Prompts -> Identity Prompts
Lens' Prompts (Bool -> HumState -> EventM Name HumState)
exitPromptFuncL (((Bool -> HumState -> EventM Name HumState)
  -> Identity (Bool -> HumState -> EventM Name HumState))
 -> HumState -> Identity HumState)
-> (Bool -> HumState -> EventM Name HumState)
-> HumState
-> HumState
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Vector Song -> Bool -> HumState -> EventM Name HumState
forall n. Vector Song -> Bool -> HumState -> EventM n HumState
songBulkAddtoNewPl Vector Song
songs
        Just PlaylistName
plname -> HumState -> EventM Name (Next HumState)
forall s n. s -> EventM n (Next s)
continue
          (HumState -> EventM Name (Next HumState))
-> EventM Name HumState -> EventM Name (Next HumState)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< String -> Vector Song -> HumState -> EventM Name HumState
forall n. String -> Vector Song -> HumState -> EventM n HumState
songBulkAddtoPl (PlaylistName -> String
forall a. ToString a => a -> String
MPD.toString PlaylistName
plname) Vector Song
songs (HumState
s HumState -> (HumState -> HumState) -> HumState
forall a b. a -> (a -> b) -> b
& (Mode -> Identity Mode) -> HumState -> Identity HumState
Lens' HumState Mode
modeL ((Mode -> Identity Mode) -> HumState -> Identity HumState)
-> Mode -> HumState -> HumState
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Mode
NormalMode)
    EvKey Key
KEnter [] -> do -- HACK find a way to unduplucate this
      let songs :: Vector Song
songs = HumState
s HumState -> Getting SongList HumState SongList -> SongList
forall s a. s -> Getting a s a -> a
^. Getting SongList HumState SongList
Lens' HumState SongList
queueL SongList -> (SongList -> SongList) -> SongList
forall a b. a -> (a -> b) -> b
& SongList -> SongList
forall (t :: * -> *) n e.
(Filterable t, Traversable t) =>
GenericList n t (e, Bool) -> GenericList n t (e, Bool)
getHighlighted SongList -> ((Song, Bool) -> Song) -> GenericList Name Vector Song
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> (Song, Bool) -> Song
forall a b. (a, b) -> a
fst GenericList Name Vector Song
-> (GenericList Name Vector Song -> Vector Song) -> Vector Song
forall a b. a -> (a -> b) -> b
& GenericList Name Vector Song -> Vector Song
forall n (t :: * -> *) e. GenericList n t e -> t e
listElements
      case HumState
s HumState
-> Getting
     (List Name (Maybe PlaylistName))
     HumState
     (List Name (Maybe PlaylistName))
-> List Name (Maybe PlaylistName)
forall s a. s -> Getting a s a -> a
^. (Prompts -> Const (List Name (Maybe PlaylistName)) Prompts)
-> HumState -> Const (List Name (Maybe PlaylistName)) HumState
Lens' HumState Prompts
promptsL ((Prompts -> Const (List Name (Maybe PlaylistName)) Prompts)
 -> HumState -> Const (List Name (Maybe PlaylistName)) HumState)
-> ((List Name (Maybe PlaylistName)
     -> Const
          (List Name (Maybe PlaylistName)) (List Name (Maybe PlaylistName)))
    -> Prompts -> Const (List Name (Maybe PlaylistName)) Prompts)
-> Getting
     (List Name (Maybe PlaylistName))
     HumState
     (List Name (Maybe PlaylistName))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (List Name (Maybe PlaylistName)
 -> Const
      (List Name (Maybe PlaylistName)) (List Name (Maybe PlaylistName)))
-> Prompts -> Const (List Name (Maybe PlaylistName)) Prompts
Lens' Prompts (List Name (Maybe PlaylistName))
plSelectPromptL List Name (Maybe PlaylistName)
-> (List Name (Maybe PlaylistName)
    -> Maybe (Int, Maybe PlaylistName))
-> Maybe (Int, Maybe PlaylistName)
forall a b. a -> (a -> b) -> b
& List Name (Maybe PlaylistName) -> Maybe (Int, Maybe PlaylistName)
forall (t :: * -> *) n e.
(Splittable t, Foldable t) =>
GenericList n t e -> Maybe (Int, e)
listSelectedElement Maybe (Int, Maybe PlaylistName)
-> ((Int, Maybe PlaylistName) -> Maybe PlaylistName)
-> Maybe (Maybe PlaylistName)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> (Int, Maybe PlaylistName) -> Maybe PlaylistName
forall a b. (a, b) -> b
snd Maybe (Maybe PlaylistName)
-> (Maybe (Maybe PlaylistName) -> Maybe PlaylistName)
-> Maybe PlaylistName
forall a b. a -> (a -> b) -> b
& Maybe (Maybe PlaylistName) -> Maybe PlaylistName
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join of
        Maybe PlaylistName
Nothing -> HumState -> EventM Name (Next HumState)
forall s n. s -> EventM n (Next s)
continue (HumState -> EventM Name (Next HumState))
-> HumState -> EventM Name (Next HumState)
forall a b. (a -> b) -> a -> b
$ HumState
s HumState -> (HumState -> HumState) -> HumState
forall a b. a -> (a -> b) -> b
& (Prompts -> Identity Prompts) -> HumState -> Identity HumState
Lens' HumState Prompts
promptsL ((Prompts -> Identity Prompts) -> HumState -> Identity HumState)
-> ((PromptType -> Identity PromptType)
    -> Prompts -> Identity Prompts)
-> (PromptType -> Identity PromptType)
-> HumState
-> Identity HumState
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (PromptType -> Identity PromptType) -> Prompts -> Identity Prompts
Lens' Prompts PromptType
currentPromptL ((PromptType -> Identity PromptType)
 -> HumState -> Identity HumState)
-> PromptType -> HumState -> HumState
forall s t a b. ASetter s t a b -> b -> s -> t
.~ PromptType
TextPrompt
                                HumState -> (HumState -> HumState) -> HumState
forall a b. a -> (a -> b) -> b
& (Prompts -> Identity Prompts) -> HumState -> Identity HumState
Lens' HumState Prompts
promptsL ((Prompts -> Identity Prompts) -> HumState -> Identity HumState)
-> (((Bool -> HumState -> EventM Name HumState)
     -> Identity (Bool -> HumState -> EventM Name HumState))
    -> Prompts -> Identity Prompts)
-> ((Bool -> HumState -> EventM Name HumState)
    -> Identity (Bool -> HumState -> EventM Name HumState))
-> HumState
-> Identity HumState
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Bool -> HumState -> EventM Name HumState)
 -> Identity (Bool -> HumState -> EventM Name HumState))
-> Prompts -> Identity Prompts
Lens' Prompts (Bool -> HumState -> EventM Name HumState)
exitPromptFuncL (((Bool -> HumState -> EventM Name HumState)
  -> Identity (Bool -> HumState -> EventM Name HumState))
 -> HumState -> Identity HumState)
-> (Bool -> HumState -> EventM Name HumState)
-> HumState
-> HumState
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Vector Song -> Bool -> HumState -> EventM Name HumState
forall n. Vector Song -> Bool -> HumState -> EventM n HumState
songBulkAddtoNewPl Vector Song
songs
        Just PlaylistName
plname -> HumState -> EventM Name (Next HumState)
forall s n. s -> EventM n (Next s)
continue
          (HumState -> EventM Name (Next HumState))
-> EventM Name HumState -> EventM Name (Next HumState)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< String -> Vector Song -> HumState -> EventM Name HumState
forall n. String -> Vector Song -> HumState -> EventM n HumState
songBulkAddtoPl (PlaylistName -> String
forall a. ToString a => a -> String
MPD.toString PlaylistName
plname) Vector Song
songs (HumState
s HumState -> (HumState -> HumState) -> HumState
forall a b. a -> (a -> b) -> b
& (Mode -> Identity Mode) -> HumState -> Identity HumState
Lens' HumState Mode
modeL ((Mode -> Identity Mode) -> HumState -> Identity HumState)
-> Mode -> HumState -> HumState
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Mode
NormalMode)
    Event
_ -> HumState -> EventM Name (Next HumState)
forall s n. s -> EventM n (Next s)
continue HumState
s
  BrickEvent Name HumEvent
_ -> HumState -> EventM Name (Next HumState)
forall s n. s -> EventM n (Next s)
continue HumState
s

-- | Add given songs to new playlist entered in prompt.
songBulkAddtoNewPl
  :: V.Vector MPD.Song -> Bool -> HumState -> EventM n HumState
songBulkAddtoNewPl :: Vector Song -> Bool -> HumState -> EventM n HumState
songBulkAddtoNewPl Vector Song
songs Bool
bl HumState
st = if Bool
bl
  then String -> Vector Song -> HumState -> EventM n HumState
forall n. String -> Vector Song -> HumState -> EventM n HumState
songBulkAddtoPl
    (Text -> String
forall a. ToString a => a -> String
toString (Text -> String) -> Text -> String
forall a b. (a -> b) -> a -> b
$ HumState
st HumState
-> Getting (TextZipper Text) HumState (TextZipper Text)
-> TextZipper Text
forall s a. s -> Getting a s a -> a
^. (Prompts -> Const (TextZipper Text) Prompts)
-> HumState -> Const (TextZipper Text) HumState
Lens' HumState Prompts
promptsL ((Prompts -> Const (TextZipper Text) Prompts)
 -> HumState -> Const (TextZipper Text) HumState)
-> ((TextZipper Text -> Const (TextZipper Text) (TextZipper Text))
    -> Prompts -> Const (TextZipper Text) Prompts)
-> Getting (TextZipper Text) HumState (TextZipper Text)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Editor Text Name -> Const (TextZipper Text) (Editor Text Name))
-> Prompts -> Const (TextZipper Text) Prompts
Lens' Prompts (Editor Text Name)
textPromptL ((Editor Text Name -> Const (TextZipper Text) (Editor Text Name))
 -> Prompts -> Const (TextZipper Text) Prompts)
-> ((TextZipper Text -> Const (TextZipper Text) (TextZipper Text))
    -> Editor Text Name -> Const (TextZipper Text) (Editor Text Name))
-> (TextZipper Text -> Const (TextZipper Text) (TextZipper Text))
-> Prompts
-> Const (TextZipper Text) Prompts
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (TextZipper Text -> Const (TextZipper Text) (TextZipper Text))
-> Editor Text Name -> Const (TextZipper Text) (Editor Text Name)
forall t1 n t2.
Lens (Editor t1 n) (Editor t2 n) (TextZipper t1) (TextZipper t2)
editContentsL TextZipper Text -> (TextZipper Text -> Text) -> Text
forall a b. a -> (a -> b) -> b
& TextZipper Text -> Text
forall a. Monoid a => TextZipper a -> a
Z.currentLine)
    Vector Song
songs
    HumState
st
  else HumState -> EventM n HumState
forall (f :: * -> *) a. Applicative f => a -> f a
pure HumState
st

-- | Handles key events for generic text prompt.
handleTextPromptEvent
  :: HumState -> BrickEvent Name HumEvent -> EventM Name (Next HumState)
handleTextPromptEvent :: HumState -> BrickEvent Name HumEvent -> EventM Name (Next HumState)
handleTextPromptEvent HumState
s BrickEvent Name HumEvent
e = case BrickEvent Name HumEvent
e of
  VtyEvent (EvKey Key
KEsc []) -> HumState -> EventM Name (Next HumState)
forall s n. s -> EventM n (Next s)
continue
    (HumState -> EventM Name (Next HumState))
-> EventM Name HumState -> EventM Name (Next HumState)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< (HumState
s HumState
-> Getting
     (Bool -> HumState -> EventM Name HumState)
     HumState
     (Bool -> HumState -> EventM Name HumState)
-> Bool
-> HumState
-> EventM Name HumState
forall s a. s -> Getting a s a -> a
^. (Prompts
 -> Const (Bool -> HumState -> EventM Name HumState) Prompts)
-> HumState
-> Const (Bool -> HumState -> EventM Name HumState) HumState
Lens' HumState Prompts
promptsL ((Prompts
  -> Const (Bool -> HumState -> EventM Name HumState) Prompts)
 -> HumState
 -> Const (Bool -> HumState -> EventM Name HumState) HumState)
-> (((Bool -> HumState -> EventM Name HumState)
     -> Const
          (Bool -> HumState -> EventM Name HumState)
          (Bool -> HumState -> EventM Name HumState))
    -> Prompts
    -> Const (Bool -> HumState -> EventM Name HumState) Prompts)
-> Getting
     (Bool -> HumState -> EventM Name HumState)
     HumState
     (Bool -> HumState -> EventM Name HumState)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Bool -> HumState -> EventM Name HumState)
 -> Const
      (Bool -> HumState -> EventM Name HumState)
      (Bool -> HumState -> EventM Name HumState))
-> Prompts
-> Const (Bool -> HumState -> EventM Name HumState) Prompts
Lens' Prompts (Bool -> HumState -> EventM Name HumState)
exitPromptFuncL) Bool
False (HumState
s HumState -> (HumState -> HumState) -> HumState
forall a b. a -> (a -> b) -> b
& (Mode -> Identity Mode) -> HumState -> Identity HumState
Lens' HumState Mode
modeL ((Mode -> Identity Mode) -> HumState -> Identity HumState)
-> Mode -> HumState -> HumState
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Mode
NormalMode)
  VtyEvent (EvKey Key
KEnter []) -> HumState -> EventM Name (Next HumState)
forall s n. s -> EventM n (Next s)
continue
    (HumState -> EventM Name (Next HumState))
-> EventM Name HumState -> EventM Name (Next HumState)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< (HumState
s HumState
-> Getting
     (Bool -> HumState -> EventM Name HumState)
     HumState
     (Bool -> HumState -> EventM Name HumState)
-> Bool
-> HumState
-> EventM Name HumState
forall s a. s -> Getting a s a -> a
^. (Prompts
 -> Const (Bool -> HumState -> EventM Name HumState) Prompts)
-> HumState
-> Const (Bool -> HumState -> EventM Name HumState) HumState
Lens' HumState Prompts
promptsL ((Prompts
  -> Const (Bool -> HumState -> EventM Name HumState) Prompts)
 -> HumState
 -> Const (Bool -> HumState -> EventM Name HumState) HumState)
-> (((Bool -> HumState -> EventM Name HumState)
     -> Const
          (Bool -> HumState -> EventM Name HumState)
          (Bool -> HumState -> EventM Name HumState))
    -> Prompts
    -> Const (Bool -> HumState -> EventM Name HumState) Prompts)
-> Getting
     (Bool -> HumState -> EventM Name HumState)
     HumState
     (Bool -> HumState -> EventM Name HumState)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Bool -> HumState -> EventM Name HumState)
 -> Const
      (Bool -> HumState -> EventM Name HumState)
      (Bool -> HumState -> EventM Name HumState))
-> Prompts
-> Const (Bool -> HumState -> EventM Name HumState) Prompts
Lens' Prompts (Bool -> HumState -> EventM Name HumState)
exitPromptFuncL) Bool
True (HumState
s HumState -> (HumState -> HumState) -> HumState
forall a b. a -> (a -> b) -> b
& (Mode -> Identity Mode) -> HumState -> Identity HumState
Lens' HumState Mode
modeL ((Mode -> Identity Mode) -> HumState -> Identity HumState)
-> Mode -> HumState -> HumState
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Mode
NormalMode)
  VtyEvent Event
vtye ->
    HumState -> EventM Name (Next HumState)
forall s n. s -> EventM n (Next s)
continue
      (HumState -> EventM Name (Next HumState))
-> EventM Name HumState -> EventM Name (Next HumState)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< HumState
-> Lens' HumState (Editor Text Name)
-> (Event -> Editor Text Name -> EventM Name (Editor Text Name))
-> Event
-> EventM Name HumState
forall a b e n.
a -> Lens' a b -> (e -> b -> EventM n b) -> e -> EventM n a
handleEventLensed HumState
s ((Prompts -> f Prompts) -> HumState -> f HumState
Lens' HumState Prompts
promptsL ((Prompts -> f Prompts) -> HumState -> f HumState)
-> ((Editor Text Name -> f (Editor Text Name))
    -> Prompts -> f Prompts)
-> (Editor Text Name -> f (Editor Text Name))
-> HumState
-> f HumState
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Editor Text Name -> f (Editor Text Name)) -> Prompts -> f Prompts
Lens' Prompts (Editor Text Name)
textPromptL) Event -> Editor Text Name -> EventM Name (Editor Text Name)
forall t n.
(DecodeUtf8 t, Eq t, Monoid t) =>
Event -> Editor t n -> EventM n (Editor t n)
handleEditorEvent Event
vtye
  BrickEvent Name HumEvent
_ -> HumState -> EventM Name (Next HumState)
forall s n. s -> EventM n (Next s)
continue HumState
s

-- | Handles key events for generic yes/no prompt.
handleYNPromptEvent
  :: HumState -> BrickEvent Name HumEvent -> EventM Name (Next HumState)
handleYNPromptEvent :: HumState -> BrickEvent Name HumEvent -> EventM Name (Next HumState)
handleYNPromptEvent HumState
s BrickEvent Name HumEvent
e = case BrickEvent Name HumEvent
e of
  VtyEvent (EvKey Key
KEsc []) ->
    HumState -> EventM Name (Next HumState)
forall s n. s -> EventM n (Next s)
continue
      (HumState -> EventM Name (Next HumState))
-> EventM Name HumState -> EventM Name (Next HumState)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< (HumState
s HumState
-> Getting
     (Bool -> HumState -> EventM Name HumState)
     HumState
     (Bool -> HumState -> EventM Name HumState)
-> Bool
-> HumState
-> EventM Name HumState
forall s a. s -> Getting a s a -> a
^. (Prompts
 -> Const (Bool -> HumState -> EventM Name HumState) Prompts)
-> HumState
-> Const (Bool -> HumState -> EventM Name HumState) HumState
Lens' HumState Prompts
promptsL ((Prompts
  -> Const (Bool -> HumState -> EventM Name HumState) Prompts)
 -> HumState
 -> Const (Bool -> HumState -> EventM Name HumState) HumState)
-> (((Bool -> HumState -> EventM Name HumState)
     -> Const
          (Bool -> HumState -> EventM Name HumState)
          (Bool -> HumState -> EventM Name HumState))
    -> Prompts
    -> Const (Bool -> HumState -> EventM Name HumState) Prompts)
-> Getting
     (Bool -> HumState -> EventM Name HumState)
     HumState
     (Bool -> HumState -> EventM Name HumState)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Bool -> HumState -> EventM Name HumState)
 -> Const
      (Bool -> HumState -> EventM Name HumState)
      (Bool -> HumState -> EventM Name HumState))
-> Prompts
-> Const (Bool -> HumState -> EventM Name HumState) Prompts
Lens' Prompts (Bool -> HumState -> EventM Name HumState)
exitPromptFuncL) Bool
False (HumState
s HumState -> (HumState -> HumState) -> HumState
forall a b. a -> (a -> b) -> b
& (Mode -> Identity Mode) -> HumState -> Identity HumState
Lens' HumState Mode
modeL ((Mode -> Identity Mode) -> HumState -> Identity HumState)
-> Mode -> HumState -> HumState
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Mode
NormalMode)
  VtyEvent (EvKey (KChar Char
'y') []) ->
    HumState -> EventM Name (Next HumState)
forall s n. s -> EventM n (Next s)
continue
      (HumState -> EventM Name (Next HumState))
-> EventM Name HumState -> EventM Name (Next HumState)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< (HumState
s HumState
-> Getting
     (Bool -> HumState -> EventM Name HumState)
     HumState
     (Bool -> HumState -> EventM Name HumState)
-> Bool
-> HumState
-> EventM Name HumState
forall s a. s -> Getting a s a -> a
^. (Prompts
 -> Const (Bool -> HumState -> EventM Name HumState) Prompts)
-> HumState
-> Const (Bool -> HumState -> EventM Name HumState) HumState
Lens' HumState Prompts
promptsL ((Prompts
  -> Const (Bool -> HumState -> EventM Name HumState) Prompts)
 -> HumState
 -> Const (Bool -> HumState -> EventM Name HumState) HumState)
-> (((Bool -> HumState -> EventM Name HumState)
     -> Const
          (Bool -> HumState -> EventM Name HumState)
          (Bool -> HumState -> EventM Name HumState))
    -> Prompts
    -> Const (Bool -> HumState -> EventM Name HumState) Prompts)
-> Getting
     (Bool -> HumState -> EventM Name HumState)
     HumState
     (Bool -> HumState -> EventM Name HumState)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Bool -> HumState -> EventM Name HumState)
 -> Const
      (Bool -> HumState -> EventM Name HumState)
      (Bool -> HumState -> EventM Name HumState))
-> Prompts
-> Const (Bool -> HumState -> EventM Name HumState) Prompts
Lens' Prompts (Bool -> HumState -> EventM Name HumState)
exitPromptFuncL) Bool
True (HumState
s HumState -> (HumState -> HumState) -> HumState
forall a b. a -> (a -> b) -> b
& (Mode -> Identity Mode) -> HumState -> Identity HumState
Lens' HumState Mode
modeL ((Mode -> Identity Mode) -> HumState -> Identity HumState)
-> Mode -> HumState -> HumState
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Mode
NormalMode)
  VtyEvent (EvKey (KChar Char
'n') []) ->
    HumState -> EventM Name (Next HumState)
forall s n. s -> EventM n (Next s)
continue
      (HumState -> EventM Name (Next HumState))
-> EventM Name HumState -> EventM Name (Next HumState)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< (HumState
s HumState
-> Getting
     (Bool -> HumState -> EventM Name HumState)
     HumState
     (Bool -> HumState -> EventM Name HumState)
-> Bool
-> HumState
-> EventM Name HumState
forall s a. s -> Getting a s a -> a
^. (Prompts
 -> Const (Bool -> HumState -> EventM Name HumState) Prompts)
-> HumState
-> Const (Bool -> HumState -> EventM Name HumState) HumState
Lens' HumState Prompts
promptsL ((Prompts
  -> Const (Bool -> HumState -> EventM Name HumState) Prompts)
 -> HumState
 -> Const (Bool -> HumState -> EventM Name HumState) HumState)
-> (((Bool -> HumState -> EventM Name HumState)
     -> Const
          (Bool -> HumState -> EventM Name HumState)
          (Bool -> HumState -> EventM Name HumState))
    -> Prompts
    -> Const (Bool -> HumState -> EventM Name HumState) Prompts)
-> Getting
     (Bool -> HumState -> EventM Name HumState)
     HumState
     (Bool -> HumState -> EventM Name HumState)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Bool -> HumState -> EventM Name HumState)
 -> Const
      (Bool -> HumState -> EventM Name HumState)
      (Bool -> HumState -> EventM Name HumState))
-> Prompts
-> Const (Bool -> HumState -> EventM Name HumState) Prompts
Lens' Prompts (Bool -> HumState -> EventM Name HumState)
exitPromptFuncL) Bool
False (HumState
s HumState -> (HumState -> HumState) -> HumState
forall a b. a -> (a -> b) -> b
& (Mode -> Identity Mode) -> HumState -> Identity HumState
Lens' HumState Mode
modeL ((Mode -> Identity Mode) -> HumState -> Identity HumState)
-> Mode -> HumState -> HumState
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Mode
NormalMode)
  VtyEvent (EvKey (KChar Char
'q') []) ->
    HumState -> EventM Name (Next HumState)
forall s n. s -> EventM n (Next s)
continue
      (HumState -> EventM Name (Next HumState))
-> EventM Name HumState -> EventM Name (Next HumState)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< (HumState
s HumState
-> Getting
     (Bool -> HumState -> EventM Name HumState)
     HumState
     (Bool -> HumState -> EventM Name HumState)
-> Bool
-> HumState
-> EventM Name HumState
forall s a. s -> Getting a s a -> a
^. (Prompts
 -> Const (Bool -> HumState -> EventM Name HumState) Prompts)
-> HumState
-> Const (Bool -> HumState -> EventM Name HumState) HumState
Lens' HumState Prompts
promptsL ((Prompts
  -> Const (Bool -> HumState -> EventM Name HumState) Prompts)
 -> HumState
 -> Const (Bool -> HumState -> EventM Name HumState) HumState)
-> (((Bool -> HumState -> EventM Name HumState)
     -> Const
          (Bool -> HumState -> EventM Name HumState)
          (Bool -> HumState -> EventM Name HumState))
    -> Prompts
    -> Const (Bool -> HumState -> EventM Name HumState) Prompts)
-> Getting
     (Bool -> HumState -> EventM Name HumState)
     HumState
     (Bool -> HumState -> EventM Name HumState)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Bool -> HumState -> EventM Name HumState)
 -> Const
      (Bool -> HumState -> EventM Name HumState)
      (Bool -> HumState -> EventM Name HumState))
-> Prompts
-> Const (Bool -> HumState -> EventM Name HumState) Prompts
Lens' Prompts (Bool -> HumState -> EventM Name HumState)
exitPromptFuncL) Bool
False (HumState
s HumState -> (HumState -> HumState) -> HumState
forall a b. a -> (a -> b) -> b
& (Mode -> Identity Mode) -> HumState -> Identity HumState
Lens' HumState Mode
modeL ((Mode -> Identity Mode) -> HumState -> Identity HumState)
-> Mode -> HumState -> HumState
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Mode
NormalMode)
  BrickEvent Name HumEvent
_                               -> HumState -> EventM Name (Next HumState)
forall s n. s -> EventM n (Next s)
continue HumState
s