[Linux] Add toggling support to rofi_select_input.hs

This commit is contained in:
Ivan Malison 2017-03-30 16:46:02 -07:00
parent fb24509e21
commit b95f990a6f
No known key found for this signature in database
GPG Key ID: 62530EFBE99DC2F8

View File

@ -7,26 +7,41 @@ import Data.Maybe
import Data.Text (unpack)
import Text.Regex
import Text.Printf
import Turtle hiding (printf)
import Turtle.Shell
import Turtle hiding (printf, find)
main :: IO ()
main = do
out <- unpack <$> getSinkText
let sinkInfos = splitOn "\nSink" out
matches = catMaybes $ matchRegex sinkRegex <$> sinkInfos
entries = map buildEntry matches
rofiText = intercalate "\n" entries
(exitCode, selection) <- shellStrict "rofi -dmenu -i" (select $ map fromString entries)
(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
case exitCode of
ExitSuccess -> do
let selectedSink = head $ splitOn " " $ unpack selection
mapM_ (setMuteAction "1" . head) matches
setMuteAction "0" selectedSink
return ()
ExitSuccess ->
void $ setMuteAction (toSetString selectedIsMuted) selectedSink
ExitFailure 10 ->
do
mapM_ (setMuteAction "1" . head) matches
void unMuteSelected
ExitFailure _ -> return ()
where getSinkText = snd <$> shellStrict "pactl list sink-inputs" empty
sinkRegex = mkRegexWithOpts "Input .([0-9]*).*?application.name =([^\n]*)" False True
buildEntry (num:name:_) = let app = (filter (not . (`elem` ("\"" :: String))) name) in printf "%s - %s" num $ trim app
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 _ = ""
setMuteAction status sink = shell (fromString $ setMuteCommand status sink) empty
setMuteCommand status sink = "pactl set-sink-input-mute " ++ sink ++ " " ++ status
trim = dropWhileEnd (== ' ') . dropWhile (== ' ')
isMuted = (== "yes")
muteString status = if isMuted status then " (Muted)" else "" :: String
noQuotes = filter (not . (`elem` ("\"" :: String)))
toSetString True = "0"
toSetString False = "1"