From 365d0a7c97f3905c16caf70c0bb7ef6054b232f2 Mon Sep 17 00:00:00 2001 From: jao Date: Sun, 30 Jun 2019 00:49:00 +0100 Subject: New options -a, -A for low battery notifications --- changelog.md | 3 +++ readme.md | 14 ++++++++++++-- src/Xmobar/Plugins/Monitors/Batt.hs | 18 ++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/changelog.md b/changelog.md index fd1316d..2ca5d3f 100644 --- a/changelog.md +++ b/changelog.md @@ -6,6 +6,9 @@ _New features_ bytes rather than speeds (see [issue #390]. - `WeatherX`: An extension to the `Weather` monitor allowing the spefication of custom strings or icons for sky conditions. + - The battery monitors accept the new arguments `-a` and `-A` to + specify a system command executed if battery left goes beyond a + given threshold. [issue #390]: https://github.com/jaor/xmobar/issues/390 diff --git a/readme.md b/readme.md index 2214bda..c8a75e6 100644 --- a/readme.md +++ b/readme.md @@ -898,6 +898,12 @@ specification, such as `("clear", "")`. - `-p`: color to display positive power (battery charging) - `-f`: file in `/sys/class/power_supply` with AC info (default: "AC/online") + - `-A`: a number between 0 and 100, threshold below which the action + given by `-a`, if any, is performed (default: 5) + - `-a`: a string with a system command that is run when the + percentage left in the battery is less or equal than the threshold + given by the `-A` option. If not present, no action is + undertaken. - `--on-icon-pattern`: dynamic string for current battery charge when AC is "on" in `leftipat`. - `--off-icon-pattern`: dynamic string for current battery charge @@ -916,14 +922,18 @@ specification, such as `("clear", "")`. "-L", "10", "-H", "80", "-p", "3", "--", "-O", "On - ", "-i", "", "-L", "-15", "-H", "-5", - "-l", "red", "-m", "blue", "-h", "green"] + "-l", "red", "-m", "blue", "-h", "green" + "-a", "notify-send -u critical 'Battery running out!!'", + "-A", "3"] 600 In the above example, the thresholds before the "--" separator affect only the `` and `` fields, while those after the separator affect how `` is displayed. For this monitor, neither the generic nor the specific options have any effect on - ``. + ``. We are also telling the monitor to execute the unix + command `notify-send` when the percentage left in the battery + reaches 6%. It is also possible to specify template variables in the `-O` and `-o` switches, as in the following example: diff --git a/src/Xmobar/Plugins/Monitors/Batt.hs b/src/Xmobar/Plugins/Monitors/Batt.hs index 9f74835..bebbf1c 100644 --- a/src/Xmobar/Plugins/Monitors/Batt.hs +++ b/src/Xmobar/Plugins/Monitors/Batt.hs @@ -15,6 +15,8 @@ module Xmobar.Plugins.Monitors.Batt ( battConfig, runBatt, runBatt' ) where +import System.Process (system) +import Control.Monad (void) import Control.Exception (SomeException, handle) import Xmobar.Plugins.Monitors.Common import System.FilePath (()) @@ -36,6 +38,8 @@ data BattOpts = BattOpts , highWColor :: Maybe String , lowThreshold :: Float , highThreshold :: Float + , onLowAction :: Maybe String + , actionThreshold :: Float , onlineFile :: FilePath , scale :: Float , onIconPattern :: Maybe IconPattern @@ -52,6 +56,8 @@ defaultOpts = BattOpts , lowWColor = Nothing , mediumWColor = Nothing , highWColor = Nothing + , onLowAction = Nothing + , actionThreshold = 6 , lowThreshold = 10 , highThreshold = 12 , onlineFile = "AC/online" @@ -74,6 +80,9 @@ options = , Option "H" ["hight"] (ReqArg (\x o -> o { highThreshold = read x }) "") "" , Option "f" ["online"] (ReqArg (\x o -> o { onlineFile = x }) "") "" , Option "s" ["scale"] (ReqArg (\x o -> o {scale = read x}) "") "" + , Option "a" ["action"] (ReqArg (\x o -> o { onLowAction = Just x }) "") "" + , Option "A" ["action-threshold"] + (ReqArg (\x o -> o { actionThreshold = read x }) "") "" , Option "" ["on-icon-pattern"] (ReqArg (\x o -> o { onIconPattern = Just $ parseIconPattern x }) "") "" , Option "" ["off-icon-pattern"] (ReqArg (\x o -> @@ -172,6 +181,14 @@ sortOn f = mostCommonDef :: Eq a => a -> [a] -> a mostCommonDef x xs = head $ last $ [x] : sortOn length (group xs) +maybeAlert :: BattOpts -> Float -> IO () +maybeAlert opts left = + case onLowAction opts of + Nothing -> do return () + Just x -> if not (isNaN left) && actionThreshold opts >= (100 * left) + then void $ system x + else return () + readBatteries :: BattOpts -> [Files] -> IO Result readBatteries opts bfs = do let bfs' = filter (/= NoFiles) bfs @@ -192,6 +209,7 @@ readBatteries opts bfs = | time == 0 = Idle | ac = Charging | otherwise = Discharging + maybeAlert opts left return $ if isNaN left then NA else Result left watts time racst runBatt :: [String] -> Monitor String -- cgit v1.2.3