From 727478f5b8916d8d98ae4208d4f6a80abb4fafc7 Mon Sep 17 00:00:00 2001 From: jao Date: Thu, 14 Apr 2022 16:41:36 +0100 Subject: Memory: new argument to scale usage units Fixes #624 --- changelog.md | 1 + doc/plugins.org | 14 ++++++++++++++ src/Xmobar/Plugins/Monitors/Mem.hs | 24 ++++++++++++------------ src/Xmobar/Plugins/Monitors/Mem/Linux.hs | 9 ++++++--- 4 files changed, 33 insertions(+), 15 deletions(-) diff --git a/changelog.md b/changelog.md index 102fedb..ebf7941 100644 --- a/changelog.md +++ b/changelog.md @@ -4,6 +4,7 @@ _New features_ - New monitor `Load` providing load averages (stolen from Finn Lawler, with FreeBSD support thanks to MichaƂ Zielonka). + - New argument `scale` for `Memory` monitor to scale size units. _Bug fixes_ diff --git a/doc/plugins.org b/doc/plugins.org index 8ff75e4..383207c 100644 --- a/doc/plugins.org +++ b/doc/plugins.org @@ -510,6 +510,10 @@ =freeipat=. - =--available-icon-pattern=: dynamic string for available memory ratio in =availableipat=. + - =--scale=: sizes (total, free, etc.) are reported in units of + ~Mb/scale~, with scale defaulting to 1.0. So, for + instance, to get sizes reported in Gb, set this parameter + to 1024. - Thresholds refer to percentage of used memory - Variables that can be used with the =-t/--template= argument: @@ -517,8 +521,18 @@ =usedbar=, =usedvbar=, =usedipat=, =freeratio=, =freebar=, =freevbar=, =freeipat=, =availableratio=, =availablebar=, =availablevbar=, =availableipat= + - Default template: =Mem: % (M)= + - Examples: + + #+begin_src haskell + -- A monitor reporting memory used in Gb + Memory [ "-t", " Gb", "--", "--scale", "1024"] 20 + -- As above, but using one decimal digit to print numbers + Memory [ "-t", " Gb", "-d", "1", "--", "--scale", "1024"] 20 + #+end_src + ***** =Swap Args RefreshRate= - Aliases to =swap= diff --git a/src/Xmobar/Plugins/Monitors/Mem.hs b/src/Xmobar/Plugins/Monitors/Mem.hs index 6a863ce..d55ac2b 100644 --- a/src/Xmobar/Plugins/Monitors/Mem.hs +++ b/src/Xmobar/Plugins/Monitors/Mem.hs @@ -13,7 +13,7 @@ -- ----------------------------------------------------------------------------- -module Xmobar.Plugins.Monitors.Mem (memConfig, runMem, totalMem, usedMem) where +module Xmobar.Plugins.Monitors.Mem (memConfig, runMem) where import Xmobar.Plugins.Monitors.Common import System.Console.GetOpt @@ -29,6 +29,7 @@ data MemOpts = MemOpts { usedIconPattern :: Maybe IconPattern , freeIconPattern :: Maybe IconPattern , availableIconPattern :: Maybe IconPattern + , scale :: Float } defaultOpts :: MemOpts @@ -36,6 +37,7 @@ defaultOpts = MemOpts { usedIconPattern = Nothing , freeIconPattern = Nothing , availableIconPattern = Nothing + , scale = 1.0 } options :: [OptDescr (MemOpts -> MemOpts)] @@ -46,31 +48,29 @@ options = o { freeIconPattern = Just $ parseIconPattern x }) "") "" , Option "" ["available-icon-pattern"] (ReqArg (\x o -> o { availableIconPattern = Just $ parseIconPattern x }) "") "" + , Option "" ["scale"] (ReqArg (\x o -> o { scale = read x }) "") "" ] memConfig :: IO MConfig memConfig = mkMConfig - "Mem: % (M)" -- template + "Mem: % (M)" ["usedbar", "usedvbar", "usedipat", "freebar", "freevbar", "freeipat", "availablebar", "availablevbar", "availableipat", "usedratio", "freeratio", "availableratio", - "total", "free", "buffer", "cache", "available", "used"] -- available replacements - -totalMem :: IO Float -totalMem = fmap ((*1024) . (!!1)) MM.parseMEM - -usedMem :: IO Float -usedMem = fmap ((*1024) . (!!6)) MM.parseMEM + "total", "free", "buffer", "cache", "available", "used"] formatMem :: MemOpts -> [Float] -> Monitor [String] formatMem opts (r:fr:ar:xs) = - do let f = showDigits 0 - mon i x = [showPercentBar (100 * x) x, showVerticalBar (100 * x) x, showIconPattern i x] + do d <- getConfigValue decDigits + let f = showDigits d + mon i x = [ showPercentBar (100 * x) x + , showVerticalBar (100 * x) x + , showIconPattern i x] sequence $ mon (usedIconPattern opts) r ++ mon (freeIconPattern opts) fr ++ mon (availableIconPattern opts) ar ++ map showPercentWithColors [r, fr, ar] - ++ map (showWithColors f) xs + ++ map (showWithColors f . (/ scale opts)) xs formatMem _ _ = replicate 10 `fmap` getConfigValue naString runMem :: [String] -> Monitor String diff --git a/src/Xmobar/Plugins/Monitors/Mem/Linux.hs b/src/Xmobar/Plugins/Monitors/Mem/Linux.hs index 9e48d22..79dcc9d 100644 --- a/src/Xmobar/Plugins/Monitors/Mem/Linux.hs +++ b/src/Xmobar/Plugins/Monitors/Mem/Linux.hs @@ -23,11 +23,14 @@ parseMEM :: IO [Float] parseMEM = do file <- fileMEM let content = map words $ take 8 $ lines file - info = M.fromList $ map (\line -> (head line, (read $ line !! 1 :: Float) / 1024)) content - [total, free, buffer, cache] = map (info M.!) ["MemTotal:", "MemFree:", "Buffers:", "Cached:"] + info = M.fromList $ map ( + \line -> (head line, (read $ line !! 1 :: Float) / 1024)) content + [total, free, buffer, cache] = + map (info M.!) ["MemTotal:", "MemFree:", "Buffers:", "Cached:"] available = M.findWithDefault (free + buffer + cache) "MemAvailable:" info used = total - available usedratio = used / total freeratio = free / total availableratio = available / total - return [usedratio, freeratio, availableratio, total, free, buffer, cache, available, used] + return [ usedratio, freeratio, availableratio + , total, free, buffer, cache, available, used] -- cgit v1.2.3