summaryrefslogtreecommitdiffhomepage
path: root/Plugins/Monitors/Cpu.hs
diff options
context:
space:
mode:
authorAndrea Rossato <andrea.rossato@ing.unitn.it>2007-07-18 17:12:11 +0200
committerAndrea Rossato <andrea.rossato@ing.unitn.it>2007-07-18 17:12:11 +0200
commita8653d8712c2d218adf3f70cef7e511060bed695 (patch)
treeb463eaa897d30c41163d0e5fbee89aa946980e7f /Plugins/Monitors/Cpu.hs
parent7235e59441c94580e99d50774629579fe54c6b1a (diff)
downloadxmobar-a8653d8712c2d218adf3f70cef7e511060bed695.tar.gz
xmobar-a8653d8712c2d218adf3f70cef7e511060bed695.tar.bz2
Monitors are now a Plugin that can be removed from Config.hs
darcs-hash:20070718151211-d6583-7e0e49c22d07feda72d03370fd592c196dfcc9c1.gz
Diffstat (limited to 'Plugins/Monitors/Cpu.hs')
-rw-r--r--Plugins/Monitors/Cpu.hs53
1 files changed, 53 insertions, 0 deletions
diff --git a/Plugins/Monitors/Cpu.hs b/Plugins/Monitors/Cpu.hs
new file mode 100644
index 0000000..9ab6d8f
--- /dev/null
+++ b/Plugins/Monitors/Cpu.hs
@@ -0,0 +1,53 @@
+-----------------------------------------------------------------------------
+-- |
+-- Module : Plugins.Monitors.Cpu
+-- Copyright : (c) Andrea Rossato
+-- License : BSD-style (see LICENSE)
+--
+-- Maintainer : Andrea Rossato <andrea.rossato@unibz.it>
+-- Stability : unstable
+-- Portability : unportable
+--
+-- A cpu monitor for Xmobar
+--
+-----------------------------------------------------------------------------
+
+module Plugins.Monitors.Cpu where
+
+import Plugins.Monitors.Common
+import qualified Data.ByteString.Lazy.Char8 as B
+
+cpuConfig :: IO MConfig
+cpuConfig = mkMConfig
+ "Cpu: <total>" -- template
+ ["total","user","nice","system","idle"] -- available replacements
+
+cpuData :: IO [Float]
+cpuData = do s <- B.readFile "/proc/stat"
+ return $ cpuParser s
+
+cpuParser :: B.ByteString -> [Float]
+cpuParser =
+ map read . map B.unpack . tail . B.words . flip (!!) 0 . B.lines
+
+parseCPU :: IO [Float]
+parseCPU =
+ do (a,b) <- doActionTwiceWithDelay 750000 cpuData
+ let dif = zipWith (-) b a
+ tot = foldr (+) 0 dif
+ percent = map (/ tot) dif
+ return percent
+
+formatCpu :: [Float] -> Monitor [String]
+formatCpu [] = return [""]
+formatCpu x =
+ do let f s = floatToPercent (s / 100)
+ t = foldr (+) 0 $ take 3 x
+ list = t:x
+ mapM (showWithColors f) . map (* 100) $ list
+
+runCpu :: [String] -> Monitor String
+runCpu _ =
+ do c <- io $ parseCPU
+ l <- formatCpu c
+ parseTemplate l