REF clean up code

This commit is contained in:
Nathan Dwarshuis 2021-11-24 01:14:23 -05:00
parent d2268da188
commit 20b915631e
2 changed files with 27 additions and 19 deletions

View File

@ -1,9 +1,29 @@
module Xmobar.Plugins.Common module Xmobar.Plugins.Common
(chooseColor) ( chooseColor
, startListener
)
where where
import DBus
import DBus.Client
import Data.Maybe
import XMonad.Hooks.DynamicLog (xmobarColor) 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 :: String -> String -> String -> Bool -> String
chooseColor text colorOn colorOff state = chooseColor text colorOn colorOff state =

View File

@ -9,10 +9,7 @@ module Xmobar.Plugins.VPN
, vpnDep , vpnDep
) where ) where
import Data.Maybe
import DBus import DBus
import DBus.Client
import XMonad.Internal.DBus.Common import XMonad.Internal.DBus.Common
import XMonad.Internal.Dependency import XMonad.Internal.Dependency
@ -39,21 +36,12 @@ vpnAlias = "vpn"
vpnDep :: DBusDep vpnDep :: DBusDep
vpnDep = Endpoint vpnBus vpnPath vpnInterface $ Property_ vpnConnType vpnDep = Endpoint vpnBus vpnPath vpnInterface $ Property_ vpnConnType
matchConnType :: [Variant] -> SignalMatch String
matchConnType = matchPropertyChanged vpnInterface vpnConnType fromVariant
callGetConnectionType :: Client -> IO [Variant]
callGetConnectionType = callPropertyGet vpnBus vpnPath vpnInterface vpnConnType
instance Exec VPN where instance Exec VPN where
alias (VPN _) = vpnAlias alias (VPN _) = vpnAlias
start (VPN (text, colorOn, colorOff)) cb = do start (VPN (text, colorOn, colorOff)) cb =
withDBusClientConnection_ True $ \c -> do startListener rule getProp fromSignal chooseColor' cb
reply <- callGetConnectionType c
cb $ maybe "N/A" chooseColor' $ fromVariant =<< listToMaybe reply
addMatchCallback (matchProperty vpnPath) (procMatch cb . matchConnType) c
where where
procMatch f (Match t) = f $ chooseColor' t rule = matchProperty vpnPath
procMatch f Failure = f "N/A" getProp = callPropertyGet vpnBus vpnPath vpnInterface vpnConnType
procMatch _ NoMatch = return () fromSignal = matchPropertyChanged vpnInterface vpnConnType fromVariant
chooseColor' = chooseColor text colorOn colorOff . ("vpn" ==) chooseColor' = chooseColor text colorOn colorOff . ("vpn" ==)