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.Hooks.DynamicLog (wrap, xmobarColor)
import XMonad.Internal.Command.Power (hasBattery)
import XMonad.Internal.DBus.Common (xmonadBus)
import XMonad.Internal.DBus.Control (pathExists)
import XMonad.Internal.DBus.IntelBacklight (blPath)
@ -138,6 +139,31 @@ getEthernet = do
[n] -> Just $ ethernetCmd n
_ -> 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
{ csAlias = vpnAlias
@ -161,6 +187,7 @@ myCommands = do
wirelessSpec <- getWireless
ethernetSpec <- getEthernet
vpnSpec <- getVPN
batterySpec <- getBattery
let left =
[ CmdSpec
{ csAlias = "UnsafeStdinReader"
@ -196,24 +223,7 @@ myCommands = do
]
}
, Just $ 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
}
, batterySpec
, Just $ CmdSpec
{ csAlias = "intelbacklight"

View File

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