summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAxel Angel <axel+git@vneko.ch>2014-07-30 17:07:15 +0200
committerAxel Angel <axel+git@vneko.ch>2014-07-30 17:18:06 +0200
commitd0dedc89e23a2e5b3d7217ad60d6659ea2ae4e11 (patch)
tree87be50b0e6da031aba51140079f451a9534abb04
parent85fae7ddd5652c72d99b2b3a1e5e10a48147c3ea (diff)
downloadxmobar-d0dedc89e23a2e5b3d7217ad60d6659ea2ae4e11.tar.gz
xmobar-d0dedc89e23a2e5b3d7217ad60d6659ea2ae4e11.tar.bz2
Implement adaptive units for Net plugin
Adaptive units are computed only if suffix is enabled Otherwise we fall back to the old computation (KB)
-rw-r--r--src/Plugins/Monitors/Net.hs24
1 files changed, 21 insertions, 3 deletions
diff --git a/src/Plugins/Monitors/Net.hs b/src/Plugins/Monitors/Net.hs
index e49d1aa..31dc411 100644
--- a/src/Plugins/Monitors/Net.hs
+++ b/src/Plugins/Monitors/Net.hs
@@ -28,6 +28,15 @@ import System.FilePath ((</>))
import qualified Data.ByteString.Lazy.Char8 as B
+data UnitPerSec = Bs | KBs | MBs | GBs deriving (Eq,Enum,Ord)
+data NetValue = NetValue Float UnitPerSec deriving (Eq,Show)
+
+instance Show UnitPerSec where
+ show Bs = "B/s"
+ show KBs = "KB/s"
+ show MBs = "MB/s"
+ show GBs = "GB/s"
+
data NetDev = NA
| NI String
| ND String Float Float deriving (Eq,Show,Read)
@@ -74,7 +83,7 @@ readNetDev (d:x:y:_) = do
up <- isUp d
return (if up then ND d (r x) (r y) else NI d)
where r s | s == "" = 0
- | otherwise = read s / 1024
+ | otherwise = read s
readNetDev _ = return NA
@@ -101,10 +110,12 @@ formatNet :: Float -> Monitor (String, String, String)
formatNet d = do
s <- getConfigValue useSuffix
dd <- getConfigValue decDigits
- let str = if s then (++"Kb/s") . showDigits dd else showDigits dd
+ let str True v = showDigits dd d' ++ show u
+ where (NetValue d' u) = byteNetVal v
+ str False v = showDigits dd $ v / 1024
b <- showLogBar 0.9 d
vb <- showLogVBar 0.9 d
- x <- showWithColors str d
+ x <- showWithColors (str s) d
return (x, b, vb)
printNet :: NetDev -> Monitor String
@@ -159,3 +170,10 @@ startDynNet a r cb = do
_ <- parseNet nref d
return (nref, d)
runM a netConfig (runNets refs) r cb
+
+byteNetVal :: Float -> NetValue
+byteNetVal v
+ | v < 1024**1 = NetValue v Bs
+ | v < 1024**2 = NetValue (v/1024**1) KBs
+ | v < 1024**3 = NetValue (v/1024**2) MBs
+ | otherwise = NetValue (v/1024**3) GBs