do not split when colons are escaped

Ricard Illa 2022-05-22 14:09:44 +02:00
parent 24a52ffb02
commit e9bdde5614
No known key found for this signature in database
GPG Key ID: F69A672B72E54902
1 changed files with 8 additions and 5 deletions

View File

@ -1,6 +1,5 @@
module Monitors.Net (queryNet) where module Monitors.Net (queryNet) where
import Data.List.Split
import Data.List import Data.List
import Data.Maybe import Data.Maybe
import System.Process import System.Process
@ -41,7 +40,7 @@ getDevStatus cmd = readProcess cmd args "" >>= mapM (buildDeviceData cmd) . line
buildDeviceData :: String -> String -> IO DeviceData buildDeviceData :: String -> String -> IO DeviceData
buildDeviceData cmd x = buildDeviceData cmd x =
let let
[d,t,state] = splitOn ":" x [d,t,state] = splitFields x
deviceType = readDeviceType t deviceType = readDeviceType t
connected = state == "connected" connected = state == "connected"
in do in do
@ -58,6 +57,11 @@ readDeviceType "wifi" = Wifi
readDeviceType "ethernet" = Ethernet readDeviceType "ethernet" = Ethernet
readDeviceType _ = OtherDevice readDeviceType _ = OtherDevice
splitFields :: String -> [String]
splitFields = splitRegex (mkRegex "[^\\]:")
getWifiSignal :: String -> DeviceType -> String -> IO (Maybe Int) getWifiSignal :: String -> DeviceType -> String -> IO (Maybe Int)
getWifiSignal cmd Wifi dev = parseStdout <$> readProcess cmd args "" getWifiSignal cmd Wifi dev = parseStdout <$> readProcess cmd args ""
where where
@ -67,9 +71,8 @@ getWifiSignal cmd Wifi dev = parseStdout <$> readProcess cmd args ""
, "device", "wifi", "list" , "device", "wifi", "list"
, "ifname", dev , "ifname", dev
] ]
splt = splitOn ":" isActive = (=="*") . head . splitFields
isActive = (=="*") . head . splt readVal = read . (!! 1) . splitFields
readVal = read . (!! 1) . splt
parseStdout = fmap readVal . find isActive . lines parseStdout = fmap readVal . find isActive . lines
getWifiSignal _ _ _ = return Nothing getWifiSignal _ _ _ = return Nothing