From 2b427789d25344c67335fa9d37f0f99310774a37 Mon Sep 17 00:00:00 2001 From: Jonathan Lamothe Date: Sat, 7 Sep 2024 16:57:20 -0400 Subject: [PATCH] defined data structure for master password form --- src/Password/App.hs | 6 ++-- src/Password/App/Types.hs | 69 +++++++++++++++++++++++++++++++++++---- 2 files changed, 65 insertions(+), 10 deletions(-) diff --git a/src/Password/App.hs b/src/Password/App.hs index 07c76d6..a30cc4c 100644 --- a/src/Password/App.hs +++ b/src/Password/App.hs @@ -39,7 +39,7 @@ import Brick import Password.App.Types -- | The main application -passmanApp :: App AppState () () +passmanApp :: App AppState () ResName passmanApp = App { appDraw = drawFunc , appChooseCursor = neverShowCursor @@ -48,10 +48,10 @@ passmanApp = App , appAttrMap = const $ attrMap (style 0) [] } -drawFunc :: AppState -> [Widget ()] +drawFunc :: AppState -> [Widget ResName] drawFunc = const [emptyWidget] -eventHandler :: BrickEvent () () -> EventM () AppState () +eventHandler :: BrickEvent ResName () -> EventM ResName AppState () eventHandler = const halt --jl diff --git a/src/Password/App/Types.hs b/src/Password/App/Types.hs index e0a00b4..1087c0e 100644 --- a/src/Password/App/Types.hs +++ b/src/Password/App/Types.hs @@ -22,28 +22,83 @@ License along with this program. If not, see -} -{-# LANGUAGE TemplateHaskell #-} +{-# LANGUAGE OverloadedStrings, TemplateHaskell #-} module Password.App.Types ( + -- * Types AppState (..), + AppMode (..), + InitState (..), + ResName (..), + -- * Lenses + -- ** AppState randGen, - mkInitialState + appMode, + -- ** AppMode + initState, + -- ** InitState + setPassForm, + spfError, + -- * Constructors + mkInitialState, ) where +import Brick (txt, (<+>)) +import Brick.Forms (Form, editPasswordField, newForm, (@@=)) import Control.Monad.IO.Class (MonadIO) -import Lens.Micro.TH +import Data.Text (Text) +import Lens.Micro (_1, _2) +import Lens.Micro.TH (makeLenses) import System.Random (StdGen, initStdGen) -- | The application state -newtype AppState = AppState +data AppState = AppState { _randGen :: StdGen -- ^ The random number generator - } deriving (Eq, Show) + , _appMode :: AppMode + } -makeLenses ''AppState +-- | The applicaiton's mode +newtype AppMode = InitMode + { _initState :: InitState + -- ^ Initialization state + } + +-- | Application initialization state +data InitState = InitState + { _setPassForm :: Form (Text, Text) () ResName + -- ^ password form + , _spfError :: Text + -- ^ error message + } + +-- | Resource identifier +data ResName + = PassField + | ConfField + deriving (Eq, Ord, Show) + +concat <$> mapM makeLenses + [ ''AppState + , ''AppMode + , ''InitState + ] -- | Builds an initial state mkInitialState :: MonadIO m => m AppState -mkInitialState = AppState <$> initStdGen +mkInitialState = AppState + <$> initStdGen + <*> return (InitMode newInitState) + +-- | New `InitState` value +newInitState :: InitState +newInitState = InitState + ( newForm + [ (txt "Master password: " <+>) @@= editPasswordField _1 PassField + , (txt "Confirm password: " <+>) @@= editPasswordField _2 ConfField + ] + ("", "") + ) + "" --jl