forked from colonelpanic/dotfiles
[Linux] Add toggling support to rofi_select_input.hs
This commit is contained in:
parent
fb24509e21
commit
b95f990a6f
@ -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)
|
||||||
|
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
|
case exitCode of
|
||||||
ExitSuccess -> do
|
ExitSuccess ->
|
||||||
let selectedSink = head $ splitOn " " $ unpack selection
|
void $ setMuteAction (toSetString selectedIsMuted) selectedSink
|
||||||
mapM_ (setMuteAction "1" . head) matches
|
ExitFailure 10 ->
|
||||||
setMuteAction "0" selectedSink
|
do
|
||||||
return ()
|
mapM_ (setMuteAction "1" . head) matches
|
||||||
|
void unMuteSelected
|
||||||
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"
|
||||||
|
Loading…
Reference in New Issue
Block a user