xmonad-config/lib/Xmobar/Plugins/Common.hs

31 lines
943 B
Haskell
Raw Normal View History

module Xmobar.Plugins.Common
2021-11-24 01:14:23 -05:00
( chooseColor
, startListener
)
where
2021-11-24 01:14:23 -05:00
import DBus
import DBus.Client
import Data.Maybe
import XMonad.Hooks.DynamicLog (xmobarColor)
import XMonad.Internal.DBus.Common
startListener :: IsVariant a => MatchRule -> (Client -> IO [Variant])
-> ([Variant] -> SignalMatch a) -> (a -> String) -> (String -> IO ()) -> IO ()
startListener rule getProp fromSignal toColor cb = do
withDBusClientConnection_ True $ \c -> do
reply <- getProp c
procMatch $ maybe Failure Match $ fromVariant =<< listToMaybe reply
addMatchCallback rule (procMatch . fromSignal) c
where
procMatch (Match t) = cb $ toColor t
procMatch Failure = cb "N/A"
procMatch NoMatch = return ()
chooseColor :: String -> String -> String -> Bool -> String
chooseColor text colorOn colorOff state =
xmobarColor (if state then colorOn else colorOff) "" text