Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added XDG support #157

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"))