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

64 lines
1.7 KiB
Haskell
Raw Normal View History

module Xmobar.Plugins.Common
2021-11-27 17:33:02 -05:00
( colorText
2021-11-24 01:14:23 -05:00
, startListener
, procSignalMatch
2021-11-25 00:12:00 -05:00
, na
, fromSingletonVariant
, withDBusClientConnection
, Callback
2021-11-27 17:33:02 -05:00
, Colors(..)
2021-11-27 13:24:13 -05:00
, displayMaybe
, displayMaybe'
2021-11-27 17:33:02 -05:00
, xmobarFGColor
2021-11-24 01:14:23 -05:00
)
where
import Control.Monad
2022-07-09 17:44:14 -04:00
import Data.Internal.DBus
2021-11-24 01:14:23 -05:00
import DBus
import DBus.Client
2022-07-09 17:44:14 -04:00
import XMonad.Hooks.DynamicLog (xmobarColor)
2021-11-24 01:14:23 -05:00
type Callback = String -> IO ()
2021-11-27 17:33:02 -05:00
data Colors = Colors
{ colorsOn :: String
, colorsOff :: String
}
deriving (Eq, Show, Read)
2022-07-09 17:44:14 -04:00
startListener :: (SafeClient c, IsVariant a) => MatchRule -> (c -> IO [Variant])
-> ([Variant] -> SignalMatch a) -> (a -> IO String) -> Callback
2022-07-09 17:44:14 -04:00
-> c -> IO ()
2021-11-25 00:12:00 -05:00
startListener rule getProp fromSignal toColor cb client = do
reply <- getProp client
2021-11-27 13:24:13 -05:00
displayMaybe cb toColor $ fromSingletonVariant reply
void $ addMatchCallback rule (procMatch . fromSignal) client
2021-11-24 01:14:23 -05:00
where
procMatch = procSignalMatch cb toColor
2021-11-27 13:24:13 -05:00
procSignalMatch :: Callback -> (a -> IO String) -> SignalMatch a -> IO ()
procSignalMatch cb f = withSignalMatch (displayMaybe cb f)
2021-11-27 17:33:02 -05:00
colorText :: Colors -> Bool -> String -> String
colorText Colors { colorsOn = c } True = xmobarFGColor c
colorText Colors { colorsOff = c } False = xmobarFGColor c
xmobarFGColor :: String -> String -> String
xmobarFGColor c = xmobarColor c ""
2021-11-25 00:12:00 -05:00
na :: String
na = "N/A"
2021-11-27 13:24:13 -05:00
displayMaybe :: Callback -> (a -> IO String) -> Maybe a -> IO ()
displayMaybe cb f = cb <=< maybe (return na) f
displayMaybe' :: Callback -> (a -> IO ()) -> Maybe a -> IO ()
displayMaybe' cb = maybe (cb na)
2022-07-09 17:08:10 -04:00
withDBusClientConnection :: SafeClient c => Callback -> (c -> IO ()) -> IO ()
withDBusClientConnection cb f = displayMaybe' cb f =<< getDBusClient