readStat :: Cpu -> IO CpuStat
readStat cpu= do
  hdl  <- openFile "/proc/stat" ReadMode
  stats <- readCpuStats hdl []
  hClose hdl
  return $ case cpu of
    CpuAll -> head stats
    CpuCore x  -> stats !! x
  where
    readCpuStats hdl stats = do
      (name, stat) <- parse <$> hGetLine hdl
      if name == "intr"
        then return stats
        else readCpuStats hdl (stats ++ [stat])

    parse = (head . words) &&& (mkCpuStat . map read . tail . words)

    mkCpuStat (u:n:s:i:io:irq:sirq:st:_) = CpuStat u n s i io irq sirq st
    mkCpuStat _                          = CpuStat 0 0 0 0 0  0   0    0
readStat :: Cpu -> IO CpuStat
readStat cpu= do
  hdl  <- openFile "/proc/stat" ReadMode
  stats <- readCpuStats hdl []
  hClose hdl
  return $ case cpu of
    CpuAll -> head stats
    CpuCore x  -> stats !! x
  where
    readCpuStats hdl stats = do
      (name, stat) <- parse <$> hGetLine hdl
      if name == "intr"
        then return stats
        else readCpuStats hdl (stats ++ [stat])

    parse = (head . words) &&& (mkCpuStat . map read . tail . words)

    mkCpuStat (u:n:s:i:io:irq:sirq:st:_) = CpuStat u n s i io irq sirq st
    mkCpuStat _                          = CpuStat 0 0 0 0 0  0   0    0
readStat cpu= do
  hdl  <- openFile "/proc/stat" ReadMode
  stats <- readCpuStats hdl []
  hClose hdl
  return $ case cpu of
    CpuAll -> head stats
    CpuCore x  -> stats !! x
  where
    readCpuStats hdl stats = do
      (name, stat) <- parse <$> hGetLine hdl
      if name == "intr"
        then return stats
        else readCpuStats hdl (stats ++ [stat])

    parse = (head . words) &&& (mkCpuStat . map read . tail . words)

    mkCpuStat (u:n:s:i:io:irq:sirq:st:_) = CpuStat u n s i io irq sirq st
    mkCpuStat _                          = CpuStat 0 0 0 0 0  0   0    0