module Xmobar.Plugins.Monitors.Swap.Linux (parseMEM) where
import qualified Data.ByteString.Lazy.Char8 as B
fileMEM :: IO B.ByteString
fileMEM :: IO ByteString
fileMEM = FilePath -> IO ByteString
B.readFile FilePath
"/proc/meminfo"
parseMEM :: IO [Float]
parseMEM :: IO [Float]
parseMEM =
do file <- IO ByteString
fileMEM
let li Int
i [[ByteString]]
l
| [[ByteString]]
l [[ByteString]] -> [[ByteString]] -> Bool
forall a. Eq a => a -> a -> Bool
/= [] = [[ByteString]] -> [ByteString]
forall a. HasCallStack => [a] -> a
head [[ByteString]]
l [ByteString] -> Int -> ByteString
forall a. HasCallStack => [a] -> Int -> a
!! Int
i
| Bool
otherwise = ByteString
B.empty
fs FilePath
s [ByteString]
l
| [ByteString] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [ByteString]
l = Bool
False
| Bool
otherwise = [ByteString] -> ByteString
forall a. HasCallStack => [a] -> a
head [ByteString]
l ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== FilePath -> ByteString
B.pack FilePath
s
get_data FilePath
s = (c -> c -> c) -> c -> c -> c
forall a b c. (a -> b -> c) -> b -> a -> c
flip c -> c -> c
forall a. Fractional a => a -> a -> a
(/) c
1024 (c -> c) -> ([[ByteString]] -> c) -> [[ByteString]] -> c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> c
forall a. Read a => FilePath -> a
read (FilePath -> c)
-> ([[ByteString]] -> FilePath) -> [[ByteString]] -> c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> FilePath
B.unpack (ByteString -> FilePath)
-> ([[ByteString]] -> ByteString) -> [[ByteString]] -> FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [[ByteString]] -> ByteString
li Int
1 ([[ByteString]] -> ByteString)
-> ([[ByteString]] -> [[ByteString]])
-> [[ByteString]]
-> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([ByteString] -> Bool) -> [[ByteString]] -> [[ByteString]]
forall a. (a -> Bool) -> [a] -> [a]
filter (FilePath -> [ByteString] -> Bool
fs FilePath
s)
st = (ByteString -> [ByteString]) -> [ByteString] -> [[ByteString]]
forall a b. (a -> b) -> [a] -> [b]
map ByteString -> [ByteString]
B.words ([ByteString] -> [[ByteString]])
-> (ByteString -> [ByteString]) -> ByteString -> [[ByteString]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> [ByteString]
B.lines (ByteString -> [[ByteString]]) -> ByteString -> [[ByteString]]
forall a b. (a -> b) -> a -> b
$ ByteString
file
tot = FilePath -> [[ByteString]] -> Float
forall {c}.
(Fractional c, Read c) =>
FilePath -> [[ByteString]] -> c
get_data FilePath
"SwapTotal:" [[ByteString]]
st
free = FilePath -> [[ByteString]] -> Float
forall {c}.
(Fractional c, Read c) =>
FilePath -> [[ByteString]] -> c
get_data FilePath
"SwapFree:" [[ByteString]]
st
return [(tot - free) / tot, tot, tot - free, free]