DoubleA4 / rofi-playerctl-switcher

simple rofi script to choose player for playerctl
2 stars 0 forks source link

Avoid using `player` auxiliary file #1

Closed marcbenedi closed 2 years ago

marcbenedi commented 2 years ago

Hey! I updated your playerSwitch.py such that it doesn't need to create an auxiliary file to select the player. This has the advantage (in my opinion) of not needing the player.sh and changing the configuration of i3.

The updated playerSwitch.py looks like this:

import os
import subprocess

# config
path = "~/.config/i3/scripts"
rofiOption = ""

# get list of players
players = subprocess.run(['playerctl', '-l'], capture_output=True, text=True).stdout.split("\n")
players.pop(len(players)-1)

# create class
class playersData:
    def __init__(self, display, command):
        self.display = display
        self.command = command

# create list and input players info into class of it's own
list = []
for i, player in enumerate(players):
    info = subprocess.run(['playerctl', '-p', player, 'metadata', '--format', "({{ artist }} - {{ title }})"], capture_output=True, text=True).stdout[:-1]
    statusGet = subprocess.run(['playerctl', '-p', player, 'status'], capture_output=True, text=True).stdout[:-1]
    selected = '✅ ' if i == 0 else ''
    if statusGet == 'Playing':
        status = ""
    else:
        status = ""
    list.append( playersData(selected +  status + " " + player.split(".")[0] + " " + info, player.strip()) )

# preparing menu
menuArray = []
for obj in list:
    menuArray.append(obj.display)
c = '"'+"\n".join(menuArray)+'"'

# displaying menu
choice = os.popen('echo ' + c + ' | rofi -dmenu -i -p "Select Players" ' + rofiOption).read()[:-1]

for i, obj in enumerate(list):
    if obj.display == choice:
        for j in range(i):
            os.system("playerctld shift")

I hope it is useful!

DoubleA4 commented 2 years ago

i never knew playerctld existed, i've updated the code according to your issue