From 86278db578feebc1d1be22de77f70d183fd70aa0 Mon Sep 17 00:00:00 2001 From: Jonathan Lamothe Date: Tue, 10 Sep 2024 20:00:24 -0400 Subject: [PATCH] load database on start --- package.yaml | 1 + passman.cabal | 5 ++++- src/Password/App.hs | 2 +- src/Password/App/Event.hs | 28 ++++++++++++++++++++++++++-- 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/package.yaml b/package.yaml index 134020b..79044c4 100644 --- a/package.yaml +++ b/package.yaml @@ -32,6 +32,7 @@ dependencies: - brick >= 2.1.1 && < 2.2 - vty >= 6.1 && < 6.2 - mtl >= 2.3.1 && < 2.4 +- easy-file >= 0.2.5 && < 0.3 ghc-options: - -Wall diff --git a/passman.cabal b/passman.cabal index 7a40e37..4785b27 100644 --- a/passman.cabal +++ b/passman.cabal @@ -4,7 +4,7 @@ cabal-version: 2.2 -- -- see: https://github.com/sol/hpack -- --- hash: fb2b45c8e1d5aead518c67637b3af5e78b55b8f0c5a95a34c20ca5d957527fa0 +-- hash: d18a8e1efd32ff2d20b0b1f5ac8186be5242411bd72a6a017fd9f97d401a9836 name: passman version: 0.3.1.1 @@ -46,6 +46,7 @@ library , brick >=2.1.1 && <2.2 , bytestring >=0.11.4.0 && <0.12 , containers >=0.6.2.1 && <0.7 + , easy-file >=0.2.5 && <0.3 , microlens >=0.4.11.2 && <0.5 , microlens-mtl >=0.2.0.3 && <0.3 , microlens-th >=0.4.3.6 && <0.5 @@ -70,6 +71,7 @@ executable passman , brick >=2.1.1 && <2.2 , bytestring >=0.11.4.0 && <0.12 , containers >=0.6.2.1 && <0.7 + , easy-file >=0.2.5 && <0.3 , microlens >=0.4.11.2 && <0.5 , microlens-mtl >=0.2.0.3 && <0.3 , microlens-th >=0.4.3.6 && <0.5 @@ -110,6 +112,7 @@ test-suite passman-test , brick >=2.1.1 && <2.2 , bytestring >=0.11.4.0 && <0.12 , containers >=0.6.2.1 && <0.7 + , easy-file >=0.2.5 && <0.3 , microlens >=0.4.11.2 && <0.5 , microlens-mtl >=0.2.0.3 && <0.3 , microlens-th >=0.4.3.6 && <0.5 diff --git a/src/Password/App.hs b/src/Password/App.hs index 74f9c9e..30236be 100644 --- a/src/Password/App.hs +++ b/src/Password/App.hs @@ -41,7 +41,7 @@ passmanApp = App { appDraw = drawFunc , appChooseCursor = showFirstCursor , appHandleEvent = eventHandler - , appStartEvent = return () + , appStartEvent = loadDatabase , appAttrMap = const $ attrMap (style 0) [] } diff --git a/src/Password/App/Event.hs b/src/Password/App/Event.hs index a7eac47..65d2869 100644 --- a/src/Password/App/Event.hs +++ b/src/Password/App/Event.hs @@ -24,7 +24,7 @@ License along with this program. If not, see {-# LANGUAGE LambdaCase, OverloadedStrings #-} -module Password.App.Event (eventHandler) where +module Password.App.Event (eventHandler, loadDatabase) where import Brick (BrickEvent (VtyEvent), EventM, halt) import Brick.Forms (handleFormEvent) @@ -39,13 +39,23 @@ import Brick.Keybindings , onEvent ) import Control.Monad (unless) -import Control.Monad.State.Class (gets) +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.Mtl (zoom) +import System.EasyFile + ( createDirectoryIfMissing + , getAppUserDataDirectory + , () + ) import Password.App.Types +dbFile :: String +dbFile = "database.json" + data KEventID = QuitKE deriving (Eq, Ord, Show) -- | The main event handler @@ -55,6 +65,14 @@ eventHandler e@(VtyEvent (EvKey k m)) = do handleKey disp k m >>= flip unless (fallbackHandler e) eventHandler e = fallbackHandler e +loadDatabase :: EventM ResName AppState () +loadDatabase = zoom database $ liftIO + ( do + dir <- mkAppDir + let fn = dir dbFile + decodeFileStrict fn + ) >>= mapM_ put + fallbackHandler :: BrickEvent ResName () -> EventM ResName AppState () fallbackHandler e = gets (^.appMode) >>= \case InitMode _ -> zoom (appMode.initState.setPassForm) $ @@ -76,4 +94,10 @@ getKeyDispatcher s = either (error "can't build dispatcher") id $ keyBindingsFor :: AppState -> [(KEventID, [Binding])] keyBindingsFor = const [(QuitKE, [ctrl 'c'])] +mkAppDir :: IO FilePath +mkAppDir = do + path <- getAppUserDataDirectory "passman" + createDirectoryIfMissing True path + return path + --jl