{-# LANGUAGE CPP #-}
module Xmobar.Plugins.MBox (MBox(..)) where
import Prelude
import Xmobar.Run.Exec
#ifdef INOTIFY
import Xmobar.Plugins.Monitors.Common (parseOptsWith)
import Xmobar.System.Utils (changeLoop, expandHome)
import Control.Monad (when)
import Control.Concurrent.STM
import Control.Exception (SomeException (..), handle, evaluate)
import System.Console.GetOpt
import System.Directory (doesFileExist)
import System.FilePath ((</>))
import System.INotify (Event(..), EventVariety(..), initINotify, addWatch)
import qualified Data.ByteString.Lazy.Char8 as B
#if MIN_VERSION_hinotify(0,3,10)
import qualified Data.ByteString.Char8 as BS (ByteString, pack)
pack :: String -> BS.ByteString
pack :: [Char] -> ByteString
pack = [Char] -> ByteString
BS.pack
#else
pack :: String -> String
pack = id
#endif
data Options = Options
{ Options -> Bool
oAll :: Bool
, Options -> Bool
oUniq :: Bool
, Options -> [Char]
oDir :: FilePath
, Options -> [Char]
oPrefix :: String
, Options -> [Char]
oSuffix :: String
}
defaults :: Options
defaults :: Options
defaults = Options {
oAll :: Bool
oAll = Bool
False, oUniq :: Bool
oUniq = Bool
False, oDir :: [Char]
oDir = [Char]
"", oPrefix :: [Char]
oPrefix = [Char]
"", oSuffix :: [Char]
oSuffix = [Char]
""
}
options :: [OptDescr (Options -> Options)]
options :: [OptDescr (Options -> Options)]
options =
[ [Char]
-> [[Char]]
-> ArgDescr (Options -> Options)
-> [Char]
-> OptDescr (Options -> Options)
forall a. [Char] -> [[Char]] -> ArgDescr a -> [Char] -> OptDescr a
Option [Char]
"a" [[Char]
"all"] ((Options -> Options) -> ArgDescr (Options -> Options)
forall a. a -> ArgDescr a
NoArg (\Options
o -> Options
o { oAll = True })) [Char]
""
, [Char]
-> [[Char]]
-> ArgDescr (Options -> Options)
-> [Char]
-> OptDescr (Options -> Options)
forall a. [Char] -> [[Char]] -> ArgDescr a -> [Char] -> OptDescr a
Option [Char]
"u" [] ((Options -> Options) -> ArgDescr (Options -> Options)
forall a. a -> ArgDescr a
NoArg (\Options
o -> Options
o { oUniq = True })) [Char]
""
, [Char]
-> [[Char]]
-> ArgDescr (Options -> Options)
-> [Char]
-> OptDescr (Options -> Options)
forall a. [Char] -> [[Char]] -> ArgDescr a -> [Char] -> OptDescr a
Option [Char]
"d" [[Char]
"dir"] (([Char] -> Options -> Options)
-> [Char] -> ArgDescr (Options -> Options)
forall a. ([Char] -> a) -> [Char] -> ArgDescr a
ReqArg (\[Char]
x Options
o -> Options
o { oDir = x }) [Char]
"") [Char]
""
, [Char]
-> [[Char]]
-> ArgDescr (Options -> Options)
-> [Char]
-> OptDescr (Options -> Options)
forall a. [Char] -> [[Char]] -> ArgDescr a -> [Char] -> OptDescr a
Option [Char]
"p" [[Char]
"prefix"] (([Char] -> Options -> Options)
-> [Char] -> ArgDescr (Options -> Options)
forall a. ([Char] -> a) -> [Char] -> ArgDescr a
ReqArg (\[Char]
x Options
o -> Options
o { oPrefix = x }) [Char]
"") [Char]
""
, [Char]
-> [[Char]]
-> ArgDescr (Options -> Options)
-> [Char]
-> OptDescr (Options -> Options)
forall a. [Char] -> [[Char]] -> ArgDescr a -> [Char] -> OptDescr a
Option [Char]
"s" [[Char]
"suffix"] (([Char] -> Options -> Options)
-> [Char] -> ArgDescr (Options -> Options)
forall a. ([Char] -> a) -> [Char] -> ArgDescr a
ReqArg (\[Char]
x Options
o -> Options
o { oSuffix = x }) [Char]
"") [Char]
""
]
#else
import System.IO
#endif
data MBox = MBox [(String, FilePath, String)] [String] String
deriving (ReadPrec [MBox]
ReadPrec MBox
Int -> ReadS MBox
ReadS [MBox]
(Int -> ReadS MBox)
-> ReadS [MBox] -> ReadPrec MBox -> ReadPrec [MBox] -> Read MBox
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS MBox
readsPrec :: Int -> ReadS MBox
$creadList :: ReadS [MBox]
readList :: ReadS [MBox]
$creadPrec :: ReadPrec MBox
readPrec :: ReadPrec MBox
$creadListPrec :: ReadPrec [MBox]
readListPrec :: ReadPrec [MBox]
Read, Int -> MBox -> ShowS
[MBox] -> ShowS
MBox -> [Char]
(Int -> MBox -> ShowS)
-> (MBox -> [Char]) -> ([MBox] -> ShowS) -> Show MBox
forall a.
(Int -> a -> ShowS) -> (a -> [Char]) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> MBox -> ShowS
showsPrec :: Int -> MBox -> ShowS
$cshow :: MBox -> [Char]
show :: MBox -> [Char]
$cshowList :: [MBox] -> ShowS
showList :: [MBox] -> ShowS
Show)
instance Exec MBox where
alias :: MBox -> [Char]
alias (MBox [([Char], [Char], [Char])]
_ [[Char]]
_ [Char]
a) = [Char]
a
#ifndef INOTIFY
start _ _ =
hPutStrLn stderr $ "Warning: xmobar is not compiled with -fwith_inotify" ++
" but the MBox plugin requires it"
#else
start :: MBox -> ([Char] -> IO ()) -> IO ()
start (MBox [([Char], [Char], [Char])]
boxes [[Char]]
args [Char]
_) [Char] -> IO ()
cb = do
opts <- [OptDescr (Options -> Options)]
-> Options -> [[Char]] -> IO Options
forall opts.
[OptDescr (opts -> opts)] -> opts -> [[Char]] -> IO opts
parseOptsWith [OptDescr (Options -> Options)]
options Options
defaults [[Char]]
args
let showAll = Options -> Bool
oAll Options
opts
prefix = Options -> [Char]
oPrefix Options
opts
suffix = Options -> [Char]
oSuffix Options
opts
uniq = Options -> Bool
oUniq Options
opts
names = (([Char], [Char], [Char]) -> [Char])
-> [([Char], [Char], [Char])] -> [[Char]]
forall a b. (a -> b) -> [a] -> [b]
map (\([Char]
t, [Char]
_, [Char]
_) -> [Char]
t) [([Char], [Char], [Char])]
boxes
colors = (([Char], [Char], [Char]) -> [Char])
-> [([Char], [Char], [Char])] -> [[Char]]
forall a b. (a -> b) -> [a] -> [b]
map (\([Char]
_, [Char]
_, [Char]
c) -> [Char]
c) [([Char], [Char], [Char])]
boxes
extractPath (a
_, [Char]
f, c
_) = [Char] -> IO [Char]
expandHome ([Char] -> IO [Char]) -> [Char] -> IO [Char]
forall a b. (a -> b) -> a -> b
$ Options -> [Char]
oDir Options
opts [Char] -> ShowS
</> [Char]
f
events = [EventVariety
CloseWrite]
i <- initINotify
vs <- mapM (\([Char], [Char], [Char])
b -> do
f <- ([Char], [Char], [Char]) -> IO [Char]
forall {a} {c}. (a, [Char], c) -> IO [Char]
extractPath ([Char], [Char], [Char])
b
exists <- doesFileExist f
n <- if exists then countMails f else return (-1)
v <- newTVarIO (f, n)
when exists $
addWatch i events (pack f) (handleNotification v) >> return ()
return v)
boxes
changeLoop (mapM (fmap snd . readTVar) vs) $ \[Int]
ns ->
let s :: [Char]
s = [[Char]] -> [Char]
unwords [ Bool -> [Char] -> Int -> ShowS
showC Bool
uniq [Char]
m Int
n [Char]
c | ([Char]
m, Int
n, [Char]
c) <- [[Char]] -> [Int] -> [[Char]] -> [([Char], Int, [Char])]
forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
zip3 [[Char]]
names [Int]
ns [[Char]]
colors
, Bool
showAll Bool -> Bool -> Bool
|| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 ]
in [Char] -> IO ()
cb (if [Char] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Char]
s then [Char]
"" else [Char]
prefix [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
s [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
suffix)
showC :: Bool -> String -> Int -> String -> String
showC :: Bool -> [Char] -> Int -> ShowS
showC Bool
u [Char]
m Int
n [Char]
c =
if [Char]
c [Char] -> [Char] -> Bool
forall a. Eq a => a -> a -> Bool
== [Char]
"" then [Char]
msg else [Char]
"<fc=" [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
c [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
">" [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
msg [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ [Char]
"</fc>"
where msg :: [Char]
msg = [Char]
m [Char] -> ShowS
forall a. [a] -> [a] -> [a]
++ if Bool -> Bool
not Bool
u Bool -> Bool -> Bool
|| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1 then Int -> [Char]
forall a. Show a => a -> [Char]
show Int
n else [Char]
""
countMails :: FilePath -> IO Int
countMails :: [Char] -> IO Int
countMails [Char]
f =
(SomeException -> IO Int) -> IO Int -> IO Int
forall e a. Exception e => (e -> IO a) -> IO a -> IO a
handle (\(SomeException e
_) -> Int -> IO Int
forall a. a -> IO a
evaluate Int
0)
(do txt <- [Char] -> IO ByteString
B.readFile [Char]
f
evaluate $! length . filter (B.isPrefixOf from) . B.lines $ txt)
where from :: ByteString
from = [Char] -> ByteString
B.pack [Char]
"From "
handleNotification :: TVar (FilePath, Int) -> Event -> IO ()
handleNotification :: TVar ([Char], Int) -> Event -> IO ()
handleNotification TVar ([Char], Int)
v Event
_ = do
(p, _) <- STM ([Char], Int) -> IO ([Char], Int)
forall a. STM a -> IO a
atomically (STM ([Char], Int) -> IO ([Char], Int))
-> STM ([Char], Int) -> IO ([Char], Int)
forall a b. (a -> b) -> a -> b
$ TVar ([Char], Int) -> STM ([Char], Int)
forall a. TVar a -> STM a
readTVar TVar ([Char], Int)
v
n <- countMails p
atomically $ writeTVar v (p, n)
#endif