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