summaryrefslogtreecommitdiff
path: root/.local/bin/pctl_status
blob: bc6af6e4b0a2d1eddcf228b4ba0c9dea43cc3e21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/bin/sh

get_pctl_status () {
	player="$(check_for_mpd)"

	if [ "$player" = "" ]; then
		playerctl_status=$(playerctl status 2>/dev/null)
	else
		playerctl_status=$(playerctl -p "$player" status 2>/dev/null)
	fi
	exit_code=$?

	if [ $exit_code -eq 0 ]; then
		echo "$playerctl_status"
	else
		echo "NPF"
	fi
}

check_for_mpd () {
	pctl_players="$(playerctl -l)"

	while IFS= read line; do
		line="$(echo $line | sed -e 's/\ //')"

		if [ "$line" = "mpd" ]; then
			echo "$line"
		fi
	done <<EOF
	$pctl_players
EOF
}

get_display_text () {
	player="$(check_for_mpd)"
	status="$(get_pctl_status "$player")"

	if [ "$player" = "mpd" ]; then
		if [ "$status" = "Stopped" ]; then
			echo "%{F$(xresource color8)}No music playing%{F}"
		elif [ "$status" = "Paused" ]; then
			artist="$(mpc --format '[%albumartist%]|[%artist%]' current)"
			title="$(mpc --format '%title%' current)"

			echo "%{F$(xresource color8)}$artist - $title%{F}"
		elif [ "$status" = "NPF" ]; then
			echo "%{F$(xresource color8)}No player found%{F}"
		else
			artist="$(mpc --format '[%albumartist%]|[%artist%]' current)"
			title="$(mpc --format '%title%' current)"

			echo "$artist - $title"
		fi
	else
		if [ "$status" = "Stopped" ]; then
			echo "%{F$(xresource color8)}No music playing%{F}"
		elif [ "$status" = "Paused" ]; then
			artist="$(playerctl metadata artist)"
			title="$(playerctl metadata title)"

			echo "%{F$(xresource color8)}$artist - $title%{F}"
		elif [ "$status" = "NPF" ]; then
			echo "%{F$(xresource color8)}No player found%{F}"
		else
			artist="$(playerctl metadata artist)"
			title="$(playerctl metadata title)"

			echo "$artist - $title"
		fi
	fi
}

get_metadata () {
	player="$(check_for_mpd)"
	status="$(get_pctl_status "$player")"

	if [ "$player" = "mpd" ]; then
		if [ "$status" = "Playing" ] || [ "$status" = "Paused" ]; then
			artist="$(mpc --format '[%albumartist%]|[%artist%]' current)"
			album="$(mpc --format '%album%' current)"
			title="$(mpc --format '%title%' current)"

			echo "yes>$artist>$album>$title"
		fi
	else
		if [ "$status" = "Playing" ] || [ "$status" = "Paused" ]; then
			artist="$(playerctl metadata artist)"
			album="$(playerctl metadata album)"
			title="$(playerctl metadata title)"

			if [ "$album" = "" ]; then
				echo "no>$artist>$title"
			else
				echo "yes>$artist>$album>$title"
			fi
		fi
	fi
}

case $1 in
	status)
		get_pctl_status
		;;
	text)
		get_display_text
		;;
	get-metadata)
		get_metadata
		;;
esac