Skip to content

Commit

Permalink
Merge pull request #157 from czaplicki/master
Browse files Browse the repository at this point in the history
Added XDG support
  • Loading branch information
athas authored Feb 6, 2025
2 parents 073eca6 + e7f515a commit c469e92
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions System/Console/Haskeline/InputT.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Control.Monad.Catch
import Control.Monad.Fail as Fail
import Control.Monad.Fix
import Data.IORef
import System.Directory(getHomeDirectory)
import System.Directory(getXdgDirectory, XdgDirectory(XdgConfig), doesFileExist, getHomeDirectory)
import System.FilePath
import System.IO

Expand Down Expand Up @@ -164,7 +164,7 @@ withBehavior (Behavior run) f = bracket (liftIO run) (liftIO . closeTerm) f
runInputTBehavior :: (MonadIO m, MonadMask m) => Behavior -> Settings m -> InputT m a -> m a
runInputTBehavior behavior settings f = withBehavior behavior $ \run -> do
prefs <- if isTerminalStyle run
then liftIO readPrefsFromHome
then liftIO readUserPrefs
else return defaultPrefs
execInputT prefs settings run f

Expand Down Expand Up @@ -217,10 +217,13 @@ preferTerm :: Behavior
preferTerm = Behavior terminalRunTerm


-- | Read 'Prefs' from @~/.haskeline.@ If there is an error reading the file,
-- | Read 'Prefs' from @$XDG_CONFIG_HOME/haskeline/haskeline@ if present
-- ortherwise @~/.haskeline.@ If there is an error reading the file,
-- the 'defaultPrefs' will be returned.
readPrefsFromHome :: IO Prefs
readPrefsFromHome = handle (\(_::IOException) -> return defaultPrefs) $ do
home <- getHomeDirectory
readPrefs (home </> ".haskeline")
readUserPrefs :: IO Prefs
readUserPrefs = handle (\(_::IOException) -> return defaultPrefs) $ do
xdg <- getXdgDirectory XdgConfig ("haskeline/haskeline")
exists <- doesFileExist xdg
home <- getHomeDirectory
readPrefs (if exists then xdg else (home </> ".haskeline"))

0 comments on commit c469e92

Please sign in to comment.