[Linux] Use haskell to write rofi_select_input

This commit is contained in:
Ivan Malison 2017-03-30 01:21:08 -07:00
parent de0671026a
commit e94b7ac060
No known key found for this signature in database
GPG Key ID: 62530EFBE99DC2F8
2 changed files with 29 additions and 9 deletions

View File

@ -0,0 +1,29 @@
#!/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
import Turtle hiding (printf)
import Turtle.Shell
main = do
out <- unpack <$> getSinkText
let sinkInfos = splitOn "\nSink" out
matches = catMaybes $ matchRegex sinkRegex <$> sinkInfos
entries = map buildEntry matches
rofiText = intercalate "\n" entries
selection <- snd <$> shellStrict "rofi -dmenu -i" (select $ map fromString entries)
let selectedSink = head $ splitOn " " $ unpack selection
mapM_ (setMuteAction "1" . head) matches
setMuteAction "0" selectedSink
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
setMuteAction status sink = shell (fromString $ setMuteCommand status sink) empty
setMuteCommand status sink = "pactl set-sink-input-mute " ++ sink ++ " " ++ status
trim = dropWhileEnd (== ' ') . dropWhile (== ' ')

View File

@ -1,9 +0,0 @@
#!/usr/bin/env sh
inputs=$(pactl list sink-inputs | grep 'Sink Input' | grep -Eo '[0-9]*')
selection=$(echo "$inputs" | rofi -dmenu -i)
echo "$inputs" | xargs -I {} -n1 pactl set-sink-input-mute {} 1
pactl set-sink-input-mute "$selection" 0