-----------------------------------------------------------------------------
-- |
-- Module      :  Plugins.Monitors.Swap.Linux
-- Copyright   :  (c) Andrea Rossato
-- License     :  BSD-style (see LICENSE)
--
-- Maintainer  :  Jose A. Ortega Ruiz <jao@gnu.org>
-- Stability   :  unstable
-- Portability :  unportable
--
-- A  swap usage monitor for Xmobar
--
-----------------------------------------------------------------------------

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 ByteString
file <- IO ByteString
fileMEM
       let li :: Int -> [[ByteString]] -> ByteString
li Int
i [[ByteString]]
l
               | [[ByteString]]
l forall a. Eq a => a -> a -> Bool
/= [] = forall a. [a] -> a
head [[ByteString]]
l forall a. [a] -> Int -> a
!! Int
i
               | Bool
otherwise = ByteString
B.empty
           fs :: FilePath -> [ByteString] -> Bool
fs FilePath
s [ByteString]
l
               | forall (t :: * -> *) a. Foldable t => t a -> Bool
null [ByteString]
l    = Bool
False
               | Bool
otherwise = forall a. [a] -> a
head [ByteString]
l forall a. Eq a => a -> a -> Bool
== FilePath -> ByteString
B.pack FilePath
s
           get_data :: FilePath -> [[ByteString]] -> c
get_data FilePath
s = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall a. Fractional a => a -> a -> a
(/) c
1024 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Read a => FilePath -> a
read forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> FilePath
B.unpack forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [[ByteString]] -> ByteString
li Int
1 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Bool) -> [a] -> [a]
filter (FilePath -> [ByteString] -> Bool
fs FilePath
s)
           st :: [[ByteString]]
st   = forall a b. (a -> b) -> [a] -> [b]
map ByteString -> [ByteString]
B.words forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> [ByteString]
B.lines forall a b. (a -> b) -> a -> b
$ ByteString
file
           tot :: Float
tot  = forall {c}.
(Fractional c, Read c) =>
FilePath -> [[ByteString]] -> c
get_data FilePath
"SwapTotal:" [[ByteString]]
st
           free :: Float
free = forall {c}.
(Fractional c, Read c) =>
FilePath -> [[ByteString]] -> c
get_data FilePath
"SwapFree:" [[ByteString]]
st
       forall (m :: * -> *) a. Monad m => a -> m a
return [(Float
tot forall a. Num a => a -> a -> a
- Float
free) forall a. Fractional a => a -> a -> a
/ Float
tot, Float
tot, Float
tot forall a. Num a => a -> a -> a
- Float
free, Float
free]