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

36 lines
1.1 KiB
Haskell
Raw Normal View History

2020-03-21 18:37:26 -04:00
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
module Xmobar.Plugins.NetworkManager where
import DBus
import DBus.Client
import Xmobar
data NetworkManager = NetworkManager (String, String, String) Int
deriving (Read, Show)
2020-03-21 18:37:26 -04:00
callConnectionType :: Client -> IO (Either MethodError Variant)
callConnectionType client =
getProperty client (methodCall "/org/freedesktop/NetworkManager"
"org.freedesktop.NetworkManager" "PrimaryConnectionType")
{ methodCallDestination = Just "org.freedesktop.NetworkManager" }
2020-03-21 18:37:26 -04:00
instance Exec NetworkManager where
alias (NetworkManager _ _) = "networkmanager"
rate (NetworkManager _ r) = r
run (NetworkManager (text, colorOn, colorOff) _) = do
2020-03-21 18:37:26 -04:00
client <- connectSystem
reply <- callConnectionType client
disconnect client
return $ fmtState $ procReply reply
2020-03-21 18:37:26 -04:00
where
procReply = \case
Right r -> (fromVariant r :: Maybe String)
Left _ -> Nothing
2020-03-21 18:37:26 -04:00
fmtState = \case
Just s -> wrapColor text $ if s == "vpn" then colorOn else colorOff
2020-03-21 18:37:26 -04:00
Nothing -> "N/A"
wrapColor s c = "<fc=" ++ c ++ ">" ++ s ++ "</fc>"