take command path as argument

Ricard Illa 2021-08-04 20:56:11 +02:00
parent e7d15f9718
commit 37e8aa3554
No known key found for this signature in database
GPG Key ID: F69A672B72E54902
4 changed files with 34 additions and 34 deletions

View File

@ -9,15 +9,15 @@ import Monitors.Net (queryNet)
import Monitors.Volume (queryVolume)
usage :: IO String
usage = printf "%s battery | volume | net" <$> getProgName
usage = printf "%s bat | vol | net | date" <$> getProgName
main :: IO ()
main = do
args <- getArgs
output <- case args of
[ "bat" ] -> queryBattery
[ "vol" ] -> queryVolume
[ "net" ] -> queryNet
[ "bat", cmd ] -> queryBattery cmd
[ "vol", cmd ] -> queryVolume cmd
[ "net", cmd ] -> queryNet cmd
[ "date" ] -> queryDate True
[ "date-min" ] -> queryDate False
_ -> usage

View File

@ -127,8 +127,8 @@ parseAcpiStdout = mapMaybe parseBatteryInfo . lines
fmtData :: [BatteryData] -> String
fmtData = (separator ++ ) . intercalate separator . map getStatus
runAcpi :: IO String
runAcpi = readProcess "acpi" ["-b"] ""
runAcpi :: String -> IO String
runAcpi cmd = readProcess cmd ["-b"] ""
queryBattery :: IO String
queryBattery = fmtData . parseAcpiStdout <$> runAcpi
queryBattery :: String -> IO String
queryBattery cmd = fmtData . parseAcpiStdout <$> runAcpi cmd

View File

@ -29,23 +29,23 @@ icons = M.map buildIcon $ M.fromList
offline :: String
offline = colorize (colors M.! "inactive") (icons M.! "wifi-off")
getConnectivity :: IO String
getConnectivity = head . lines <$> readProcess "nmcli" args ""
getConnectivity :: String -> IO String
getConnectivity cmd = head . lines <$> readProcess cmd args ""
where args = ["networking", "connectivity", "check"]
getDevStatus :: IO [DeviceData]
getDevStatus = readProcess "nmcli" args "" >>= mapM buildDeviceData . lines
getDevStatus :: String -> IO [DeviceData]
getDevStatus cmd = readProcess cmd args "" >>= mapM (buildDeviceData cmd) . lines
where
args = ["--terse", "--fields", "device,type,state", "device", "status"]
buildDeviceData :: String -> IO DeviceData
buildDeviceData x =
buildDeviceData :: String -> String -> IO DeviceData
buildDeviceData cmd x =
let
[d,t,state] = splitOn ":" x
deviceType = readDeviceType t
connected = state == "connected"
in do
signal <- getWifiSignal deviceType d
signal <- getWifiSignal cmd deviceType d
return DeviceData
{ deviceName = d
, deviceType = deviceType
@ -58,8 +58,8 @@ readDeviceType "wifi" = Wifi
readDeviceType "ethernet" = Ethernet
readDeviceType _ = OtherDevice
getWifiSignal :: DeviceType -> String -> IO (Maybe Int)
getWifiSignal Wifi dev = parseStdout <$> readProcess "nmcli" args ""
getWifiSignal :: String -> DeviceType -> String -> IO (Maybe Int)
getWifiSignal cmd Wifi dev = parseStdout <$> readProcess cmd args ""
where
args =
[ "--terse"
@ -71,10 +71,10 @@ getWifiSignal Wifi dev = parseStdout <$> readProcess "nmcli" args ""
isActive = (=="*") . head . splt
readVal = read . (!! 1) . splt
parseStdout = fmap readVal . find isActive . lines
getWifiSignal _ _ = return Nothing
getWifiSignal _ _ _ = return Nothing
getActiveDevs :: IO [DeviceData]
getActiveDevs = filter activeDev <$> getDevStatus
getActiveDevs :: String -> IO [DeviceData]
getActiveDevs cmd = filter activeDev <$> getDevStatus cmd
where
activeDev :: DeviceData -> Bool
activeDev DeviceData { deviceType = Wifi, connected = True } = True
@ -114,11 +114,11 @@ getStatus :: [DeviceData] -> String
getStatus = intercalate separator . map makeDevIcon
queryNet :: IO String
queryNet = do
connectivity <- getConnectivity
queryNet :: String -> IO String
queryNet cmd = do
connectivity <- getConnectivity cmd
--icon <- if connectivity `elem` ["none", "limited"]
icon <- if connectivity == "none"
then return offline
else getStatus <$> getActiveDevs
else getStatus <$> getActiveDevs cmd
return $ separator ++ icon

View File

@ -76,24 +76,24 @@ parseMixerInfo = fmap fmtMixer . matchRegex regex
fmtData :: MixerData -> String
fmtData = (separator ++ ) . getStatus
getMute :: IO Bool
getMute = parseMute <$> readProcess "/nix/store/ambq4n07zlax0lwz0v1yph85cph1sssk-pamixer-1.4/bin/pamixer" ["--get-mute"] ""
getMute :: String -> IO Bool
getMute cmd = parseMute <$> readProcess cmd ["--get-mute"] ""
where
parseMute "true" = True
parseMute "false" = False
parseMute _ = False
getVol :: IO Int
getVol = parseVol <$> readProcess "pamixer" ["--get-volume"] ""
getVol :: String -> IO Int
getVol cmd = parseVol <$> readProcess cmd ["--get-volume"] ""
where
parseVol x = case readMaybe x of (Just vol) -> vol
Nothing -> 0
getInfo :: IO MixerData
getInfo = do
mute <- getMute
vol <- getVol
getInfo :: String -> IO MixerData
getInfo cmd = do
mute <- getMute cmd
vol <- getVol cmd
return $ MixerData { mute = mute, vol = vol }
queryVolume :: IO String
queryVolume = fmtData <$> getInfo
queryVolume :: String -> IO String
queryVolume cmd = fmtData <$> getInfo cmd