ENH check for ethernet interface in xmobar

This commit is contained in:
Nathan Dwarshuis 2021-06-23 20:19:55 -04:00
parent b93fbeee6d
commit 0066ad05b9
1 changed files with 32 additions and 14 deletions

View File

@ -21,7 +21,6 @@ import DBus
import System.Directory import System.Directory
import System.Exit import System.Exit
import System.FilePath.Posix
import System.IO.Error import System.IO.Error
import System.Process (readProcessWithExitCode) import System.Process (readProcessWithExitCode)
@ -95,26 +94,49 @@ sysfsNet :: FilePath
sysfsNet = "/sys/class/net" sysfsNet = "/sys/class/net"
wirelessCmd :: String -> CmdSpec wirelessCmd :: String -> CmdSpec
wirelessCmd interface = CmdSpec wirelessCmd iface = CmdSpec
{ csAlias = interface ++ "wi" { csAlias = iface ++ "wi"
, csDepends = Nothing , csDepends = Nothing
, csRunnable = Run , csRunnable = Run
$ Wireless interface $ Wireless iface
[ "-t", "<qualityipat><essid>" [ "-t", "<qualityipat><essid>"
, "--" , "--"
, "--quality-icon-pattern", "<icon=wifi_%%.xpm/>" , "--quality-icon-pattern", "<icon=wifi_%%.xpm/>"
] 5 ] 5
} }
ethernetCmd :: String -> CmdSpec
ethernetCmd iface = CmdSpec
{ csAlias = iface
, csDepends = Just $ sysDepends devBus devPath
, csRunnable = Run
$ Device ("enp7s0f1", "<fn=2>\xf0e8</fn>", T.fgColor, T.backdropFgColor) 5
}
isWireless :: String -> Bool
isWireless ('w':'l':_) = True
isWireless _ = False
isEthernet :: String -> Bool
isEthernet ('e':'n':_) = True
isEthernet _ = False
listInterfaces :: IO [String]
listInterfaces = fromRight [] <$> tryIOError (listDirectory sysfsNet)
getWireless :: IO (Maybe CmdSpec) getWireless :: IO (Maybe CmdSpec)
getWireless = do getWireless = do
r <- tryIOError (listDirectory sysfsNet) ns <- filter isWireless <$> listInterfaces
ns <- filterM hasWireless $ fromRight [] r
return $ case ns of return $ case ns of
[n] -> Just $ wirelessCmd n [n] -> Just $ wirelessCmd n
_ -> Nothing _ -> Nothing
where
hasWireless p = doesPathExist $ sysfsNet </> p </> "wireless" getEthernet :: IO (Maybe CmdSpec)
getEthernet = do
ns <- filter isEthernet <$> listInterfaces
return $ case ns of
[n] -> Just $ ethernetCmd n
_ -> Nothing
vpnCmd :: CmdSpec vpnCmd :: CmdSpec
vpnCmd = CmdSpec vpnCmd = CmdSpec
@ -137,6 +159,7 @@ getVPN = do
myCommands :: IO BarRegions myCommands :: IO BarRegions
myCommands = do myCommands = do
wirelessSpec <- getWireless wirelessSpec <- getWireless
ethernetSpec <- getEthernet
vpnSpec <- getVPN vpnSpec <- getVPN
let left = let left =
[ CmdSpec [ CmdSpec
@ -148,12 +171,7 @@ myCommands = do
let right = let right =
[ wirelessSpec [ wirelessSpec
, Just $ CmdSpec , ethernetSpec
{ csAlias = "enp7s0f1"
, csDepends = Just $ sysDepends devBus devPath
, csRunnable = Run
$ Device ("enp7s0f1", "<fn=2>\xf0e8</fn>", T.fgColor, T.backdropFgColor) 5
}
, vpnSpec , vpnSpec