diff options
Diffstat (limited to 'src/Plugins/Monitors/Bright.hs')
-rw-r--r-- | src/Plugins/Monitors/Bright.hs | 52 |
1 files changed, 23 insertions, 29 deletions
diff --git a/src/Plugins/Monitors/Bright.hs b/src/Plugins/Monitors/Bright.hs index 0679ab8..cb510f6 100644 --- a/src/Plugins/Monitors/Bright.hs +++ b/src/Plugins/Monitors/Bright.hs @@ -14,9 +14,9 @@ module Plugins.Monitors.Bright (brightConfig, runBright) where +import Control.Applicative ((<$>)) import Control.Exception (SomeException, handle) import qualified Data.ByteString.Lazy.Char8 as B -import Data.Char import System.FilePath ((</>)) import System.Posix.Files (fileExist) import System.Console.GetOpt @@ -26,18 +26,22 @@ import Plugins.Monitors.Common data BrightOpts = BrightOpts { subDir :: String , currBright :: String , maxBright :: String + , curBrightIconPattern :: Maybe IconPattern } defaultOpts :: BrightOpts defaultOpts = BrightOpts { subDir = "acpi_video0" , currBright = "actual_brightness" , maxBright = "max_brightness" + , curBrightIconPattern = Nothing } options :: [OptDescr (BrightOpts -> BrightOpts)] options = [ Option "D" ["device"] (ReqArg (\x o -> o { subDir = x }) "") "" , Option "C" ["curr"] (ReqArg (\x o -> o { currBright = x }) "") "" , Option "M" ["max"] (ReqArg (\x o -> o { maxBright = x }) "") "" + , Option "" ["brightness-icon-pattern"] (ReqArg (\x o -> + o { curBrightIconPattern = Just $ parseIconPattern x }) "") "" ] -- from Batt.hs @@ -52,7 +56,7 @@ sysDir = "/sys/class/backlight/" brightConfig :: IO MConfig brightConfig = mkMConfig "<percent>" -- template - ["hbar", "percent", "bar"] -- replacements + ["vbar", "percent", "bar", "ipat"] -- replacements data Files = Files { fCurr :: String , fMax :: String @@ -61,12 +65,12 @@ data Files = Files { fCurr :: String brightFiles :: BrightOpts -> IO Files brightFiles opts = do - is_curr <- fileExist $ (fCurr files) - is_max <- fileExist $ (fCurr files) - if is_curr && is_max then return files else return NoFiles - where prefix = sysDir </> (subDir opts) - files = Files { fCurr = prefix </> (currBright opts) - , fMax = prefix </> (maxBright opts) + is_curr <- fileExist $ fCurr files + is_max <- fileExist $ fCurr files + return (if is_curr && is_max then files else NoFiles) + where prefix = sysDir </> subDir opts + files = Files { fCurr = prefix </> currBright opts + , fMax = prefix </> maxBright opts } runBright :: [String] -> Monitor String @@ -76,30 +80,20 @@ runBright args = do c <- io $ readBright f case f of NoFiles -> return "hurz" - _ -> fmtPercent c >>= parseTemplate - where fmtPercent :: Float -> Monitor [String] - fmtPercent c = do r <- showHorizontalBar (100 * c) - s <- showPercentWithColors c - t <- showPercentBar (100 * c) c - return [r,s,t] + _ -> fmtPercent opts c >>= parseTemplate + where fmtPercent :: BrightOpts -> Float -> Monitor [String] + fmtPercent opts c = do r <- showVerticalBar (100 * c) c + s <- showPercentWithColors c + t <- showPercentBar (100 * c) c + d <- showIconPattern (curBrightIconPattern opts) c + return [r,s,t,d] readBright :: Files -> IO Float readBright NoFiles = return 0 readBright files = do - currVal<- grab $ (fCurr files) - maxVal <- grab $ (fMax files) - return $ (currVal / maxVal) - where grab f = handle handler (fmap (read . B.unpack) $ B.readFile f) + currVal<- grab $ fCurr files + maxVal <- grab $ fMax files + return (currVal / maxVal) + where grab f = handle handler (read . B.unpack <$> B.readFile f) handler = const (return 0) :: SomeException -> IO Float -showHorizontalBar :: Float -> Monitor String -showHorizontalBar x = do - return $ [convert x] - where convert :: Float -> Char - convert val - | t <= 9600 = ' ' - | t > 9608 = chr 9608 - | otherwise = chr t - where - -- we scale from 0 to 100, we have 8 slots (9 elements), 100/8 = 12 - t = 9600 + ((round val) `div` 12) |