Compare commits

...

3 Commits

3 changed files with 37 additions and 51 deletions

View File

@ -22,25 +22,33 @@ License along with this program. If not, see
-}
{-# LANGUAGE OverloadedStrings #-}
module Password.App.Draw (drawFunc) where
import Brick (Widget, txt, vBox)
import Brick.Forms (renderForm)
import Brick (Widget, emptyWidget, txt, vBox)
import Brick.Forms (Form, formState, renderForm)
import Data.Text (Text)
import Lens.Micro ((^.))
import Password.App.Types
-- | Renders the application view
drawFunc :: AppState -> [Widget ResName]
drawFunc s = case s^.appMode of
InitMode is -> drawPassForm is
drawFunc s = maybe [emptyWidget] drawPassForm $ s^.passForm
drawPassForm :: InitState -> [Widget ResName]
drawPassForm is =
drawPassForm :: Form (Text, Text) () ResName -> [Widget ResName]
drawPassForm f =
[ vBox
[ renderForm $ is^.setPassForm
, txt $ is^.spfError
[ renderForm f
, txt $ pfText $ formState f
]
]
pfText :: (Text, Text) -> Text
pfText (pass, conf)
| pass == "" = "Password cannot be blank."
| pass /= conf = "Passwords do not match."
| otherwise = ""
--jl

View File

@ -43,7 +43,7 @@ import Control.Monad.IO.Class (liftIO)
import Control.Monad.State.Class (gets, put)
import Data.Aeson (decodeFileStrict)
import Graphics.Vty.Input.Events (Event (EvKey))
import Lens.Micro ((^.))
import Lens.Micro (each, (^.))
import Lens.Micro.Mtl (zoom)
import System.EasyFile
( createDirectoryIfMissing
@ -77,9 +77,9 @@ loadDatabase = zoom database $ liftIO
) >>= mapM_ put
fallbackHandler :: BrickEvent ResName () -> EventM ResName AppState ()
fallbackHandler e = gets (^.appMode) >>= \case
InitMode _ -> zoom (appMode.initState.setPassForm) $
handleFormEvent e
fallbackHandler e = gets (^.passForm) >>= \case
Just _ -> zoom (passForm.each) $ handleFormEvent e
Nothing -> return ()
getKeyDispatcher
:: AppState

View File

@ -27,19 +27,13 @@ License along with this program. If not, see
module Password.App.Types (
-- * Types
AppState (..),
AppMode (..),
InitState (..),
ResName (..),
-- * Lenses
-- ** AppState
randGen,
database,
appMode,
-- ** AppMode
initState,
-- ** InitState
setPassForm,
spfError,
mainPass,
passForm,
-- * Constructors
mkInitialState,
) where
@ -60,23 +54,13 @@ data AppState = AppState
-- ^ The random number generator
, _database :: PWDatabase
-- ^ The password database
, _appMode :: AppMode
-- ^ The current operating mode
, _mainPass :: String
-- ^ The main password
, _passForm :: Maybe PassForm
}
-- | 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
}
-- | A password form (with confirmation)
type PassForm = Form (Text, Text) () ResName
-- | Resource identifier
data ResName
@ -84,28 +68,22 @@ data ResName
| ConfField
deriving (Eq, Ord, Show)
concat <$> mapM makeLenses
[ ''AppState
, ''AppMode
, ''InitState
]
makeLenses ''AppState
-- | Builds an initial state
mkInitialState :: MonadIO m => m AppState
mkInitialState = AppState
<$> initStdGen
<*> return newPWDatabase
<*> return (InitMode newInitState)
<*> return ""
<*> return (Just newPassForm)
-- | New `InitState` value
newInitState :: InitState
newInitState = InitState
( newForm
[ (txt "Master password: " <+>) @@= editPasswordField _1 PassField
, (txt "Confirm password: " <+>) @@= editPasswordField _2 ConfField
]
("", "")
)
""
-- | Constructs a blank password form
newPassForm :: PassForm
newPassForm = newForm
[ (txt "Master password: " <+>) @@= editPasswordField _1 PassField
, (txt "Confirm password: " <+>) @@= editPasswordField _2 ConfField
]
("", "")
--jl