ENH test for battery before displaying discharge thing in xmobar

This commit is contained in:
Nathan Dwarshuis 2021-06-23 20:47:41 -04:00
parent f456ed93bc
commit 07e8f0f34d
2 changed files with 42 additions and 24 deletions

View File

@ -32,6 +32,7 @@ import Xmobar.Plugins.VPN
import XMonad (getXMonadDir) import XMonad (getXMonadDir)
import XMonad.Hooks.DynamicLog (wrap, xmobarColor) import XMonad.Hooks.DynamicLog (wrap, xmobarColor)
import XMonad.Internal.Command.Power (hasBattery)
import XMonad.Internal.DBus.Common (xmonadBus) import XMonad.Internal.DBus.Common (xmonadBus)
import XMonad.Internal.DBus.Control (pathExists) import XMonad.Internal.DBus.Control (pathExists)
import XMonad.Internal.DBus.IntelBacklight (blPath) import XMonad.Internal.DBus.IntelBacklight (blPath)
@ -138,6 +139,31 @@ getEthernet = do
[n] -> Just $ ethernetCmd n [n] -> Just $ ethernetCmd n
_ -> Nothing _ -> Nothing
batteryCmd :: CmdSpec
batteryCmd = CmdSpec
{ csAlias = "battery"
, csDepends = Nothing
, csRunnable = Run
$ Battery
[ "--template", "<acstatus><left>"
, "--Low", "10"
, "--High", "80"
, "--low", "red"
, "--normal", T.fgColor
, "--high", T.fgColor
, "--"
, "-P"
, "-o" , "<fn=1>\xf0e7</fn>"
, "-O" , "<fn=1>\xf1e6</fn>"
, "-i" , "<fn=1>\xf1e6</fn>"
] 50
}
getBattery :: IO (Maybe CmdSpec)
getBattery = do
b <- hasBattery
return $ if b then Just batteryCmd else Nothing
vpnCmd :: CmdSpec vpnCmd :: CmdSpec
vpnCmd = CmdSpec vpnCmd = CmdSpec
{ csAlias = vpnAlias { csAlias = vpnAlias
@ -161,6 +187,7 @@ myCommands = do
wirelessSpec <- getWireless wirelessSpec <- getWireless
ethernetSpec <- getEthernet ethernetSpec <- getEthernet
vpnSpec <- getVPN vpnSpec <- getVPN
batterySpec <- getBattery
let left = let left =
[ CmdSpec [ CmdSpec
{ csAlias = "UnsafeStdinReader" { csAlias = "UnsafeStdinReader"
@ -196,24 +223,7 @@ myCommands = do
] ]
} }
, Just $ CmdSpec , batterySpec
{ csAlias = "battery"
, csDepends = Nothing
, csRunnable = Run
$ Battery
[ "--template", "<acstatus><left>"
, "--Low", "10"
, "--High", "80"
, "--low", "red"
, "--normal", T.fgColor
, "--high", T.fgColor
, "--"
, "-P"
, "-o" , "<fn=1>\xf0e7</fn>"
, "-O" , "<fn=1>\xf1e6</fn>"
, "-i" , "<fn=1>\xf1e6</fn>"
] 50
}
, Just $ CmdSpec , Just $ CmdSpec
{ csAlias = "intelbacklight" { csAlias = "intelbacklight"

View File

@ -11,16 +11,20 @@ module XMonad.Internal.Command.Power
, runSuspend , runSuspend
, runSuspendPrompt , runSuspendPrompt
, runQuitPrompt , runQuitPrompt
, hasBattery
) where ) where
import Control.Arrow (first) import Control.Arrow (first)
import Data.Either
import qualified Data.Map as M import qualified Data.Map as M
import Graphics.X11.Types import Graphics.X11.Types
import System.Directory import System.Directory
import System.Exit import System.Exit
import System.FilePath.Posix
import System.IO.Error
import System.Process import System.Process
import XMonad.Core import XMonad.Core
@ -98,18 +102,22 @@ hasSwitchableGPU = withShellOutput cmd hasIntelAndOther
ivendors = filter (== "Intel Corporation") vendors in ivendors = filter (== "Intel Corporation") vendors in
length vendors > length ivendors && not (null ivendors) length vendors > length ivendors && not (null ivendors)
-- this is hacky but so much easier than the "pure haskell" solution hasBattery :: IO Bool
hasBattery :: IO (Maybe Bool) hasBattery = do
hasBattery = withShellOutput (fmtCmd "cat" ["/sys/class/power_supply/*/type"]) ps <- fromRight [] <$> tryIOError (listDirectory syspath)
$ elem "Battery" . lines ts <- mapM readType ps
return $ "Battery\n" `elem` ts
where
readType p = fromRight [] <$> tryIOError (readFile $ syspath </> p </> "type")
syspath = "/sys/class/power_supply"
requireOptimus :: IO Bool requireOptimus :: IO Bool
requireOptimus = do requireOptimus = do
s <- hasSwitchableGPU s <- hasSwitchableGPU
b <- hasBattery b <- hasBattery
case (s, b) of case (s, b) of
(Just True, Just True) -> return True (Just True, True) -> return True
_ -> warn >> return False _ -> warn >> return False
where where
warn = putStrLn warn = putStrLn
"WARNING: could not determine if switchable GPU present. Assuming not" "WARNING: could not determine if switchable GPU present. Assuming not"