From b93fbeee6d7096352a124bf85c8d2b53b94c52cd Mon Sep 17 00:00:00 2001 From: ndwarshuis Date: Wed, 23 Jun 2021 00:38:23 -0400 Subject: [PATCH] ENH check for vpn profiles before enabling vpn in xmobar --- bin/xmobar.hs | 28 ++++++++++++++++++++++------ my-xmonad.cabal | 1 + 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/bin/xmobar.hs b/bin/xmobar.hs index 769eefb..398a62e 100644 --- a/bin/xmobar.hs +++ b/bin/xmobar.hs @@ -20,8 +20,10 @@ import Data.Maybe import DBus import System.Directory +import System.Exit import System.FilePath.Posix import System.IO.Error +import System.Process (readProcessWithExitCode) import Xmobar.Plugins.Bluetooth import Xmobar.Plugins.Device @@ -114,9 +116,28 @@ getWireless = do where hasWireless p = doesPathExist $ sysfsNet p "wireless" +vpnCmd :: CmdSpec +vpnCmd = CmdSpec + { csAlias = vpnAlias + , csDepends = Just $ sysDepends vpnBus vpnPath + , csRunnable = Run + $ VPN ("\xf023", T.fgColor, T.backdropFgColor) 5 + } + +getVPN :: IO (Maybe CmdSpec) +getVPN = do + -- TODO ensure nmcli exists (or guard against an exception if it doesn't) + (ec, out, _) <- readProcessWithExitCode "nmcli" args "" + return $ case ec of + ExitSuccess -> if "vpn" `elem` lines out then Just vpnCmd else Nothing + _ -> Nothing + where + args = ["-c", "no", "-t", "-f", "TYPE", "c", "show"] + myCommands :: IO BarRegions myCommands = do wirelessSpec <- getWireless + vpnSpec <- getVPN let left = [ CmdSpec { csAlias = "UnsafeStdinReader" @@ -134,12 +155,7 @@ myCommands = do $ Device ("enp7s0f1", "\xf0e8", T.fgColor, T.backdropFgColor) 5 } - , Just $ CmdSpec - { csAlias = vpnAlias - , csDepends = Just $ sysDepends vpnBus vpnPath - , csRunnable = Run - $ VPN ("\xf023", T.fgColor, T.backdropFgColor) 5 - } + , vpnSpec , Just $ CmdSpec { csAlias = btAlias diff --git a/my-xmonad.cabal b/my-xmonad.cabal index c5799da..f81a9a2 100644 --- a/my-xmonad.cabal +++ b/my-xmonad.cabal @@ -65,6 +65,7 @@ executable xmobar , my-xmonad , xmobar , xmonad >= 0.13 + , process >= 1.6.5.0 , filepath >= 1.4.2.1 , xmonad-contrib >= 0.13 , directory >= 1.3.3.0