blob: ed5c11ea16549b31fb692eb04dd8ed2276c57163 (
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
-----------------------------------------------------------------------------
-- |
-- Module : 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 Main where
import Monitors.Common
import qualified Data.ByteString.Lazy.Char8 as B
import Data.IORef
monitorConfig :: IO MConfig
monitorConfig =
do lc <- newIORef "#BFBFBF"
l <- newIORef 2
nc <- newIORef "#00FF00"
h <- newIORef 60
hc <- newIORef "#FF0000"
t <- newIORef "Cpu: <total>"
p <- newIORef package
u <- newIORef ""
a <- newIORef []
e <- newIORef ["total","user","nice","system","idle"]
return $ MC nc l lc h hc t p u a e
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
package :: String
package = "xmb-cpu"
runCpu :: [String] -> Monitor String
runCpu _ =
do c <- io $ parseCPU
l <- formatCpu c
parseTemplate l
main :: IO ()
main =
do let af = runCpu []
runMonitor monitorConfig af runCpu
|