This repository was archived by the owner on Oct 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdmenu_player
More file actions
208 lines (188 loc) · 4.91 KB
/
dmenu_player
File metadata and controls
208 lines (188 loc) · 4.91 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/env bash
# TODO:
# Dmenu_player cria um menu interativo para busca e interação básica com arquivos de música (dentro da pasta ~/Música/)
# Músicas que tenham o mesmo nome irão entrar em conflito , não tocando nenhuma delas - SOLUÇÃO renomear de forma diferente ;P
# Utiliza dmenu modificado com patches
init_variables(){
SCRIPT_NAME=$(basename "$BASH_ARGV0")
CMD_FIFO_FILE="/tmp/mplayer-fifo"
PLAY=(mplayer -really-quiet -quiet -slave -input file="$CMD_FIFO_FILE")
# Arquivos usados
PLAYER_DIR="$HOME/.local/share/$SCRIPT_NAME"
MUSIC_LIST="$PLAYER_DIR/lista_de_musicas_completa.dmenu"
IS_NEXT_SONG="$PLAYER_DIR/is_next_song.dmenu"
MODULO="$PLAYER_DIR/modulo.dmenu"
MUSIC_PATHS="$PLAYER_DIR/pastas_de_musicas.dmenu"
REPEAT="$PLAYER_DIR/repeat.dmenu"
PAUSED="$PLAYER_DIR/paused.dmenu"
# Keys
KEY_PLAY=p
KEY_STOP=s
KEY_REPEAT=r
KEY_KILL_MUSIC=x
KEY_NEXT=n
KEY_PREVIOUS=b
KEY_PLAYLIST=q
}
update_songs_list() {
local MUSIC_DIR="$HOME/Música/"
# Captura todas as músicas nos formatos flac, ogg, mp3
du -a "$MUSIC_DIR" | cut -f2 | grep --color=never ".flac\|.mp3\|.ogg\|.oga" > "$MUSIC_LIST"
# Captura todas as pastas dentro da pasta ~/Música/
du "$MUSIC_DIR"* | cut -f2 > "$MUSIC_PATHS"
}
redraw_statusbar(){
polybar-msg hook music_playing 1
}
# $1=MUSICA
player() {
redraw_statusbar
"${PLAY[@]}" "$1"
while (( $(< "$REPEAT") ))
do
"${PLAY[@]}" "$1"
done
}
pause_song(){
echo "pause" > "$CMD_FIFO_FILE"
toggle "$PAUSED"
}
reset_to_next_song(){
echo '1' > "$IS_NEXT_SONG"
}
player_playlist(){
local songs=("$@")
local index=0
reset_to_next_song
while (( $index < ${#songs[@]} ))
do
player "${songs[$index]}"
# 0=false; other value=true
if (( $(< "$IS_NEXT_SONG") ))
then
((index++))
else
((index--))
reset_to_next_song
fi
done
}
toggle(){
((i=$(< "$1")>0?0:1))
echo "$i" > "$1"
redraw_statusbar
}
reset_statusbar(){
echo '0' > "$REPEAT"
echo '0' > "$PAUSED"
redraw_statusbar
}
# $1=ARQUIVO_LISTA; $2=MÓDULO
playlist_queue(){
PLAYER_MENU_ROWS=10
echo "$2" > "$MODULO"
echo '0' > "$REPEAT"
# Sempre observar a necessidade de usar IFS (valor padrão <space><tab><newline>) quando criar listas de comandos
local IFS=$'\n'
local songs_list=($(shuf < "$1"))
local choose=
local songs=()
if (( $2 ))
then
choose=$(player_command_menu "Escolha uma música:" "${songs_list[@]##*/}")
# Se não for escolhida nenhuma música, encerrar o script
[[ -z $choose ]] && exit 0
songs+=($(grep --color=never "$choose$" "$1"))
player "${songs[@]}"
else
choose=$(player_command_menu "Escolha uma das pastas de música:" "${songs_list[@]##*/}")
# Se nenhuma playlist for escolhida, encerrar o script
[[ -n $choose ]] && notify-send -t 6000 "${SCRIPT_NAME^^}" "Escutando músicas da pasta: $choose" || exit 0
songs+=($(shuf < <(grep --color=never "$choose/" "$MUSIC_LIST")))
player_playlist "${songs[@]}"
fi
redraw_statusbar
}
# $1=WHAT_TO_KILL
# 0=script; other_value=song
process_kill(){
if (( $1 ))
then
pkill -f "^${PLAY[0]}" &
else
pkill -f "^${PLAY[0]}|^bash.*${SCRIPT_NAME}$" &
fi
reset_statusbar
}
# 1=PROXIMA MÚSICA; 0=MÚSICA ANTERIOR
# kill song
tracking_music(){
reset_statusbar
[[ $(< "$IS_NEXT_SONG") != $1 ]] && echo "$1" > "$IS_NEXT_SONG"
process_kill "1"
}
init_files(){
# Criando arquivo e diretórios
[[ ! -p $CMD_FIFO_FILE ]] && mkfifo "$CMD_FIFO_FILE"
[[ ! -d $PLAYER_DIR ]] && mkdir "$PLAYER_DIR"
[[ ! -e $MUSIC_LIST ]] && update_songs_list
}
run_player(){
dmenu -i -f -l "$PLAYER_MENU_ROWS" -fn "Source Code Pro Medium:size=14"
}
player_command_menu(){
printf '%s\n' "$@" | run_player
}
player_options(){
if [[ -z ${MUSIC_PLAYING:=$(pgrep -a "^${PLAY[0]}")} ]]
then
PLAYER_MENU=("[1] - [Procurar por Música]" \
"[2] - [Escolher por Músicas por Pasta]" \
"[3] - [Atualizar lista de Músicas]")
else
if (( $(< "$MODULO") ))
then
PLAYER_MENU=("[$KEY_PLAY/$KEY_STOP] - [Play!/Stop!]" \
"[$KEY_REPEAT] - [Repeat!]" \
"[$KEY_KILL_MUSIC] - [Parar Música!]")
else
PLAYER_MENU=("[$KEY_PLAY/$KEY_STOP] - [Play!/Stop!]" \
"[$KEY_REPEAT] - [Repeat!]" \
"[$KEY_NEXT] - [Próxima Música!]" \
"[$KEY_PREVIOUS] - [Música Anterior!]" \
"[$KEY_PLAYLIST] - [Encerrar Playlist!]")
fi
fi
PLAYER_MENU_ROWS=$(( ${#PLAYER_MENU[@]} + 1 ))
}
main_menu(){
player_options
PLAYER_MAIN_MENU=$(player_command_menu "${SCRIPT_NAME^^} - MENU:" "${PLAYER_MENU[@]}")
[[ -z $PLAYER_MAIN_MENU ]] && exit 0
case "${PLAYER_MAIN_MENU:1:1}" in
(1)
playlist_queue "$MUSIC_LIST" "1";;
(2)
playlist_queue "$MUSIC_PATHS" "0";;
(3)
update_songs_list;;
($KEY_PLAY|$KEY_STOP)
pause_song;;
($KEY_REPEAT)
toggle "$REPEAT";;
($KEY_PLAYLIST)
process_kill "0";;
($KEY_KILL_MUSIC|$KEY_NEXT)
tracking_music "1";;
($KEY_PREVIOUS)
tracking_music "0";;
(*)
notify-send -u critical -t 3250 "${SCRIPT_NAME^^}" "Comando não reconhecido!" & exit 0;;
esac
}
main(){
init_variables
init_files
main_menu
}
main