2017-03-30 01:21:08 -07:00
|
|
|
#!/usr/bin/env runhaskell
|
|
|
|
{-# LANGUAGE OverloadedStrings, AllowAmbiguousTypes #-}
|
|
|
|
|
|
|
|
import Data.List
|
|
|
|
import Data.List.Split
|
|
|
|
import Data.Maybe
|
|
|
|
import Data.Text (unpack)
|
|
|
|
import Text.Regex
|
|
|
|
import Text.Printf
|
2017-03-30 16:46:02 -07:00
|
|
|
import Turtle hiding (printf, find)
|
2017-03-30 01:21:08 -07:00
|
|
|
|
2017-03-30 16:46:02 -07:00
|
|
|
main :: IO ()
|
2017-03-30 01:21:08 -07:00
|
|
|
main = do
|
|
|
|
out <- unpack <$> getSinkText
|
|
|
|
let sinkInfos = splitOn "\nSink" out
|
|
|
|
matches = catMaybes $ matchRegex sinkRegex <$> sinkInfos
|
|
|
|
entries = map buildEntry matches
|
2017-03-30 16:46:02 -07:00
|
|
|
(exitCode, selection) <- shellStrict "rofi -dmenu -i -kb-custom-1 'Alt-o'"
|
|
|
|
(select $ map fromString entries)
|
|
|
|
let selectedSink = head $ splitOn " " $ unpack selection
|
|
|
|
unMuteSelected = setMuteAction "0" selectedSink
|
|
|
|
selectedIsMuted = fromMaybe True $
|
|
|
|
isMuted . (!! 1) <$> find ((== selectedSink) . head) matches
|
|
|
|
print selectedSink
|
|
|
|
print matches
|
|
|
|
print selectedIsMuted
|
2017-03-30 11:36:44 -07:00
|
|
|
case exitCode of
|
2017-03-30 16:46:02 -07:00
|
|
|
ExitSuccess ->
|
|
|
|
void $ setMuteAction (toSetString selectedIsMuted) selectedSink
|
|
|
|
ExitFailure 10 ->
|
|
|
|
do
|
|
|
|
mapM_ (setMuteAction "1" . head) matches
|
|
|
|
void unMuteSelected
|
2017-03-30 11:36:44 -07:00
|
|
|
ExitFailure _ -> return ()
|
2017-03-30 01:21:08 -07:00
|
|
|
where getSinkText = snd <$> shellStrict "pactl list sink-inputs" empty
|
2017-03-30 16:46:02 -07:00
|
|
|
sinkRegex = mkRegexWithOpts "Input .([0-9]*).*?Mute: ([^\n]*).*?application.name =([^\n]*)" False True
|
|
|
|
buildEntry (num:status:name:_) =
|
|
|
|
printf "%s - %s%s" num (trim $ noQuotes name) (muteString status)
|
|
|
|
buildEntry _ = ""
|
2017-03-30 01:21:08 -07:00
|
|
|
setMuteAction status sink = shell (fromString $ setMuteCommand status sink) empty
|
|
|
|
setMuteCommand status sink = "pactl set-sink-input-mute " ++ sink ++ " " ++ status
|
|
|
|
trim = dropWhileEnd (== ' ') . dropWhile (== ' ')
|
2017-03-30 16:46:02 -07:00
|
|
|
isMuted = (== "yes")
|
|
|
|
muteString status = if isMuted status then " (Muted)" else "" :: String
|
|
|
|
noQuotes = filter (not . (`elem` ("\"" :: String)))
|
|
|
|
toSetString True = "0"
|
|
|
|
toSetString False = "1"
|