From efd9bc7177c66d7bb9a994e919d915ecb5c75154 Mon Sep 17 00:00:00 2001 From: Jose Antonio Ortega Ruiz Date: Sun, 12 Dec 2010 23:51:14 +0100 Subject: Uptime as a Monitor --- Config.hs | 3 +-- NEWS | 4 ++-- Plugins/Monitors.hs | 4 ++++ Plugins/Monitors/Uptime.hs | 50 +++++++++++++++++++++++++++++++++++++++++++++ Plugins/Uptime.hs | 51 ---------------------------------------------- README | 15 ++++++++++---- 6 files changed, 68 insertions(+), 59 deletions(-) create mode 100644 Plugins/Monitors/Uptime.hs delete mode 100644 Plugins/Uptime.hs diff --git a/Config.hs b/Config.hs index 23bf997..6eb55a0 100644 --- a/Config.hs +++ b/Config.hs @@ -32,7 +32,6 @@ import Plugins.CommandReader import Plugins.StdinReader import Plugins.XMonadLog import Plugins.EWMH -import Plugins.Uptime #ifdef INOTIFY import Plugins.Mail @@ -109,7 +108,7 @@ infixr :*: -- the 'Runnable.Runnable' Read instance. To install a plugin just add -- the plugin's type to the list of types (separated by ':*:') appearing in -- this function's type signature. -runnableTypes :: Command :*: Monitors :*: Date :*: PipeReader :*: CommandReader :*: StdinReader :*: XMonadLog :*: EWMH :*: Uptime :*: +runnableTypes :: Command :*: Monitors :*: Date :*: PipeReader :*: CommandReader :*: StdinReader :*: XMonadLog :*: EWMH :*: #ifdef INOTIFY Mail :*: MBox :*: #endif diff --git a/NEWS b/NEWS index 936dd8a..44a2e0f 100644 --- a/NEWS +++ b/NEWS @@ -20,8 +20,8 @@ _New features_ - Window borders: configuration options `border` and `borderColor` allow drawing borders around xmobar's window. - - New plugin, `Uptime`, showing the system uptime. - - New Monitor option (`-P`) to enable displaying the `%` sign in + - New monitor, `Uptime`, showing the system uptime. + - New monitor argument (`-P`) to enable displaying the `%` sign in percentages; the sign is now never included by default. - New 'run once' commands, by specifying a 0 refresh rate in `Run Com` ([issue 26]). diff --git a/Plugins/Monitors.hs b/Plugins/Monitors.hs index 98949cf..a19f82a 100644 --- a/Plugins/Monitors.hs +++ b/Plugins/Monitors.hs @@ -31,6 +31,7 @@ import Plugins.Monitors.CpuFreq import Plugins.Monitors.CoreTemp import Plugins.Monitors.Disk import Plugins.Monitors.Top +import Plugins.Monitors.Uptime #ifdef IWLIB import Plugins.Monitors.Wireless #endif @@ -53,6 +54,7 @@ data Monitors = Weather Station Args Rate | CoreTemp Args Rate | TopProc Args Rate | TopMem Args Rate + | Uptime Args Rate #ifdef IWLIB | Wireless Interface Args Rate #endif @@ -86,6 +88,7 @@ instance Exec Monitors where alias (CoreTemp _ _) = "coretemp" alias (DiskU _ _ _) = "disku" alias (DiskIO _ _ _) = "diskio" + alias (Uptime _ _) = "uptime" #ifdef IWLIB alias (Wireless i _ _) = i ++ "wi" #endif @@ -106,6 +109,7 @@ instance Exec Monitors where start (DiskU s a r) = runM a diskUConfig (runDiskU s) r start (DiskIO s a r) = runM a diskIOConfig (runDiskIO s) r start (TopMem a r) = runM a topMemConfig runTopMem r + start (Uptime a r) = runM a uptimeConfig runUptime r start (TopProc a r) = startTop a r #ifdef IWLIB start (Wireless i a r) = runM (a ++ [i]) wirelessConfig runWireless r diff --git a/Plugins/Monitors/Uptime.hs b/Plugins/Monitors/Uptime.hs new file mode 100644 index 0000000..453b9ad --- /dev/null +++ b/Plugins/Monitors/Uptime.hs @@ -0,0 +1,50 @@ +------------------------------------------------------------------------------ +-- | +-- Module : Plugins.Monitors.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.Monitors.Uptime (uptimeConfig, runUptime) where + +import Plugins.Monitors.Common + +import qualified Data.ByteString.Lazy.Char8 as B + +uptimeConfig :: IO MConfig +uptimeConfig = mkMConfig "Up d h m" + ["days", "hours", "minutes", "seconds"] + +readUptime :: IO Float +readUptime = + fmap (read . B.unpack . head . B.words) (B.readFile "/proc/uptime") + +secsPerDay :: Integer +secsPerDay = 24 * 3600 + +uptime :: Monitor [String] +uptime = do + t <- io readUptime + u <- getConfigValue usePercent + let tsecs = floor t + secs = tsecs `mod` secsPerDay + days = tsecs `quot` secsPerDay + hours = secs `quot` 3600 + mins = (secs `mod` 3600) `div` 60 + ss = secs `mod` 60 + str x s = if u then show x ++ s else show x + mapM (`showWithColors'` days) + [str days "d", str hours "h", str mins "m", str ss "s"] + +runUptime :: [String] -> Monitor String +runUptime _ = uptime >>= parseTemplate diff --git a/Plugins/Uptime.hs b/Plugins/Uptime.hs deleted file mode 100644 index 6d81acd..0000000 --- a/Plugins/Uptime.hs +++ /dev/null @@ -1,51 +0,0 @@ ------------------------------------------------------------------------------- --- | --- 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 diff --git a/README b/README index 8de3050..a8e7e0c 100644 --- a/README +++ b/README @@ -314,6 +314,17 @@ installed by default. Each monitor has an `alias` to be used in the output template. Monitors have default aliases. +`Uptime Args RefreshRate` + +- Aliases to `uptime` +- Args: default monitor arguments (see below). The low and high + thresholds refer to the number of days. +- Variables that can be used with the `-t`/`--template` argument: + `days`, `hours`, `minutes`, `seconds`. The total uptime is the + sum of all those fields. You can set the `-P` argument to "True" + to add units to the display of those numeric fields. +- Default template: `Up: d h m` + `Weather StationID Args RefreshRate` - Aliases to the Station ID: so `Weather "LIPB" []` can be used in template as `%LIPB%` @@ -660,10 +671,6 @@ can be used in the output template as `%mydate%` - Format is a time format string, as accepted by the standard ISO C `strftime` function (or Haskell's `formatCalendarTime`). -`Uptime Alias RefreshRate` - -- Shows system uptime. - `CommandReader "/path/to/program" Alias` - Runs the given program, and displays its standard output. -- cgit v1.2.3