module System.Taffybar.Information.DiskIO ( getDiskTransfer ) where
import Data.Maybe ( mapMaybe )
import Safe ( atMay, headMay, readDef )
import System.Taffybar.Information.StreamInfo ( getParsedInfo, getTransfer )
getDiskTransfer :: String -> IO [Double]
getDiskTransfer :: String -> IO [Double]
getDiskTransfer String
disk = Double -> IO [Int] -> IO [Double]
forall a b. (Integral a, RealFloat b) => b -> IO [a] -> IO [b]
getTransfer Double
0.05 (IO [Int] -> IO [Double]) -> IO [Int] -> IO [Double]
forall a b. (a -> b) -> a -> b
$ String -> IO [Int]
getDiskInfo String
disk
getDiskInfo :: String -> IO [Int]
getDiskInfo :: String -> IO [Int]
getDiskInfo = String -> (String -> [(String, [Int])]) -> String -> IO [Int]
forall a. String -> (String -> [(String, [a])]) -> String -> IO [a]
getParsedInfo String
"/proc/diskstats" String -> [(String, [Int])]
parse
parse :: String -> [(String, [Int])]
parse :: String -> [(String, [Int])]
parse = (String -> Maybe (String, [Int])) -> [String] -> [(String, [Int])]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe ([String] -> Maybe (String, [Int])
tuplize ([String] -> Maybe (String, [Int]))
-> (String -> [String]) -> String -> Maybe (String, [Int])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> [String] -> [String]
forall a. Int -> [a] -> [a]
drop Int
2 ([String] -> [String])
-> (String -> [String]) -> String -> [String]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [String]
words) ([String] -> [(String, [Int])])
-> (String -> [String]) -> String -> [(String, [Int])]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> [String]
lines
tuplize :: [String] -> Maybe (String, [Int])
tuplize :: [String] -> Maybe (String, [Int])
tuplize [String]
s = do
String
device <- [String] -> Maybe String
forall a. [a] -> Maybe a
headMay [String]
s
String
used <- [String]
s [String] -> Int -> Maybe String
forall a. [a] -> Int -> Maybe a
`atMay` Int
3
String
capacity <- [String]
s [String] -> Int -> Maybe String
forall a. [a] -> Int -> Maybe a
`atMay` Int
7
(String, [Int]) -> Maybe (String, [Int])
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return (String
device, [Int -> String -> Int
forall a. Read a => a -> String -> a
readDef (-Int
1) String
used, Int -> String -> Int
forall a. Read a => a -> String -> a
readDef (-Int
1) String
capacity])