From 9d9ae4166bd0d6b85f1eb19d6f1699a6453517db Mon Sep 17 00:00:00 2001 From: Jose Antonio Ortega Ruiz Date: Sat, 15 Jan 2011 00:44:36 +0100 Subject: Volume monitor: new field 'volumebar', and a bit of refactoring --- README | 2 +- src/Plugins/Monitors/Volume.hs | 32 +++++++++++++++++--------------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/README b/README index 4d94987..2e5eb47 100644 --- a/README +++ b/README @@ -579,7 +579,7 @@ Monitors have default aliases. - `--highd` _number_ High threshold for dB. Defaults to -5.0. - `--lowd` _number_ Low threshold for dB. Defaults to -30.0. - Variables that can be used with the `-t`/`--template` argument: - `volume`, `dB`, `status` + `volume`, `volumebar`, `dB`, `status` - Default template: `Vol: % ` - Requires the package [alsa-mixer] installed in your system. In addition, to activate this plugin you must pass `--flags="with_alsa"` during diff --git a/src/Plugins/Monitors/Volume.hs b/src/Plugins/Monitors/Volume.hs index c0f97a6..0c5f92c 100644 --- a/src/Plugins/Monitors/Volume.hs +++ b/src/Plugins/Monitors/Volume.hs @@ -21,7 +21,8 @@ import Sound.ALSA.Mixer import System.Console.GetOpt volumeConfig :: IO MConfig -volumeConfig = mkMConfig "Vol: % " ["volume","dB","status"] +volumeConfig = mkMConfig "Vol: % " + ["volume", "volumebar", "dB","status"] data VolumeOpts = VolumeOpts @@ -66,9 +67,13 @@ percent v' lo' hi' = (v - lo) / (hi - lo) hi = fromIntegral hi' formatVol :: Integer -> Integer -> Integer -> Monitor String -formatVol v lo hi = +formatVol lo hi v = showPercentWithColors $ percent v lo hi +formatVolBar :: Integer -> Integer -> Integer -> Monitor String +formatVolBar lo hi v = + showPercentBar (100 * x) x where x = percent v lo hi + switchHelper :: VolumeOpts -> (VolumeOpts -> Maybe String) -> (VolumeOpts -> String) @@ -85,12 +90,13 @@ formatSwitch opts False = switchHelper opts offColor offString colorHelper :: Maybe String -> String colorHelper = maybe "" (\c -> "") -formatDb :: VolumeOpts -> Float -> Monitor String -formatDb opts db = do +formatDb :: VolumeOpts -> Integer -> Monitor String +formatDb opts dbi = do h <- getConfigValue highColor m <- getConfigValue normalColor l <- getConfigValue lowColor - let digits = showDigits 0 db + let db = fromIntegral dbi / 100.0 + digits = showDigits 0 db startColor | db >= highDbThresh opts = colorHelper h | db < lowDbThresh opts = colorHelper l | otherwise = colorHelper m @@ -106,17 +112,13 @@ runVolume mixerName controlName argv = do (common $ volume control) switchControl = fromJust $ maybe (playback $ switch control) Just (common $ switch control) + maybeNA = maybe (return "N/A") (lo, hi) <- io $ getRange volumeControl val <- io $ getChannel FrontLeft $ value volumeControl db <- io $ getChannel FrontLeft $ dB volumeControl sw <- io $ getChannel FrontLeft switchControl - p <- case val of - Just x -> formatVol x lo hi - Nothing -> formatVol hi lo hi - d <- case db of - Just x -> formatDb opts $ fromIntegral x / 100.0 - Nothing -> formatDb opts 0.0 - s <- case sw of - Just x -> formatSwitch opts x - Nothing -> formatSwitch opts True - parseTemplate [ p, d, s ] + p <- maybeNA (formatVol lo hi) val + b <- maybeNA (formatVolBar lo hi) val + d <- maybeNA (formatDb opts) db + s <- maybeNA (formatSwitch opts) sw + parseTemplate [p, b, d, s] -- cgit v1.2.3