-----------------------------------------------------------------------------
-- |
-- 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 [[ByteString]] -> [[ByteString]] -> Bool
forall a. Eq a => a -> a -> Bool
/= [] = [[ByteString]] -> [ByteString]
forall a. [a] -> a
head [[ByteString]]
l [ByteString] -> Int -> ByteString
forall a. [a] -> Int -> a
!! Int
i
               | Bool
otherwise = ByteString
B.empty
           fs :: FilePath -> [ByteString] -> Bool
fs FilePath
s [ByteString]
l
               | [ByteString] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [ByteString]
l    = Bool
False
               | Bool
otherwise = [ByteString] -> ByteString
forall a. [a] -> a
head [ByteString]
l ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== FilePath -> ByteString
B.pack FilePath
s
           get_data :: FilePath -> [[ByteString]] -> c
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]]
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 :: Float
tot  = FilePath -> [[ByteString]] -> Float
forall c. (Fractional c, Read c) => FilePath -> [[ByteString]] -> c
get_data FilePath
"SwapTotal:" [[ByteString]]
st
           free :: Float
free = FilePath -> [[ByteString]] -> Float
forall c. (Fractional c, Read c) => FilePath -> [[ByteString]] -> c
get_data FilePath
"SwapFree:" [[ByteString]]
st
       [Float] -> IO [Float]
forall (m :: * -> *) a. Monad m => a -> m a
return [(Float
tot Float -> Float -> Float
forall a. Num a => a -> a -> a
- Float
free) Float -> Float -> Float
forall a. Fractional a => a -> a -> a
/ Float
tot, Float
tot, Float
tot Float -> Float -> Float
forall a. Num a => a -> a -> a
- Float
free, Float
free]