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

76 lines
2.0 KiB
Haskell
Raw Normal View History

{-# LANGUAGE OverloadedStrings #-}
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
2022-12-30 14:58:23 -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
)
2022-12-30 14:58:23 -05:00
where
2022-12-30 14:58:23 -05:00
import DBus
import DBus.Client
import Data.Internal.DBus
import RIO
2022-12-30 14:58:23 -05:00
import qualified RIO.Text as T
import XMonad.Hooks.DynamicLog (xmobarColor)
2021-11-24 01:14:23 -05:00
-- use string here since all the callbacks in xmobar use strings :(
type Callback = String -> IO ()
2021-11-27 17:33:02 -05:00
data Colors = Colors
2022-12-30 14:58:23 -05:00
{ colorsOn :: T.Text
, colorsOff :: T.Text
2021-11-27 17:33:02 -05:00
}
deriving (Eq, Show, Read)
2022-12-30 14:58:23 -05:00
startListener
:: (MonadUnliftIO m, SafeClient c, IsVariant a)
2022-12-30 14:58:23 -05:00
=> MatchRule
-> (c -> m [Variant])
2022-12-30 14:58:23 -05:00
-> ([Variant] -> SignalMatch a)
-> (a -> m T.Text)
2022-12-30 14:58:23 -05:00
-> Callback
-> c
-> m ()
2021-11-25 00:12:00 -05:00
startListener rule getProp fromSignal toColor cb client = do
reply <- getProp client
displayMaybe cb toColor $ fromSingletonVariant reply
void $ addMatchCallback rule (procMatch . fromSignal) client
2021-11-24 01:14:23 -05:00
where
procMatch = procSignalMatch cb toColor
procSignalMatch
:: MonadUnliftIO m => Callback -> (a -> m T.Text) -> SignalMatch a -> m ()
2021-11-27 13:24:13 -05:00
procSignalMatch cb f = withSignalMatch (displayMaybe cb f)
colorText :: Colors -> Bool -> T.Text -> T.Text
2022-12-30 14:58:23 -05:00
colorText Colors {colorsOn = c} True = xmobarFGColor c
colorText Colors {colorsOff = c} False = xmobarFGColor c
2021-11-27 17:33:02 -05:00
xmobarFGColor :: T.Text -> T.Text -> T.Text
xmobarFGColor c = T.pack . xmobarColor (T.unpack c) "" . T.unpack
2021-11-25 00:12:00 -05:00
na :: T.Text
2021-11-25 00:12:00 -05:00
na = "N/A"
displayMaybe :: MonadUnliftIO m => Callback -> (a -> m T.Text) -> Maybe a -> m ()
displayMaybe cb f = (liftIO . cb . T.unpack) <=< maybe (return na) f
2021-11-27 13:24:13 -05:00
displayMaybe' :: MonadUnliftIO m => Callback -> (a -> m ()) -> Maybe a -> m ()
displayMaybe' cb = maybe (liftIO $ cb $ T.unpack na)
withDBusClientConnection
:: (MonadUnliftIO m, SafeClient c)
=> Callback
-> (c -> RIO SimpleApp ())
-> m ()
withDBusClientConnection cb f = runSimpleApp $ displayMaybe' cb f =<< getDBusClient