start :: TMVar () -> StatusChannel -> IO ThreadId
start waitMutex statusChan = do
    (conf, state) <- init
    spawnProcess conf state (catchProcess loop stop)
  where
    loop = do
        cmdChan <- asks cCmdChan
        outChan <- asks cOutChan
        command <- liftIO . atomically $ readTChan cmdChan
        case command of
            Quit -> liftIO . atomically $ putTMVar waitMutex ()
            Help -> liftIO . atomically $ writeTChan outChan helpMessage
            Show -> do
                ret <- liftIO newEmptyTMVarIO
                liftIO . atomically $ writeTChan statusChan (RequestAllTorrents ret)
                status <- liftIO . atomically $ takeTMVar ret
                liftIO . atomically $ writeTChan outChan (show status)
            (Unknown cmd) -> liftIO . atomically $ writeTChan outChan $ "Неизвестная комманда: " ++ cmd
        loop


stop :: Process 
start :: TMVar () -> StatusChannel -> IO ThreadId
start waitMutex statusChan = do
    (conf, state) <- init
    spawnProcess conf state (catchProcess loop stop)
  where
    loop = do
        cmdChan <- asks cCmdChan
        outChan <- asks cOutChan
        command <- liftIO . atomically $ readTChan cmdChan
        case command of
            Quit -> liftIO . atomically $ putTMVar waitMutex ()
            Help -> liftIO . atomically $ writeTChan outChan helpMessage
            Show -> do
                ret <- liftIO newEmptyTMVarIO
                liftIO . atomically $ writeTChan statusChan (RequestAllTorrents ret)
                status <- liftIO . atomically $ takeTMVar ret
                liftIO . atomically $ writeTChan outChan (show status)
            (Unknown cmd) -> liftIO . atomically $ writeTChan outChan $ "Неизвестная комманда: " ++ cmd
        loop
start waitMutex statusChan = do
    (conf, state) <- init
    spawnProcess conf state (catchProcess loop stop)
  where
    loop = do
        cmdChan <- asks cCmdChan
        outChan <- asks cOutChan
        command <- liftIO . atomically $ readTChan cmdChan
        case command of
            Quit -> liftIO . atomically $ putTMVar waitMutex ()
            Help -> liftIO . atomically $ writeTChan outChan helpMessage
            Show -> do
                ret <- liftIO newEmptyTMVarIO
                liftIO . atomically $ writeTChan statusChan (RequestAllTorrents ret)
                status <- liftIO . atomically $ takeTMVar ret
                liftIO . atomically $ writeTChan outChan (show status)
            (Unknown cmd) -> liftIO . atomically $ writeTChan outChan $ "Неизвестная комманда: " ++ cmd
        loop