From 0ddcc4362ccab3a443d244c6c8beb7a1eef4d9a4 Mon Sep 17 00:00:00 2001 From: Josh Rahm Date: Fri, 3 Feb 2023 11:05:39 -0700 Subject: Rename spotify-control to media-control. This is part of a plan to decouple Spotify from RDE. --- extras/HOME/.local/bin/media-control | 107 +++++++++++++++++++++++++++++++++ extras/HOME/.local/bin/spotify-control | 107 --------------------------------- extras/HOME/.xmobarrc | 2 +- extras/HOME/.xmonad/xmobar-media | 2 +- src/Rahm/Desktop/Keys.hs | 6 +- xmobar/extras/weather/src/Main.hs | 101 ++++++++++++++++--------------- 6 files changed, 165 insertions(+), 160 deletions(-) create mode 100755 extras/HOME/.local/bin/media-control delete mode 100755 extras/HOME/.local/bin/spotify-control diff --git a/extras/HOME/.local/bin/media-control b/extras/HOME/.local/bin/media-control new file mode 100755 index 0000000..34a2633 --- /dev/null +++ b/extras/HOME/.local/bin/media-control @@ -0,0 +1,107 @@ +#!/bin/bash + +if [ $# -lt 1 ] +then + echo "No command?" + exit +fi + +target_hint="spotify" +while [[ "$1" == --* ]] ; do + arg="$1" + case "$arg" in + --target-hint=*) + target_hint="${arg/--target-hint=/}" + ;; + *) + echo "Bad Argument $1" + exit 1 + ;; + esac + shift +done + +# Some targets (spotifyd) don't have a stable dbus path. +targets="$( \ + dbus-send \ + --print-reply \ + --dest=org.freedesktop.DBus \ + /org/freedesktop/DBus org.freedesktop.DBus.ListNames \ + | sed 's#.*string \"\(.*\)"#\1#' \ + | grep MediaPlayer2)" + +# Prefer the target hint. +target="$(echo "$targets" | grep -i "$target_hint" | head -n1)" +if [ -z "$target" ] ; then + # If no spotify, pick an arbitrary one + target="$(echo "$targets" | head -n1)" +fi + +function mpris2_dbus_player_do { + dbus-send \ + --print-reply \ + --dest="$target" \ + /org/mpris/MediaPlayer2 \ + "org.mpris.MediaPlayer2.Player.$1" +} + +function mpris2_dbus_get_player_property { + dbus-send \ + --print-reply \ + --dest="$target" \ + /org/mpris/MediaPlayer2 \ + org.freedesktop.DBus.Properties.Get \ + string:'org.mpris.MediaPlayer2.Player' "string:$1" +} + +case $1 in + "play") + mpris2_dbus_player_do PlayPause + ;; + "next") + mpris2_dbus_player_do Next + ;; + "pause") + mpris2_dbus_player_do Pause + ;; + "justplay") + mpris2_dbus_player_do Play + ;; + "prev") + mpris2_dbus_player_do Previous + ;; + "getTitle") + mpris2_dbus_get_player_property 'Metadata' | \ + egrep -A 1 "title" | \ + egrep -v "title" | \ + cut -b 44- | \ + cut -d '"' -f 1 | \ + egrep -v ^$ + ;; + "getArtist") + mpris2_dbus_get_player_property 'Metadata' | \ + -A 2 "artist" | \ + egrep -v "artist" | \ + egrep -v "array" | \ + cut -b 27- | \ + cut -d '"' -f 1 | \ + egrep -v ^$ + ;; + "getAlbum") + mpris2_dbus_get_player_property 'Metadata' | \ + egrep -A 2 "album" | \ + egrep -v "album" | \ + egrep -v "array" | \ + cut -b 44- | \ + cut -d '"' -f 1 | \ + egrep -v ^$ + ;; + "getStatus") + mpris2_dbus_get_player_property 'PlaybackStatus' | \ + grep 'string "[^"]*"' | \ + sed 's/.*"\(.*\)"[^"]*$/\1/' + ;; + *) + echo "Unknown command: " $1 + ;; +esac diff --git a/extras/HOME/.local/bin/spotify-control b/extras/HOME/.local/bin/spotify-control deleted file mode 100755 index 34a2633..0000000 --- a/extras/HOME/.local/bin/spotify-control +++ /dev/null @@ -1,107 +0,0 @@ -#!/bin/bash - -if [ $# -lt 1 ] -then - echo "No command?" - exit -fi - -target_hint="spotify" -while [[ "$1" == --* ]] ; do - arg="$1" - case "$arg" in - --target-hint=*) - target_hint="${arg/--target-hint=/}" - ;; - *) - echo "Bad Argument $1" - exit 1 - ;; - esac - shift -done - -# Some targets (spotifyd) don't have a stable dbus path. -targets="$( \ - dbus-send \ - --print-reply \ - --dest=org.freedesktop.DBus \ - /org/freedesktop/DBus org.freedesktop.DBus.ListNames \ - | sed 's#.*string \"\(.*\)"#\1#' \ - | grep MediaPlayer2)" - -# Prefer the target hint. -target="$(echo "$targets" | grep -i "$target_hint" | head -n1)" -if [ -z "$target" ] ; then - # If no spotify, pick an arbitrary one - target="$(echo "$targets" | head -n1)" -fi - -function mpris2_dbus_player_do { - dbus-send \ - --print-reply \ - --dest="$target" \ - /org/mpris/MediaPlayer2 \ - "org.mpris.MediaPlayer2.Player.$1" -} - -function mpris2_dbus_get_player_property { - dbus-send \ - --print-reply \ - --dest="$target" \ - /org/mpris/MediaPlayer2 \ - org.freedesktop.DBus.Properties.Get \ - string:'org.mpris.MediaPlayer2.Player' "string:$1" -} - -case $1 in - "play") - mpris2_dbus_player_do PlayPause - ;; - "next") - mpris2_dbus_player_do Next - ;; - "pause") - mpris2_dbus_player_do Pause - ;; - "justplay") - mpris2_dbus_player_do Play - ;; - "prev") - mpris2_dbus_player_do Previous - ;; - "getTitle") - mpris2_dbus_get_player_property 'Metadata' | \ - egrep -A 1 "title" | \ - egrep -v "title" | \ - cut -b 44- | \ - cut -d '"' -f 1 | \ - egrep -v ^$ - ;; - "getArtist") - mpris2_dbus_get_player_property 'Metadata' | \ - -A 2 "artist" | \ - egrep -v "artist" | \ - egrep -v "array" | \ - cut -b 27- | \ - cut -d '"' -f 1 | \ - egrep -v ^$ - ;; - "getAlbum") - mpris2_dbus_get_player_property 'Metadata' | \ - egrep -A 2 "album" | \ - egrep -v "album" | \ - egrep -v "array" | \ - cut -b 44- | \ - cut -d '"' -f 1 | \ - egrep -v ^$ - ;; - "getStatus") - mpris2_dbus_get_player_property 'PlaybackStatus' | \ - grep 'string "[^"]*"' | \ - sed 's/.*"\(.*\)"[^"]*$/\1/' - ;; - *) - echo "Unknown command: " $1 - ;; -esac diff --git a/extras/HOME/.xmobarrc b/extras/HOME/.xmobarrc index ffe7957..62efc0a 100644 --- a/extras/HOME/.xmobarrc +++ b/extras/HOME/.xmobarrc @@ -34,7 +34,7 @@ Config \%UnsafeStdinReader%}{\ \\ \\ - \%weather% \ + \| %weather% \ \\ \%media%\ \%bluetooth% %bat% %cpu% %memory% %time% ", diff --git a/extras/HOME/.xmonad/xmobar-media b/extras/HOME/.xmonad/xmobar-media index 8855ea9..bdedc8d 100755 --- a/extras/HOME/.xmonad/xmobar-media +++ b/extras/HOME/.xmonad/xmobar-media @@ -1,6 +1,6 @@ #!/bin/bash -title="$(spotify-control getTitle)" +title="$(media-control getTitle)" ntitle=${#title} if [[ "$ntitle" -gt 40 ]] ; then title="${title:0:36} ..." diff --git a/src/Rahm/Desktop/Keys.hs b/src/Rahm/Desktop/Keys.hs index b136c62..c0d9c3a 100644 --- a/src/Rahm/Desktop/Keys.hs +++ b/src/Rahm/Desktop/Keys.hs @@ -173,11 +173,11 @@ decreaseVolume = spawnX "pactl set-sink-volume @DEFAULT_SINK@ -5%" increaseVolume = spawnX "pactl set-sink-volume @DEFAULT_SINK@ +5%" -playPause = spawnX "spotify-control play" +playPause = spawnX "media-control play" -mediaPrev = spawnX "spotify-control prev" +mediaPrev = spawnX "media-control prev" -mediaNext = spawnX "spotify-control next" +mediaNext = spawnX "media-control next" decreaseVolumeDoc = doc "Decrease volume" decreaseVolume diff --git a/xmobar/extras/weather/src/Main.hs b/xmobar/extras/weather/src/Main.hs index aa5a408..ccabc21 100644 --- a/xmobar/extras/weather/src/Main.hs +++ b/xmobar/extras/weather/src/Main.hs @@ -11,6 +11,7 @@ import Data.Aeson import Data.Aeson.Types (Parser) import qualified Data.ByteString.Lazy.Char8 import qualified Data.Char +import Data.List (isInfixOf) import qualified Data.Map import Data.Maybe (fromMaybe) import qualified Data.Text @@ -22,21 +23,21 @@ import Text.Printf (printf) -- Date time that easily comparable. data SillyTime = SillyTime - { sillyTimeDate :: String, - sillyTimeAMPM :: String, - sillyTimeTime :: String + { sillyTimeDate :: !String, + sillyTimeAMPM :: !String, + sillyTimeTime :: !String } deriving (Ord, Eq, Show) data CurrentCondition = CurrentCondition - { feelsLikeF :: String, - cloudCover :: String, - humidity :: String, - tempF :: String, - weatherDesc :: String, - windspeedMiles :: String, - winddir :: String, - observationTime :: SillyTime + { feelsLikeF :: !String, + cloudCover :: !String, + humidity :: !String, + tempF :: !String, + weatherDesc :: !String, + windspeedMiles :: !String, + winddir :: !String, + observationTime :: !SillyTime } deriving (Generic, Show) @@ -46,15 +47,15 @@ newtype NearestArea = NearestArea deriving (Generic, Show) data Astronomy = Astronomy - { sunrise :: SillyTime, - sunset :: SillyTime + { sunrise :: !SillyTime, + sunset :: !SillyTime } deriving (Generic, Show) data Weather = Weather - { currentCondition :: CurrentCondition, - nearestArea :: NearestArea, - astronomy :: Astronomy + { currentCondition :: !CurrentCondition, + nearestArea :: !NearestArea, + astronomy :: !Astronomy } deriving (Generic, Show) @@ -118,39 +119,38 @@ instance FromJSON Weather where =<< getFirst (v .: "weather") ) -conditionsIconDay :: Data.Map.Map String String +conditionsIconDay :: [(String -> Bool, String)] conditionsIconDay = - Data.Map.fromList - [ ("overcast", fc "#808080" "\63070"), - ("fair", fc "a0a0a0" "šŸŒ‘"), - ("clear", fc "#ddcf04" "\58125"), - ("sunny", fc "#ddcf04" "\58125"), - ("mostly clear", fc "#00a3c4" "\57894"), - ("mostly sunny", fc "#ddcf04" "\58124"), - ("partly sunny", fc "#ddcf04" "\58124"), - ("fair", fc "#a0a0a0" "\127761"), - ("cloudy", fc "#a0a0a0" "\64143"), - ("overcast", fc "#808080" "\63070"), - ("partly cloudy", fc "#a0a0a0" "\64148"), - ("mostly cloudy", fc "#808080" "\63070"), - ("considerable cloudiness", fc "#a0a0a0" "\64381") - ] - -conditionsIconNight :: Data.Map.Map String String + [ ((== "overcast"), fc "#808080" "\63070"), + ((== "fair"), fc "a0a0a0" "šŸŒ‘"), + ((== "clear"), fc "#ddcf04" "\58125"), + ((== "sunny"), fc "#ddcf04" "\58125"), + ((== "mostly clear"), fc "#00a3c4" "\57894"), + ((== "mostly sunny"), fc "#ddcf04" "\58124"), + ((== "partly sunny"), fc "#ddcf04" "\58124"), + ((== "fair"), fc "#a0a0a0" "\127761"), + ((== "cloudy"), fc "#a0a0a0" "\64143"), + ((== "overcast"), fc "#808080" "\63070"), + ((== "partly cloudy"), fc "#a0a0a0" "\64148"), + ((== "mostly cloudy"), fc "#808080" "\63070"), + ((== "considerable cloudiness"), fc "#a0a0a0" "\64381"), + (("snow" `isInfixOf`), fc "#a0a0f0" "\58138") + ] + +conditionsIconNight :: [(String -> Bool, String)] conditionsIconNight = - Data.Map.fromList - [ ("clear", fc "#00a3c4" "\61830"), - ("sunny", fc "#00a3c4" "\61830"), - ("mostly clear", fc "#00a3c4" "\57894"), - ("mostly sunny", fc "#00a3c4" "\57894"), - ("partly sunny", fc "#00a3c4" "\57894"), - ("fair", fc "#808080" "\127761"), - ("cloudy", fc "#808080" "\64143"), - ("overcast", fc "#404040" "\63070"), - ("partly cloudy", fc "#a0a0a0" "\57894"), - ("mostly cloudy", fc "#808080" "\63070"), - ("considerable cloudiness", fc "#a0a0a0" "\64381") - ] + [ ((== "clear"), fc "#00a3c4" "\61830"), + ((== "sunny"), fc "#00a3c4" "\61830"), + ((== "mostly clear"), fc "#00a3c4" "\57894"), + ((== "mostly sunny"), fc "#00a3c4" "\57894"), + ((== "partly sunny"), fc "#00a3c4" "\57894"), + ((== "fair"), fc "#808080" "\127761"), + ((== "cloudy"), fc "#808080" "\64143"), + ((== "overcast"), fc "#404040" "\63070"), + ((== "partly cloudy"), fc "#a0a0a0" "\57894"), + ((== "mostly cloudy"), fc "#808080" "\63070"), + ((== "considerable cloudiness"), fc "#a0a0a0" "\64381") + ] handleWeather :: Weather -> String handleWeather w = execWriter $ do @@ -186,7 +186,7 @@ handleWeather w = execWriter $ do tell $ fn 5 $ fromMaybe "?" $ - Data.Map.lookup (map Data.Char.toLower $ weatherDesc (currentCondition w)) conditions + findMatch (map Data.Char.toLower $ weatherDesc (currentCondition w)) conditions tell " " tell $ lightGrey $ fn 3 $ printf "%s°F" (tempF $ currentCondition w) @@ -203,6 +203,11 @@ handleWeather w = execWriter $ do fc :: String -> String -> String fc = printf "%s" +findMatch :: a -> [(a -> Bool, b)] -> Maybe b +findMatch a ((f, b) : fs) | f a = Just b +findMatch a (_ : fs) = findMatch a fs +findMatch _ [] = Nothing + main :: IO () main = do (code, resp) <- curlGetString "https://wttr.in?format=j2" [] -- cgit