From e94b7ac060d37d575501478524241c3aeb9c4df1 Mon Sep 17 00:00:00 2001 From: Ivan Malison Date: Thu, 30 Mar 2017 01:21:08 -0700 Subject: [PATCH] [Linux] Use haskell to write rofi_select_input --- dotfiles/lib/bin/rofi_select_input.hs | 29 +++++++++++++++++++++++++++ dotfiles/lib/bin/rofi_select_input.sh | 9 --------- 2 files changed, 29 insertions(+), 9 deletions(-) create mode 100755 dotfiles/lib/bin/rofi_select_input.hs delete mode 100755 dotfiles/lib/bin/rofi_select_input.sh diff --git a/dotfiles/lib/bin/rofi_select_input.hs b/dotfiles/lib/bin/rofi_select_input.hs new file mode 100755 index 00000000..71a7ca19 --- /dev/null +++ b/dotfiles/lib/bin/rofi_select_input.hs @@ -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 (== ' ') diff --git a/dotfiles/lib/bin/rofi_select_input.sh b/dotfiles/lib/bin/rofi_select_input.sh deleted file mode 100755 index 09604839..00000000 --- a/dotfiles/lib/bin/rofi_select_input.sh +++ /dev/null @@ -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 -