repl: Support loading .hnixrc from current directory
This commit is contained in:
parent
f7704b4c28
commit
b84f4f6c19
|
@ -12,3 +12,4 @@ ctags
|
||||||
dist-newstyle
|
dist-newstyle
|
||||||
result*
|
result*
|
||||||
.ghc.environment.*
|
.ghc.environment.*
|
||||||
|
.hnixrc
|
||||||
|
|
34
main/Repl.hs
34
main/Repl.hs
|
@ -62,6 +62,7 @@ import System.Console.Repline ( Cmd
|
||||||
)
|
)
|
||||||
import qualified System.Console.Repline
|
import qualified System.Console.Repline
|
||||||
import qualified System.Exit
|
import qualified System.Exit
|
||||||
|
import qualified System.IO.Error
|
||||||
|
|
||||||
-- | Repl entry point
|
-- | Repl entry point
|
||||||
main :: (MonadNix e t f m, MonadIO m, MonadMask m) => m ()
|
main :: (MonadNix e t f m, MonadIO m, MonadMask m) => m ()
|
||||||
|
@ -76,15 +77,18 @@ main' iniVal = flip evalStateT (initState iniVal)
|
||||||
banner
|
banner
|
||||||
cmd
|
cmd
|
||||||
options
|
options
|
||||||
(Just ':')
|
(Just commandPrefix)
|
||||||
(Just "paste")
|
(Just "paste")
|
||||||
completer
|
completer
|
||||||
greeter
|
(rcFile >> greeter)
|
||||||
finalizer
|
finalizer
|
||||||
where
|
where
|
||||||
|
commandPrefix = ':'
|
||||||
|
|
||||||
banner = pure . \case
|
banner = pure . \case
|
||||||
System.Console.Repline.SingleLine -> "hnix> "
|
System.Console.Repline.SingleLine -> "hnix> "
|
||||||
System.Console.Repline.MultiLine -> "| "
|
System.Console.Repline.MultiLine -> "| "
|
||||||
|
|
||||||
greeter =
|
greeter =
|
||||||
liftIO
|
liftIO
|
||||||
$ putStrLn
|
$ putStrLn
|
||||||
|
@ -95,6 +99,32 @@ main' iniVal = flip evalStateT (initState iniVal)
|
||||||
liftIO $ putStrLn "Goodbye."
|
liftIO $ putStrLn "Goodbye."
|
||||||
return Exit
|
return Exit
|
||||||
|
|
||||||
|
rcFile = do
|
||||||
|
f <- liftIO $ Data.Text.IO.readFile ".hnixrc" `catch` handleMissing
|
||||||
|
forM_ (map (words . Data.Text.unpack) $ Data.Text.lines f) $ \case
|
||||||
|
((prefix:command) : xs) | prefix == commandPrefix -> do
|
||||||
|
let arguments = unwords xs
|
||||||
|
optMatcher command options arguments
|
||||||
|
x -> cmd $ unwords x
|
||||||
|
|
||||||
|
handleMissing e
|
||||||
|
| System.IO.Error.isDoesNotExistError e = return ""
|
||||||
|
| otherwise = throwIO e
|
||||||
|
|
||||||
|
-- Replicated and slightly adjusted `optMatcher` from `System.Console.Repline`
|
||||||
|
-- which doesn't export it.
|
||||||
|
-- * @MonadIO m@ instead of @MonadHaskeline m@
|
||||||
|
-- * @putStrLn@ instead of @outputStrLn@
|
||||||
|
optMatcher :: MonadIO m
|
||||||
|
=> String
|
||||||
|
-> System.Console.Repline.Options m
|
||||||
|
-> String
|
||||||
|
-> m ()
|
||||||
|
optMatcher s [] _ = liftIO $ putStrLn $ "No such command :" ++ s
|
||||||
|
optMatcher s ((x, m) : xs) args
|
||||||
|
| s `Data.List.isPrefixOf` x = m args
|
||||||
|
| otherwise = optMatcher s xs args
|
||||||
|
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
-- Types
|
-- Types
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
|
|
Loading…
Reference in New Issue