diff options
author | jao <jao@gnu.org> | 2022-03-30 02:37:00 +0100 |
---|---|---|
committer | jao <jao@gnu.org> | 2022-03-30 02:37:00 +0100 |
commit | 4f4e8de1d8b4d33e02057d085399d25e05d87a17 (patch) | |
tree | 1b0a7af4622052c8dba7d3a3f34e0a52a1f4acdf /src/Xmobar/Plugins/Monitors/Load.hs | |
parent | 28e51ce251bb7ecf8d688c0bdfb701c94b1fa7e2 (diff) | |
download | xmobar-4f4e8de1d8b4d33e02057d085399d25e05d87a17.tar.gz xmobar-4f4e8de1d8b4d33e02057d085399d25e05d87a17.tar.bz2 |
Load: new load average monitor
Closes #208
Diffstat (limited to 'src/Xmobar/Plugins/Monitors/Load.hs')
-rw-r--r-- | src/Xmobar/Plugins/Monitors/Load.hs | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/Xmobar/Plugins/Monitors/Load.hs b/src/Xmobar/Plugins/Monitors/Load.hs new file mode 100644 index 0000000..238d374 --- /dev/null +++ b/src/Xmobar/Plugins/Monitors/Load.hs @@ -0,0 +1,45 @@ +----------------------------------------------------------------------------- +-- | +-- 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) + +-- | 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 + parseTemplate =<< mapM (showWithColors $ showDigits 2) l) + else + return "Load: N/A" |