{-# Language ExistentialQuantification #-}
module Client.Commands.Types where
import Client.Commands.Arguments.Spec (Args)
import Client.State (ClientState, clientErrorMsg, clientConnection, clientFocus)
import Client.State.Focus (Focus)
import Client.State.Network (NetworkState, csNetwork)
import Control.Lens (set, view)
import Data.List.NonEmpty (NonEmpty)
import Data.Text (Text)
import Irc.Identifier (Identifier)
import LensUtils (setStrict)
data CommandResult
= CommandSuccess ClientState
| CommandFailure ClientState
| CommandQuit ClientState
type ClientCommand a = ClientState -> a -> IO CommandResult
type WindowCommand a = Focus -> ClientCommand a
type NetworkCommand a = NetworkState -> ClientCommand a
type MaybeChatCommand a = Maybe Identifier -> NetworkCommand a
type ChannelCommand a = Identifier -> NetworkCommand a
data CommandImpl a
= ClientCommand (ClientCommand a) (Bool -> ClientCommand String)
| WindowCommand (WindowCommand a) (Bool -> WindowCommand String)
| NetworkCommand (NetworkCommand a) (Bool -> NetworkCommand String)
| MaybeChatCommand (MaybeChatCommand a) (Bool -> MaybeChatCommand String)
| ChatCommand (ChannelCommand a) (Bool -> ChannelCommand String)
| ChannelCommand (ChannelCommand a) (Bool -> ChannelCommand String)
data ArgsContext = ArgsContext
{ ArgsContext -> ClientState
argsContextSt :: ClientState
, ArgsContext -> Focus
argsContextFocus :: Focus
}
makeArgsContext :: ClientState -> ArgsContext
makeArgsContext :: ClientState -> ArgsContext
makeArgsContext ClientState
st = ArgsContext {argsContextSt :: ClientState
argsContextSt=ClientState
st, argsContextFocus :: Focus
argsContextFocus=Getting Focus ClientState Focus -> ClientState -> Focus
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting Focus ClientState Focus
Lens' ClientState Focus
clientFocus ClientState
st}
data Command = forall a. Command
{
Command -> NonEmpty Text
cmdNames :: NonEmpty Text
, ()
cmdArgumentSpec :: Args ArgsContext a
, Command -> Text
cmdDocumentation :: Text
, ()
cmdImplementation :: CommandImpl a
}
data CommandSection = CommandSection
{ CommandSection -> Text
cmdSectionName :: Text
, CommandSection -> [Command]
cmdSectionCmds :: [Command]
}
commandSuccess :: Monad m => ClientState -> m CommandResult
commandSuccess :: forall (m :: * -> *). Monad m => ClientState -> m CommandResult
commandSuccess = CommandResult -> m CommandResult
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (CommandResult -> m CommandResult)
-> (ClientState -> CommandResult) -> ClientState -> m CommandResult
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ClientState -> CommandResult
CommandSuccess
commandSuccessUpdateCS :: NetworkState -> ClientState -> IO CommandResult
commandSuccessUpdateCS :: NetworkState -> ClientState -> IO CommandResult
commandSuccessUpdateCS NetworkState
cs ClientState
st =
do let network :: Text
network = Getting Text NetworkState Text -> NetworkState -> Text
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting Text NetworkState Text
Lens' NetworkState Text
csNetwork NetworkState
cs
ClientState -> IO CommandResult
forall (m :: * -> *). Monad m => ClientState -> m CommandResult
commandSuccess
(ClientState -> IO CommandResult)
-> ClientState -> IO CommandResult
forall a b. (a -> b) -> a -> b
$ ASetter ClientState ClientState NetworkState NetworkState
-> NetworkState -> ClientState -> ClientState
forall s t a b. ASetter s t a b -> b -> s -> t
setStrict (Text -> ASetter ClientState ClientState NetworkState NetworkState
forall (f :: * -> *).
Applicative f =>
Text -> LensLike' f ClientState NetworkState
clientConnection Text
network) NetworkState
cs ClientState
st
commandFailure :: Monad m => ClientState -> m CommandResult
commandFailure :: forall (m :: * -> *). Monad m => ClientState -> m CommandResult
commandFailure = CommandResult -> m CommandResult
forall a. a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return (CommandResult -> m CommandResult)
-> (ClientState -> CommandResult) -> ClientState -> m CommandResult
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ClientState -> CommandResult
CommandFailure
commandFailureMsg :: Text -> ClientState -> IO CommandResult
commandFailureMsg :: Text -> ClientState -> IO CommandResult
commandFailureMsg Text
e ClientState
st =
CommandResult -> IO CommandResult
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (CommandResult -> IO CommandResult)
-> CommandResult -> IO CommandResult
forall a b. (a -> b) -> a -> b
$! ClientState -> CommandResult
CommandFailure (ClientState -> CommandResult) -> ClientState -> CommandResult
forall a b. (a -> b) -> a -> b
$! ASetter ClientState ClientState (Maybe Text) (Maybe Text)
-> Maybe Text -> ClientState -> ClientState
forall s t a b. ASetter s t a b -> b -> s -> t
set ASetter ClientState ClientState (Maybe Text) (Maybe Text)
Lens' ClientState (Maybe Text)
clientErrorMsg (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
e) ClientState
st