From 0f689b733d401d7cf5c146be0436702a06a46194 Mon Sep 17 00:00:00 2001 From: Sibren Vasse Date: Sun, 31 Mar 2019 16:12:38 +0200 Subject: [PATCH] feat(custom): make spotify module generic Set class via json attribute Choose icon via alt attribute --- resources/config | 11 ++++++++--- resources/custom_modules/mediaplayer.py | 24 +++++++++++++++--------- resources/style.css | 12 ++++++++++-- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/resources/config b/resources/config index 06992478..8b4a4be6 100644 --- a/resources/config +++ b/resources/config @@ -4,7 +4,7 @@ // "height": 30, // Waybar height // "width": 1280, // Waybar width // Choose the order of the modules - "modules-left": ["sway/workspaces", "sway/mode", "custom/spotify"], + "modules-left": ["sway/workspaces", "sway/mode", "custom/media"], "modules-center": ["sway/window"], "modules-right": ["idle_inhibitor", "pulseaudio", "network", "cpu", "memory", "temperature", "backlight", "battery", "battery#bat2", "clock", "tray"], // Modules configuration @@ -95,9 +95,14 @@ }, "on-click": "pavucontrol" }, - "custom/spotify": { - "format": " {}", + "custom/media": { + "format": "{icon} {}", + "return-type": "json", "max-length": 40, + "format-icons": { + "spotify": "", + "default": "🎜" + }, "escape": true, "exec": "$HOME/.config/waybar/mediaplayer.py 2> /dev/null" // Script in resources folder } diff --git a/resources/custom_modules/mediaplayer.py b/resources/custom_modules/mediaplayer.py index 0aaa412b..f6171b13 100755 --- a/resources/custom_modules/mediaplayer.py +++ b/resources/custom_modules/mediaplayer.py @@ -4,12 +4,24 @@ import logging import sys import signal import gi +import json gi.require_version('Playerctl', '2.0') from gi.repository import Playerctl, GLib logger = logging.getLogger(__name__) +def write_output(text, player): + logger.info('Writing output') + + output = {'text': text, + 'class': 'custom-' + player.props.player_name, + 'alt': player.props.player_name} + + sys.stdout.write(json.dumps(output) + '\n') + sys.stdout.flush() + + def on_play(player, status, manager): logger.info('Received new playback status') on_metadata(player, player.props.metadata, manager) @@ -26,16 +38,10 @@ def on_metadata(player, metadata, manager): elif player.get_artist() != '' and player.get_title() != '': track_info = '{artist} - {title}'.format(artist=player.get_artist(), title=player.get_title()) - else: - sys.stdout.write('\n') - sys.stdout.flush() - return - if player.props.status == 'Playing': - sys.stdout.write(track_info + '\n') - else: - sys.stdout.write(' ' + track_info + '\n') - sys.stdout.flush() + if player.props.status != 'Playing': + track_info = ' ' + track_info + write_output(track_info, player) def on_player_appeared(manager, player, selected_player=None): diff --git a/resources/style.css b/resources/style.css index 3e959e03..362daab6 100644 --- a/resources/style.css +++ b/resources/style.css @@ -30,7 +30,7 @@ window#waybar { border-bottom: 3px solid #ffffff; } -#clock, #battery, #cpu, #memory, #temperature, #backlight, #network, #pulseaudio, #custom-spotify, #tray, #mode, #idle_inhibitor { +#clock, #battery, #cpu, #memory, #temperature, #backlight, #network, #pulseaudio, #custom-media, #tray, #mode, #idle_inhibitor { padding: 0 10px; margin: 0 5px; } @@ -97,11 +97,19 @@ window#waybar { color: #2a5c45; } -#custom-spotify { +#custom-media { background: #66cc99; color: #2a5c45; } +.custom-spotify { + background: #66cc99; +} + +.custom-vlc { + background: #ffa000; +} + #temperature { background: #f0932b; }