Debian Codegraph @

src/Lambdabot/Plugin/Core/More.hs

raw
1 -- | Support for more(1) buffering
2 module Lambdabot.Plugin.Core.More (morePlugin) where
3
4 import Lambdabot.Bot
5 import Lambdabot.Monad
6 import Lambdabot.Plugin
7
8 import Control.Monad.Trans
9
10 type MoreState = GlobalPrivate () [String]
11 type More = ModuleT MoreState LB
12
13 -- the @more state is handled centrally
14 morePlugin :: Module (GlobalPrivate () [String])
15 morePlugin = newModule
16 { moduleDefState = return $ mkGlobalPrivate 20 ()
17 , moduleInit = registerOutputFilter moreFilter
18 -- TODO: improve output filter system...
19 -- currently, @more output will bypass any filters in the
20 -- chain after 'moreFilter'
21
22 , moduleCmds = return
23 [ (command "more")
24 { help = say "@more. Return more output from the bot buffer."
25 , process = \_ -> do
26 target <- getTarget
27 morestate <- readPS target
28 -- TODO: test theory that we can just "say" morestate;
29 -- it should end up going through the moreFilter as needed
30 case morestate of
31 Nothing -> return ()
32 Just ls -> lift (moreFilter target ls)
33 >>= mapM_ (lb . ircPrivmsg' target)
34 }
35 ]
36 }
37
38 moreFilter :: Nick -> [String] -> More [String]
39 moreFilter target msglines = do
40 let (morelines, thislines) = case drop (maxLines+2) msglines of
41 [] -> ([],msglines)
42 _ -> (drop maxLines msglines, take maxLines msglines)
43 writePS target $ if null morelines then Nothing else Just morelines
44 return $ thislines ++ if null morelines
45 then []
46 else ['[':shows (length morelines) " @more lines]"]
47
48 where maxLines = 5 -- arbitrary, really