[linux] 80chars

This commit is contained in:
Ivan Malison 2018-04-28 01:57:49 -07:00
parent 887e0a77f3
commit d534721312
No known key found for this signature in database
GPG Key ID: 62530EFBE99DC2F8

View File

@ -5,7 +5,6 @@ import Control.Monad
import Data.List
import Data.List.Split
import Data.Maybe
import Data.Text (unpack)
import System.Process
import Text.Printf
import Text.Regex
@ -17,12 +16,13 @@ main = do
let sinkInfos = splitOn "\nSink" out
matches = catMaybes $ matchRegex sinkRegex <$> sinkInfos
entries = map buildEntry matches
(exitCode, selection, _) <- readCreateProcessWithExitCode (shell "rofi -dmenu -i -kb-custom-1 'Alt-o'") $
intercalate "\n" entries
(exitCode, selection, _) <-
readCreateProcessWithExitCode
(shell "rofi -dmenu -i -kb-custom-1 'Alt-o'") $ intercalate "\n" entries
let selectedSink = head $ splitOn " " selection
unMuteSelected = setMuteAction "0" selectedSink
selectedIsMuted = fromMaybe True $
isMuted . (!! 1) <$> find ((== selectedSink) . head) matches
selectedIsMuted =
maybe True (isMuted . (!! 1)) $ find ((== selectedSink) . head) matches
setAll state = mapM_ (setMuteAction state . head) matches
case exitCode of
ExitSuccess ->
@ -32,18 +32,25 @@ main = do
setAll "1"
void unMuteSelected
ExitFailure _ -> setAll "0"
where getSinkText = do
(_, txt, _) <- readCreateProcessWithExitCode (shell "pactl list sink-inputs") ""
where getSinkText =
do
(_, txt, _) <- readCreateProcessWithExitCode
(shell "pactl list sink-inputs") ""
return txt
sinkRegex = mkRegexWithOpts "Input .([0-9]*).*?Mute: ([^\n]*).*?application.name =([^\n]*)" False True
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 = callCommand $ setMuteCommand status sink
setMuteCommand status sink = "pactl set-sink-input-mute " ++ sink ++ " " ++ status
setMuteCommand status sink =
"pactl set-sink-input-mute " ++ sink ++ " " ++ status
trim = dropWhileEnd (== ' ') . dropWhile (== ' ')
isMuted = (== "yes")
muteString status = if isMuted status then " (Muted)" else "" :: String
muteString status =
if isMuted status then " (Muted)" else "" :: String
noQuotes = filter (not . (`elem` ("\"" :: String)))
toSetString True = "0"
toSetString False = "1"