2022-12-26 14:45:49 -05:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
|
2021-11-24 00:43:58 -05:00
|
|
|
module Xmobar.Plugins.Common
|
2021-11-27 17:33:02 -05:00
|
|
|
( colorText
|
2021-11-24 01:14:23 -05:00
|
|
|
, startListener
|
2021-11-26 23:35:03 -05:00
|
|
|
, procSignalMatch
|
2021-11-25 00:12:00 -05:00
|
|
|
, na
|
2021-11-26 23:35:03 -05:00
|
|
|
, fromSingletonVariant
|
|
|
|
, withDBusClientConnection
|
|
|
|
, Callback
|
2021-11-27 17:33:02 -05:00
|
|
|
, Colors(..)
|
2021-11-27 13:24:13 -05:00
|
|
|
, displayMaybe
|
2022-12-30 10:38:21 -05:00
|
|
|
, displayMaybe_
|
2021-11-27 17:33:02 -05:00
|
|
|
, xmobarFGColor
|
2021-11-24 01:14:23 -05:00
|
|
|
)
|
2021-11-24 00:43:58 -05:00
|
|
|
where
|
|
|
|
|
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-12-30 10:38:21 -05:00
|
|
|
import RIO
|
2022-12-26 14:45:49 -05:00
|
|
|
import qualified RIO.Text as T
|
|
|
|
|
2022-07-09 17:44:14 -04:00
|
|
|
import XMonad.Hooks.DynamicLog (xmobarColor)
|
2021-11-24 01:14:23 -05:00
|
|
|
|
2022-12-26 14:45:49 -05:00
|
|
|
-- use string here since all the callbacks in xmobar use strings :(
|
2021-11-26 23:35:03 -05:00
|
|
|
type Callback = String -> IO ()
|
|
|
|
|
2021-11-27 17:33:02 -05:00
|
|
|
data Colors = Colors
|
2022-12-26 14:45:49 -05:00
|
|
|
{ colorsOn :: T.Text
|
|
|
|
, colorsOff :: T.Text
|
2021-11-27 17:33:02 -05:00
|
|
|
}
|
|
|
|
deriving (Eq, Show, Read)
|
|
|
|
|
2022-12-30 10:38:21 -05:00
|
|
|
startListener :: (MonadIO m, SafeClient c, IsVariant a) => MatchRule -> (c -> m [Variant])
|
2022-12-26 14:45:49 -05:00
|
|
|
-> ([Variant] -> SignalMatch a) -> (a -> IO T.Text) -> Callback
|
2022-12-30 10:38:21 -05:00
|
|
|
-> c -> m ()
|
2021-11-25 00:12:00 -05:00
|
|
|
startListener rule getProp fromSignal toColor cb client = do
|
|
|
|
reply <- getProp client
|
2022-12-30 10:38:21 -05:00
|
|
|
displayMaybe cb (liftIO . toColor) $ fromSingletonVariant reply
|
2021-11-26 23:35:03 -05:00
|
|
|
void $ addMatchCallback rule (procMatch . fromSignal) client
|
2021-11-24 01:14:23 -05:00
|
|
|
where
|
2021-11-26 23:35:03 -05:00
|
|
|
procMatch = procSignalMatch cb toColor
|
|
|
|
|
2022-12-30 10:38:21 -05:00
|
|
|
procSignalMatch
|
|
|
|
:: Callback
|
|
|
|
-> (a -> IO T.Text)
|
|
|
|
-> SignalMatch a
|
|
|
|
-> IO ()
|
2021-11-27 13:24:13 -05:00
|
|
|
procSignalMatch cb f = withSignalMatch (displayMaybe cb f)
|
2021-11-24 00:43:58 -05:00
|
|
|
|
2022-12-26 14:45:49 -05:00
|
|
|
colorText :: Colors -> Bool -> T.Text -> T.Text
|
2021-11-27 17:33:02 -05:00
|
|
|
colorText Colors { colorsOn = c } True = xmobarFGColor c
|
|
|
|
colorText Colors { colorsOff = c } False = xmobarFGColor c
|
|
|
|
|
2022-12-26 14:45:49 -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
|
|
|
|
2022-12-26 14:45:49 -05:00
|
|
|
na :: T.Text
|
2021-11-25 00:12:00 -05:00
|
|
|
na = "N/A"
|
2021-11-26 23:35:03 -05:00
|
|
|
|
2022-12-30 10:38:21 -05:00
|
|
|
displayMaybe :: (MonadIO 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
|
|
|
|
2022-12-30 10:38:21 -05:00
|
|
|
displayMaybe_ :: MonadIO m => Callback -> (a -> m ()) -> Maybe a -> m ()
|
|
|
|
displayMaybe_ cb = maybe (liftIO $ cb $ T.unpack na)
|
2021-11-26 23:35:03 -05:00
|
|
|
|
2022-12-30 10:38:21 -05:00
|
|
|
withDBusClientConnection
|
|
|
|
:: (SafeClient c)
|
|
|
|
=> Callback
|
|
|
|
-> (c -> RIO SimpleApp ())
|
|
|
|
-> IO ()
|
|
|
|
withDBusClientConnection cb f = do
|
|
|
|
-- TODO be more sophisticated
|
|
|
|
runSimpleApp $ withDBusClient_ $ displayMaybe_ cb f . Just
|