summaryrefslogtreecommitdiffhomepage
path: root/src/Xmobar/Plugins/Monitors/Batt.hs
diff options
context:
space:
mode:
authorjao <jao@gnu.org>2019-06-30 00:49:00 +0100
committerjao <jao@gnu.org>2019-06-30 00:49:00 +0100
commit365d0a7c97f3905c16caf70c0bb7ef6054b232f2 (patch)
tree82d42a8705ebc1b3f44f538b2bdb4e4a27a25054 /src/Xmobar/Plugins/Monitors/Batt.hs
parenta160cc25ba27b17e798f9d0eadd2c9576473ae9a (diff)
downloadxmobar-365d0a7c97f3905c16caf70c0bb7ef6054b232f2.tar.gz
xmobar-365d0a7c97f3905c16caf70c0bb7ef6054b232f2.tar.bz2
New options -a, -A for low battery notifications
Diffstat (limited to 'src/Xmobar/Plugins/Monitors/Batt.hs')
-rw-r--r--src/Xmobar/Plugins/Monitors/Batt.hs18
1 files changed, 18 insertions, 0 deletions
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