From fa5443124c7aded9698e7683eca1e5c31a550ed4 Mon Sep 17 00:00:00 2001
From: Jose Antonio Ortega Ruiz <jao@gnu.org>
Date: Sun, 18 Dec 2011 03:17:38 +0100
Subject: New -d (decimal digits) monitor option (issue #58)

---
 src/Plugins/Monitors/Batt.hs   |  9 +++++----
 src/Plugins/Monitors/Common.hs | 44 +++++++++++++++++++++++-------------------
 src/Plugins/Monitors/Net.hs    |  3 ++-
 src/Plugins/Monitors/Swap.hs   |  9 +++++----
 src/Plugins/Monitors/Top.hs    |  3 ++-
 src/Plugins/Monitors/Volume.hs |  3 ++-
 6 files changed, 40 insertions(+), 31 deletions(-)

(limited to 'src/Plugins')

diff --git a/src/Plugins/Monitors/Batt.hs b/src/Plugins/Monitors/Batt.hs
index 24f5263..4445e7a 100644
--- a/src/Plugins/Monitors/Batt.hs
+++ b/src/Plugins/Monitors/Batt.hs
@@ -151,17 +151,18 @@ runBatt' bfs args = do
   opts <- io $ parseOpts args
   c <- io $ readBatteries opts =<< mapM batteryFiles bfs
   suffix <- getConfigValue useSuffix
+  d <- getConfigValue decDigits
   case c of
     Result x w t s ->
       do l <- fmtPercent x
-         parseTemplate (l ++ s:[fmtTime $ floor t, fmtWatts w opts suffix])
+         parseTemplate (l ++ s:[fmtTime $ floor t, fmtWatts w opts suffix d])
     NA -> return "N/A"
   where fmtPercent :: Float -> Monitor [String]
         fmtPercent x = do
           p <- showPercentWithColors x
           b <- showPercentBar (100 * x) x
           return [b, p]
-        fmtWatts x o s = color x o $ showDigits 1 x ++ (if s then "W" else "")
+        fmtWatts x o s d = color x o $ showDigits d x ++ (if s then "W" else "")
         fmtTime :: Integer -> String
         fmtTime x = hours ++ ":" ++ if length minutes == 2
                                     then minutes else '0' : minutes
@@ -170,6 +171,6 @@ runBatt' bfs args = do
         maybeColor Nothing str = str
         maybeColor (Just c) str = "<fc=" ++ c ++ ">" ++ str ++ "</fc>"
         color x o | x >= 0 = maybeColor (posColor o)
-                  | x >= highThreshold o = maybeColor (highWColor o)
-                  | x >= lowThreshold o = maybeColor (mediumWColor o)
+                  | -x >= highThreshold o = maybeColor (highWColor o)
+                  | -x >= lowThreshold o = maybeColor (mediumWColor o)
                   | otherwise = maybeColor (lowWColor o)
diff --git a/src/Plugins/Monitors/Common.hs b/src/Plugins/Monitors/Common.hs
index 02eb6e1..c9f0843 100644
--- a/src/Plugins/Monitors/Common.hs
+++ b/src/Plugins/Monitors/Common.hs
@@ -78,6 +78,7 @@ data MConfig =
        , template    :: IORef String
        , export      :: IORef [String]
        , ppad        :: IORef Int
+       , decDigits   :: IORef Int
        , minWidth    :: IORef Int
        , maxWidth    :: IORef Int
        , padChars    :: IORef String
@@ -120,6 +121,7 @@ mkMConfig tmpl exprts =
        t  <- newIORef tmpl
        e  <- newIORef exprts
        p  <- newIORef 0
+       d  <- newIORef 0
        mn <- newIORef 0
        mx <- newIORef 0
        pc <- newIORef " "
@@ -128,7 +130,7 @@ mkMConfig tmpl exprts =
        bf <- newIORef "#"
        bw <- newIORef 10
        up <- newIORef False
-       return $ MC nc l lc h hc t e p mn mx pc pr bb bf bw up
+       return $ MC nc l lc h hc t e p d mn mx pc pr bb bf bw up
 
 data Opts = HighColor String
           | NormalColor String
@@ -137,6 +139,7 @@ data Opts = HighColor String
           | High String
           | Template String
           | PercentPad String
+          | DecDigits String
           | MinWidth String
           | MaxWidth String
           | Width String
@@ -149,27 +152,27 @@ data Opts = HighColor String
 
 options :: [OptDescr Opts]
 options =
-    [ Option "H"  ["High"]     (ReqArg High "number"               )  "The high threshold"
-    , Option "L"  ["Low"]      (ReqArg Low "number"                )  "The low threshold"
-    , Option "h"  ["high"]     (ReqArg HighColor "color number"    )  "Color for the high threshold: ex \"#FF0000\""
-    , Option "n"  ["normal"]   (ReqArg NormalColor "color number"  )  "Color for the normal threshold: ex \"#00FF00\""
-    , Option "l"  ["low"]      (ReqArg LowColor "color number"     )  "Color for the low threshold: ex \"#0000FF\""
-    , Option "t"  ["template"] (ReqArg Template "output template"  )  "Output template."
-    , Option "S"  ["suffix"]   (ReqArg UseSuffix "True/False"      )  "Use % to display percents or other suffixes."
-    , Option "p"  ["ppad"]     (ReqArg PercentPad "percent padding")  "Minimum percentage width."
-    , Option "m"  ["minwidth"] (ReqArg MinWidth "minimum width"    )  "Minimum field width"
-    , Option "M"  ["maxwidth"] (ReqArg MaxWidth "maximum width"    )  "Maximum field width"
-    , Option "w"  ["width"]    (ReqArg Width "fixed width"         )  "Fixed field width"
-    , Option "c"  ["padchars"] (ReqArg PadChars "padding chars"    )  "Characters to use for padding"
-    , Option "a"  ["align"]    (ReqArg PadAlign "padding alignment")  "'l' for left padding, 'r' for right"
-    , Option "b"  ["bback"]    (ReqArg BarBack "bar background"    )  "Characters used to draw bar backgrounds"
-    , Option "f"  ["bfore"]    (ReqArg BarFore "bar foreground"    )  "Characters used to draw bar foregrounds"
-    , Option "W"  ["bwidth"]   (ReqArg BarWidth "bar width"        )  "Bar width"
+    [
+      Option "H"  ["High"] (ReqArg High "number") "The high threshold"
+    , Option "L"  ["Low"] (ReqArg Low "number") "The low threshold"
+    , Option "h"  ["high"] (ReqArg HighColor "color number") "Color for the high threshold: ex \"#FF0000\""
+    , Option "n"  ["normal"] (ReqArg NormalColor "color number") "Color for the normal threshold: ex \"#00FF00\""
+    , Option "l"  ["low"] (ReqArg LowColor "color number") "Color for the low threshold: ex \"#0000FF\""
+    , Option "t"  ["template"] (ReqArg Template "output template") "Output template."
+    , Option "S"  ["suffix"] (ReqArg UseSuffix "True/False") "Use % to display percents or other suffixes."
+    , Option "d"  ["ddigits"] (ReqArg DecDigits "decimal digits") "Number of decimal digits to display."
+    , Option "p"  ["ppad"] (ReqArg PercentPad "percent padding") "Minimum percentage width."
+    , Option "m"  ["minwidth"] (ReqArg MinWidth "minimum width") "Minimum field width"
+    , Option "M"  ["maxwidth"] (ReqArg MaxWidth "maximum width") "Maximum field width"
+    , Option "w"  ["width"] (ReqArg Width "fixed width") "Fixed field width"
+    , Option "c"  ["padchars"] (ReqArg PadChars "padding chars") "Characters to use for padding"
+    , Option "a"  ["align"] (ReqArg PadAlign "padding alignment") "'l' for left padding, 'r' for right"
+    , Option "b"  ["bback"] (ReqArg BarBack "bar background") "Characters used to draw bar backgrounds"
+    , Option "f"  ["bfore"] (ReqArg BarFore "bar foreground") "Characters used to draw bar foregrounds"
+    , Option "W"  ["bwidth"] (ReqArg BarWidth "bar width") "Bar width"
     ]
 
-doArgs :: [String]
-       -> ([String] -> Monitor String)
-       -> Monitor String
+doArgs :: [String] -> ([String] -> Monitor String) -> Monitor String
 doArgs args action =
     case getOpt Permute options args of
       (o, n, [])   -> do doConfigOptions o
@@ -190,6 +193,7 @@ doConfigOptions (o:oo) =
           LowColor    c -> setConfigValue (Just c) lowColor
           Template    t -> setConfigValue t template
           PercentPad  p -> setConfigValue (nz p) ppad
+          DecDigits   d -> setConfigValue (nz d) decDigits
           MinWidth    w -> setConfigValue (nz w) minWidth
           MaxWidth    w -> setConfigValue (nz w) maxWidth
           Width       w -> setConfigValue (nz w) minWidth >>
diff --git a/src/Plugins/Monitors/Net.hs b/src/Plugins/Monitors/Net.hs
index 8382542..1513e07 100644
--- a/src/Plugins/Monitors/Net.hs
+++ b/src/Plugins/Monitors/Net.hs
@@ -74,7 +74,8 @@ netParser =
 formatNet :: Float -> Monitor (String, String)
 formatNet d = do
     s <- getConfigValue useSuffix
-    let str = if s then (++"Kb/s") . showDigits 1 else showDigits 1
+    dd <- getConfigValue decDigits
+    let str = if s then (++"Kb/s") . showDigits dd else showDigits dd
     b <- showLogBar 0.9 d
     x <- showWithColors str d
     return (x, b)
diff --git a/src/Plugins/Monitors/Swap.hs b/src/Plugins/Monitors/Swap.hs
index e466dbb..107eb1e 100644
--- a/src/Plugins/Monitors/Swap.hs
+++ b/src/Plugins/Monitors/Swap.hs
@@ -42,10 +42,11 @@ parseMEM =
        return [(tot - free) / tot, tot, tot - free, free]
 
 formatSwap :: [Float] -> Monitor [String]
-formatSwap (r:xs) =
-     do other <- mapM (showWithColors (showDigits 2)) xs
-        ratio <- showPercentWithColors r
-        return $ ratio:other
+formatSwap (r:xs) = do
+  d <- getConfigValue decDigits
+  other <- mapM (showWithColors (showDigits d)) xs
+  ratio <- showPercentWithColors r
+  return $ ratio:other
 formatSwap _ = return $ replicate 4 "N/A"
 
 runSwap :: [String] -> Monitor String
diff --git a/src/Plugins/Monitors/Top.hs b/src/Plugins/Monitors/Top.hs
index 6001164..6f16bdb 100644
--- a/src/Plugins/Monitors/Top.hs
+++ b/src/Plugins/Monitors/Top.hs
@@ -158,7 +158,8 @@ topProcesses tref scale = do
   return (len, nts', mis')
 
 showTimeInfo :: TimeInfo -> Monitor [String]
-showTimeInfo (n, t) = showInfo n (showDigits 0 t) t
+showTimeInfo (n, t) =
+  getConfigValue decDigits >>= \d -> showInfo n (showDigits d t) t
 
 showTimeInfos :: [TimeInfo] -> Monitor [[String]]
 showTimeInfos = mapM showTimeInfo
diff --git a/src/Plugins/Monitors/Volume.hs b/src/Plugins/Monitors/Volume.hs
index 3e3a8b9..8eef780 100644
--- a/src/Plugins/Monitors/Volume.hs
+++ b/src/Plugins/Monitors/Volume.hs
@@ -97,8 +97,9 @@ formatDb opts dbi = do
     h <- getConfigValue highColor
     m <- getConfigValue normalColor
     l <- getConfigValue lowColor
+    d <- getConfigValue decDigits
     let db = fromIntegral dbi / 100.0
-        digits = showDigits 0 db
+        digits = showDigits d db
         startColor | db >= highDbThresh opts = colorHelper h
                    | db < lowDbThresh opts = colorHelper l
                    | otherwise = colorHelper m
-- 
cgit v1.2.3