aboutsummaryrefslogtreecommitdiff
path: root/lib/Daffm/Configuration.hs
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2025-10-05 22:21:42 +0530
committerAkshay Nair <phenax5@gmail.com>2025-10-08 13:07:40 +0530
commit424d65e53b99c65f07f36578e3d0975c4d093c0e (patch)
tree8e6f9939a37632d51888010bad56011be191057c /lib/Daffm/Configuration.hs
parentabcbb255e8dd5f624ba5173783cf665522b527df (diff)
downloaddaffm-424d65e53b99c65f07f36578e3d0975c4d093c0e.tar.gz
daffm-424d65e53b99c65f07f36578e3d0975c4d093c0e.zip
Add extend config property to extend configs + move defaults
Diffstat (limited to '')
-rw-r--r--lib/Daffm/Configuration.hs22
1 files changed, 18 insertions, 4 deletions
diff --git a/lib/Daffm/Configuration.hs b/lib/Daffm/Configuration.hs
index d3691cd..9536007 100644
--- a/lib/Daffm/Configuration.hs
+++ b/lib/Daffm/Configuration.hs
@@ -1,3 +1,6 @@
+{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
+
+{-# HLINT ignore "Use <=<" #-}
module Daffm.Configuration where
import Control.Applicative ((<|>))
@@ -35,12 +38,21 @@ resolveConfigPath (Just path) = pure path
loadConfigFile :: Maybe String -> IO Configuration
loadConfigFile pathM = do
- resolveConfigPath pathM >>= (IO.try . Text.readFile) >>= foobar
+ cfgPath <- resolveConfigPath pathM
+ config <- load cfgPath
+ case configExtend config of
+ Just path -> do
+ baseCfgPath <- resolveConfigPath $ Just $ Text.unpack path
+ if baseCfgPath == cfgPath
+ then pure config
+ else (config <>) <$> load baseCfgPath
+ _ -> pure config
where
- foobar :: Either IOError Text.Text -> IO Configuration
- foobar rawE = case rawE of
+ load = (>>= parseWithDefault) . IO.try . Text.readFile
+ parseWithDefault :: Either IOError Text.Text -> IO Configuration
+ parseWithDefault rawE = case rawE of
Left _ -> pure defaultConfiguration
- Right txt -> parse txt
+ Right txt -> (<> defaultConfiguration) <$> parse txt
parse txt = case parseConfig txt of
Left e -> throwIO $ userError $ show e
Right c -> pure c
@@ -53,9 +65,11 @@ configurationCodec =
Configuration
<$> (keymapCodec "keymap" .= configKeymap)
<*> (openerCodec "opener" .= configOpener)
+ <*> (extendCodec "extend" .= configExtend)
<*> pure Map.empty .= configTheme
where
openerCodec = Toml.dioptional . Toml.text
+ extendCodec = Toml.dioptional . Toml.text
keymapCodec :: Toml.Key -> Toml.TomlCodec Keymap
keymapCodec = Toml.dimap (const Map.empty) toKeymap . keymapRawCodec