Skip to content

Commit

Permalink
Merge pull request #36 from MoritzR/read-template-from-config-directory
Browse files Browse the repository at this point in the history
Read template from config directory
  • Loading branch information
MoritzR authored Oct 19, 2023
2 parents 932556a + 536c447 commit 1197388
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
17 changes: 10 additions & 7 deletions src/Config/Files.hs
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE TemplateHaskell #-}

module Config.Files (getDefaultConfigDirectory, getConfigFilePath, templateFile, exampleFile, pyfintsFile, ConfigDirectory (..)) where
module Config.Files (getDefaultConfigDirectory, getConfigFilePath, getTemplateFile, exampleFile, pyfintsFile, ConfigDirectory (..)) where

import Data.ByteString (ByteString)
import Data.FileEmbed (embedDir)
import Data.Map (Map, fromList, (!))
import Data.String (IsString)
import Data.Text (Text)
import Data.Text.Encoding qualified as T
import System.Directory (getHomeDirectory)
import System.Directory (XdgDirectory (XdgConfig), doesFileExist, getXdgDirectory)
import System.FilePath ((</>))

getDefaultConfigDirectory :: IO ConfigDirectory
getDefaultConfigDirectory = do
homeDirectory <- getHomeDirectory
return $ ConfigDirectory $ homeDirectory </> ".config" </> "fints2ledger"
getDefaultConfigDirectory = ConfigDirectory <$> getXdgDirectory XdgConfig "fints2ledger"

getConfigFilePath :: ConfigDirectory -> FilePath
getConfigFilePath configDirectory = configDirectory.get </> "config.yml"

templateFile :: Text
templateFile = T.decodeUtf8 $ dataFiles ! "template.txt"
getTemplateFile :: ConfigDirectory -> (FilePath -> IO Text) -> IO Text
getTemplateFile configDirectory readTextFile = do
let templateFilePath = configDirectory.get </> "template.txt"
fileExists <- doesFileExist templateFilePath
if fileExists
then readTextFile templateFilePath
else return $ T.decodeUtf8 $ dataFiles ! "template.txt"

exampleFile :: Text
exampleFile = T.decodeUtf8 $ dataFiles ! "example.json"
Expand Down
8 changes: 5 additions & 3 deletions src/Prompt.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Prompt (transactionsToLedger) where

import App (App, Env (..), PromptResult (..), printText)
import Config.AppConfig (AppConfig (..))
import Config.Files (templateFile)
import Config.Files (getTemplateFile)
import Config.YamlConfig (Fill, Filling (..), LedgerConfig (..))
import Control.Arrow ((>>>))
import Control.Monad (forM_, when)
Expand Down Expand Up @@ -40,13 +40,14 @@ transactionsToLedger transactions = do
config <- asks (.config)
readFile <- asks (.readFile)
existingMd5Sums <- getExistingMd5Sums <$> liftIO (readFile config.journalFile)
template <- liftIO $ getTemplateFile config.configDirectory readFile

printText " Controls:"
printText " - Ctrl + D or enter 's' to skip an entry"
printText " - Ctrl + C to abort"

forM_ transactions do
transactionToLedger existingMd5Sums templateFile
transactionToLedger existingMd5Sums template

transactionToLedger :: Set Text -> Text -> Transaction -> App ()
transactionToLedger existingMd5Sums template transaction = do
Expand Down Expand Up @@ -152,7 +153,8 @@ md5Regex = "; md5sum: ([A-Za-z0-9]{32})"

getExistingMd5Sums :: Text -> Set Text
getExistingMd5Sums textToSearchIn =
textToSearchIn =~ md5Regex
textToSearchIn
=~ md5Regex
& getAllTextMatches @(Array Int)
& elems
& map (!! 1)
Expand Down

0 comments on commit 1197388

Please sign in to comment.