Guest User

Untitled

a guest
Jun 22nd, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. #!/usr/bin/env stack
  2. -- stack script --resolver lts-8.12
  3. -- --package turtle
  4. -- --package string-conv
  5.  
  6. -- Compile like:
  7. -- > stack exec --package turtle --package string-conv -- ghc --make BumpSound.hs
  8.  
  9. {-# LANGUAGE OverloadedStrings #-}
  10.  
  11. import Turtle
  12. import qualified Control.Foldl as Fold
  13. import Data.String.Conv
  14. import Control.Monad
  15.  
  16.  
  17. stepSize = 4
  18. maxSound = 70
  19. minSound = 0
  20.  
  21.  
  22. shell' c = shell c empty
  23.  
  24.  
  25. parser :: Parser (Text)
  26. parser = argText "direction" "Either 'up' or 'down'."
  27.  
  28.  
  29. main :: IO ()
  30. main = void $ do
  31. cmd <- options "amixer sound bumper" parser
  32. vol <- currentVolume
  33.  
  34. case cmd of
  35. "down" -> step vol (-stepSize)
  36. "up" -> step vol stepSize
  37.  
  38.  
  39. step :: Maybe Int -> Int -> IO ()
  40. step Nothing _ = return ()
  41. step (Just v) s = do
  42. sh $ shell' ("amixer set Master " <> (toS (show nextVol)) <> "%")
  43. where
  44. nextVol :: Int
  45. nextVol = min (max minSound (v+s)) maxSound
  46.  
  47.  
  48. currentVolume :: IO (Maybe Int)
  49. currentVolume = do
  50. v <- flip fold Fold.head $ inshell "amixer get Master | grep '\\[on\\]' | awk '{ print $4 }' | grep -Po '\\d+'" ""
  51. return $ read . toS . lineToText <$> v
Add Comment
Please, Sign In to add comment