summaryrefslogtreecommitdiffhomepage
path: root/src/Xmobar/Plugins/Monitors/Load.hs
blob: 4703e8a418b47c0d02c89dae56577951f0fc88b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
-----------------------------------------------------------------------------
-- |
-- Module      :  Plugins.Monitors.Load
-- Copyright   :  Finn Lawler
-- License     :  BSD-style (see LICENSE)
--
-- Author      :  Finn Lawler <flawler@cs.tcd.ie>
-- Maintainer  :  jao <mail@jao.io>
-- Stability   :  unstable
-- Portability :  unportable
--
-- A load average monitor for Xmobar.  Adapted from
-- Xmobar.Plugins.Monitors.Thermal by Juraj Hercek.
--
-----------------------------------------------------------------------------

module Xmobar.Plugins.Monitors.Load (loadConfig, runLoad) where

import Xmobar.Plugins.Monitors.Common
import qualified Data.ByteString.Lazy.Char8 as B
import System.Posix.Files (fileExist)
import Control.Monad (zipWithM)

-- | Default configuration.
loadConfig :: IO MConfig
loadConfig = mkMConfig
       "Load: <load1>" -- template
       ["load1", "load5", "load15"]       -- available replacements

-- | Parses the contents of a loadavg proc file, returning
-- the list of load averages
parseLoadAvgs :: B.ByteString -> [Float]
parseLoadAvgs =
  map ((read :: String -> Float) . B.unpack) . take 3 . B.words . head . B.lines

-- | Retrieves load information.  Returns the monitor string parsed
-- according to template (either default or user specified).
runLoad :: [String] -> Monitor String
runLoad _ = do
  let file = "/proc/loadavg"
  exists <- io $ fileExist file
  if exists then
      (do l <- io $ B.readFile file >>= return . parseLoadAvgs
          d <- getConfigValue decDigits
          let s = showWithColors . const . showDigits d
          parseTemplate =<< zipWithM s l l)
    else
      return "Load: N/A"