module Lambdabot.Plugin.Haskell.Source (sourcePlugin) where
import Lambdabot.Plugin
import Lambdabot.Util
import Control.Monad
import qualified Data.ByteString.Char8 as P
import qualified Data.Map as M
type Env = M.Map P.ByteString P.ByteString
sourcePlugin :: Module (M.Map P.ByteString P.ByteString)
sourcePlugin :: Module (Map ByteString ByteString)
sourcePlugin = forall st. Module st
newModule
{ moduleCmds :: ModuleT
(Map ByteString ByteString)
LB
[Command (ModuleT (Map ByteString ByteString) LB)]
moduleCmds = forall (m :: * -> *) a. Monad m => a -> m a
return
[ (String -> Command Identity
command String
"src")
{ help :: Cmd (ModuleT (Map ByteString ByteString) LB) ()
help = forall (m :: * -> *). Monad m => String -> Cmd m ()
say String
helpStr
, process :: String -> Cmd (ModuleT (Map ByteString ByteString) LB) ()
process = \String
key -> forall (m :: * -> *). MonadLBState m => m (LBState m)
readMS forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Map ByteString ByteString
env -> case ByteString -> Map ByteString ByteString -> Maybe ByteString
fetch (String -> ByteString
P.pack String
key) Map ByteString ByteString
env of
Maybe ByteString
_ | forall k a. Map k a -> Bool
M.null Map ByteString ByteString
env -> forall (m :: * -> *). Monad m => String -> Cmd m ()
say String
"No source in the environment yet"
Maybe ByteString
_ | forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
key -> forall (m :: * -> *). Monad m => String -> Cmd m ()
say String
helpStr
Maybe ByteString
Nothing -> forall (m :: * -> *). Monad m => String -> Cmd m ()
say forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
"Source not found. " forall a. [a] -> [a] -> [a]
++) forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall (m :: * -> *). (MonadIO m, MonadConfig m) => m String
randomFailureMsg
Just ByteString
s -> forall (m :: * -> *). Monad m => String -> Cmd m ()
say (ByteString -> String
P.unpack ByteString
s)
}
]
, moduleSerialize :: Maybe (Serial (Map ByteString ByteString))
moduleSerialize = forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall b. (ByteString -> b) -> Serial b
readOnly forall a b. (a -> b) -> a -> b
$ forall k a. Ord k => [(k, a)] -> Map k a
M.fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map [ByteString] -> (ByteString, ByteString)
pair forall b c a. (b -> c) -> (a -> b) -> a -> c
. [ByteString] -> [[ByteString]]
splat forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> [ByteString]
P.lines
}
where
pair :: [ByteString] -> (ByteString, ByteString)
pair (ByteString
a:[ByteString]
b) = (ByteString
a, [ByteString] -> ByteString
P.unlines [ByteString]
b)
pair [ByteString]
_ = forall a. HasCallStack => String -> a
error String
"Source Plugin error: not a pair"
splat :: [ByteString] -> [[ByteString]]
splat [] = []
splat [ByteString]
s = [ByteString]
a forall a. a -> [a] -> [a]
: [ByteString] -> [[ByteString]]
splat (forall a. [a] -> [a]
tail [ByteString]
b) where ([ByteString]
a,[ByteString]
b) = forall a. (a -> Bool) -> [a] -> ([a], [a])
break ByteString -> Bool
P.null [ByteString]
s
fetch :: P.ByteString -> Env -> Maybe P.ByteString
fetch :: ByteString -> Map ByteString ByteString -> Maybe ByteString
fetch ByteString
x Map ByteString ByteString
m = forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup ByteString
x Map ByteString ByteString
m forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
`mplus`
forall k a. Ord k => k -> Map k a -> Maybe a
M.lookup ([ByteString] -> ByteString
P.concat [Char -> ByteString
P.singleton Char
'(', ByteString
x, Char -> ByteString
P.singleton Char
')']) Map ByteString ByteString
m
helpStr :: String
helpStr :: String
helpStr = String
"src <id>. Display the implementation of a standard function"