diff options
author | Jose Antonio Ortega Ruiz <jao@gnu.org> | 2010-12-12 22:01:12 +0100 |
---|---|---|
committer | Jose Antonio Ortega Ruiz <jao@gnu.org> | 2010-12-12 22:01:12 +0100 |
commit | 52be04baa703cb5c0263aecb61f0079230367cd0 (patch) | |
tree | a1503d98651867fb2de3bdabaadc41a316757717 /Plugins/Uptime.hs | |
parent | dc4712688986edd9a3f272858b91b94badf2560d (diff) | |
download | xmobar-52be04baa703cb5c0263aecb61f0079230367cd0.tar.gz xmobar-52be04baa703cb5c0263aecb61f0079230367cd0.tar.bz2 |
New plugin: Uptime
Diffstat (limited to 'Plugins/Uptime.hs')
-rw-r--r-- | Plugins/Uptime.hs | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/Plugins/Uptime.hs b/Plugins/Uptime.hs new file mode 100644 index 0000000..6d81acd --- /dev/null +++ b/Plugins/Uptime.hs @@ -0,0 +1,51 @@ +------------------------------------------------------------------------------ +-- | +-- Module : Plugins.Uptime +-- Copyright : (c) 2010 Jose Antonio Ortega Ruiz +-- License : BSD3-style (see LICENSE) +-- +-- Maintainer : jao@gnu.org +-- Stability : unstable +-- Portability : unportable +-- Created: Sun Dec 12, 2010 20:26 +-- +-- +-- Uptime +-- +------------------------------------------------------------------------------ + + +module Plugins.Uptime (Uptime(..)) where + +import Plugins + +import qualified Data.ByteString.Lazy.Char8 as B + +data Uptime = Uptime String Int + deriving (Read, Show) + +instance Exec Uptime where + alias (Uptime a _) = a + run (Uptime _ _) = uptime + rate (Uptime _ r) = r + +readUptime :: IO Float +readUptime = + fmap (read . B.unpack . head . B.words) (B.readFile "/proc/uptime") + +secsPerDay :: Integer +secsPerDay = 24 * 3600 + +uptime :: IO String +uptime = do + t <- readUptime + let tsecs = floor t + secs = tsecs `mod` secsPerDay + days = tsecs `quot` secsPerDay + hrs = secs `quot` 3600 + mins = (secs `mod` 3600) `div` 60 + dstr | days == 0 = "" + | otherwise = show days ++ "d " + str x | x < 10 = '0':show x + | otherwise = show x + return $ dstr ++ str hrs ++ ":" ++ str mins |