From 37e8aa3554f77d95300838349e262598212d16bf Mon Sep 17 00:00:00 2001 From: Ricard Illa Date: Wed, 4 Aug 2021 20:56:11 +0200 Subject: [PATCH] take command path as argument --- app/Main.hs | 8 ++++---- src/Monitors/Battery.hs | 8 ++++---- src/Monitors/Net.hs | 32 ++++++++++++++++---------------- src/Monitors/Volume.hs | 20 ++++++++++---------- 4 files changed, 34 insertions(+), 34 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index 55ac3c3..f5c0a0d 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -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 diff --git a/src/Monitors/Battery.hs b/src/Monitors/Battery.hs index d6c57f9..1fb423e 100644 --- a/src/Monitors/Battery.hs +++ b/src/Monitors/Battery.hs @@ -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 diff --git a/src/Monitors/Net.hs b/src/Monitors/Net.hs index 8932f9a..9297cac 100644 --- a/src/Monitors/Net.hs +++ b/src/Monitors/Net.hs @@ -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 diff --git a/src/Monitors/Volume.hs b/src/Monitors/Volume.hs index e992d3b..1fe5175 100644 --- a/src/Monitors/Volume.hs +++ b/src/Monitors/Volume.hs @@ -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