Commit e7450303 authored by Benedikt Vollmerhaus's avatar Benedikt Vollmerhaus

Add energy consumption and playback status script to Polybar

parent e03b6fd8
......@@ -26,7 +26,7 @@ color8 = ${xrdb:color8:#373b41}
[color]
base-bg = #8c0a0a0a
glyph-bg = #b40a0a0a
glyph-bg = #be0a0a0a
module-bg = #dc0a0a0a
selected-bg = #dc1a1a1a
......@@ -93,7 +93,7 @@ font-4 = mplus Nerd Font:style=Medium:size=7;3
[bar/top-primary]
inherit = bar/base
modules-left = volume right-end-top
modules-left = now-playing volume right-end-top
modules-center = left-end-bottom date right-end-top
modules-right = left-end-top wlan eth filesystem battery
......@@ -118,7 +118,7 @@ inherit = bar/base
bottom = true
modules-left = i3 right-end-bottom left-end-top xwindow right-end-bottom
modules-right = left-end-bottom wlan-netspeed eth-netspeed cpu memory temp
modules-right = left-end-bottom wlan-netspeed eth-netspeed cpu memory temp consumption
[bar/bottom-secondary]
......@@ -422,6 +422,18 @@ label = %date% %time%
; | |(/_(_||(_|
;
[module/now-playing]
type = custom/script
inherit = generic-format-style
exec = $HOME/.config/polybar/now_playing.py
tail = true
click-left = playerctl next
click-middle = playerctl play-pause
click-right = playerctl previous
[module/volume]
type = internal/alsa
......@@ -524,6 +536,36 @@ format-full-prefix-padding-right = ${layout.icon-padding}
label-full = ${self.label-discharging}
[module/consumption]
type = internal/battery
inherit = module/battery
; Discharging
format-discharging = <label-discharging>
format-discharging-prefix =
format-discharging-prefix-font = ${layout.icon-font}
format-discharging-prefix-foreground = ${color.icon-fg}
format-discharging-prefix-padding-right = ${layout.icon-padding}
label-discharging = -%consumption% W
; Charging
format-charging = <label-charging>
format-charging-overline =
format-charging-underline = ${xcolor.color5}
format-charging-prefix = ${self.format-discharging-prefix}
format-charging-prefix-font = ${layout.icon-font}
format-charging-prefix-foreground = ${color.icon-fg}
format-charging-prefix-padding-right = ${layout.icon-padding}
label-charging = +%consumption% W
; Full
format-full =
; __ __
; |__) _ _ (_ _ _|__|_. _ _ _
; |__)(_|| __)(/_ | | || |(_|_)
......
#!/usr/bin/env python3
"""
A script for Polybar which listens to an MPRIS-compatible media player's
events and outputs the current playback status, i.e., the current track.
This rudimentary quick-and-dirty script should only be temporary since a
native MPRIS/Playerctl module for Polybar is planned for the near future.
Required Python packages:
- PyGObject (https://pygobject.readthedocs.io/en/latest/)
Required applications:
- playerctl (https://github.com/acrisci/playerctl)
Author: Benedikt Vollmerhaus
License: MIT
"""
import sys
import time
import gi
gi.require_version('Playerctl', '1.0')
from gi.repository import Playerctl, GLib
class NowPlaying(object):
"""
Retrieve and print the metadata of the currently playing track.
"""
ARTIST = 'xesam:artist'
TITLE = 'xesam:title'
def __init__(self) -> None:
self.player = None
self.icon: str = '栗'
self.status: str = ''
self.artist: str = ''
self.title: str = ''
def listen(self) -> None:
"""
Start the main event loop once a player has been launched.
:return: None
"""
self.wait_for_player()
main = GLib.MainLoop()
main.run()
def wait_for_player(self) -> None:
"""
Wait for an MPRIS-compatible player and bind its events.
:return: None
"""
while True:
try:
self.player = Playerctl.Player()
self.player.on('play', self.on_play)
self.player.on('pause', self.on_pause)
self.player.on('metadata', self.on_metadata)
self.player.on('exit', self.on_exit)
print('%{T2}栗%{T-} Stopped', flush=True)
break
except GLib.Error:
print('%{T2}ﱘ%{T-} No player running', flush=True)
time.sleep(2) # Wait before searching again
def on_play(self, _player) -> None:
"""
:param _player: The player that started playing
:return: None
"""
self.icon = '契'
self.print_status()
def on_pause(self, _player) -> None:
"""
:param _player: The player that got paused
:return: None
"""
self.icon = ''
self.print_status()
def on_metadata(self, _player, data) -> None:
"""
:param _player: The player whose metadata to display
:param data: The currently playing track's metadata
:return: None
"""
if self.ARTIST in data.keys() and self.TITLE in data.keys():
self.artist = data[self.ARTIST][0]
self.title = data[self.TITLE]
self.print_status()
def on_exit(self, _player) -> None:
"""
:param _player: The player that got closed
:return: None
"""
self.wait_for_player()
def print_status(self) -> None:
"""
Print the status icon, currently playing track and its artist.
:return: None
"""
# Flushing the buffer forces Python to write to stdout. This is
# required due to the script being continuously tail'ed through
# Polybar, which would otherwise lead to not getting any output.
print(f'%{{T2}}{self.icon}%{{T-}} {self.artist} - {self.title}',
flush=True)
if __name__ == '__main__':
try:
NowPlaying().listen()
except KeyboardInterrupt:
print('\rBye!')
sys.exit(0)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment