summaryrefslogtreecommitdiff
path: root/ar/.local/bin/mpdmenu
blob: 126a876f0f90a8b808c88698d89382770864ba27 (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
111
112
113
#!/bin/bash

all_name='[ALL]'
playlist_dir="${XDG_CONFIG_HOME:-${HOME}/.config}/mpd/playlists"

# Functions
d_artist() {
  mpc list artist | sort -f | dmenu -i -p artist "${dmenu_args[@]}" || exit
}

d_album() {
  local artist="$1"
  local albums

  mapfile -t albums < <(mpc list album artist "$artist")
  if ((${#albums[@]} > 1)); then
    {
      printf '%s\n' "$all_name"
      printf '%s\n' "${albums[@]}" | sort -f
    } | dmenu -i -p album "${dmenu_args[@]}" || exit
  else
    # We only have one album, so just use that.
    printf '%s\n' "${albums[0]}"
  fi
}

d_title() {
  titles=$(mpc list title | sort -f)
  [[ -z "$titles" ]] && exit
  echo "$titles" | dmenu -i -p "Select title:" "${dmenu_args[@]}"
}

d_playlist() {
  playlists=$(find "$playlist_dir" \( -type f -o -type l \) -name "*.m3u" -exec basename {} .m3u \;)

  selected_playlist=$(echo "$playlists" | sort | dmenu -i -p "Select Playlist:" "${dmenu_args[@]}")

  printf '%s' "$selected_playlist"
}

d_queue() {
  format="%position% %title%"
  extra_format="(%artist% - %album%)"

  # If all tracks are from the same artist and album, no need to display that
  num_extras=$(mpc playlist -f "$extra_format" | sort | uniq | wc -l)
  ((num_extras == 1)) || format+=" $extra_format"

  track=$(mpc playlist -f "$format" | dmenu -i -p track "${dmenu_args[@]}")
  printf '%s' "${track%% *}"
}

# Parse arguments (if any)
i=2
for arg in "$@"; do
  if [[ $arg == :: ]]; then
    dmenu_args=("${@:$i}")
    break
  fi

  case "$arg" in
  -l) mode="library" ;;
  -p) mode="playlist" ;;
  -q) mode="queue" ;;
  esac

  ((i + 1))
done

# Main Menu
[[ -z "$mode" ]] && mode=$(echo -e "library\nplaylist\nqueue" | dmenu -i -p "Choose mode:" "${dmenu_args[@]}")

# Mode Handling
case "$mode" in
"library")
  search_type=$(echo -e "artist\ntitle" | dmenu -i -p "Search by:" "${dmenu_args[@]}")
  case "$search_type" in
  "artist")
    artist=$(d_artist)
    album=$(d_album "$artist")
    mpc clear
    if [[ $album == "$all_name" ]]; then
      mpc find artist "$artist" | sort | mpc add
    else
      mpc find artist "$artist" album "$album" | sort | mpc add
    fi
    ;;
  "title")
    title=$(d_title)
    mpc clear
    mpc find title "$title" | sort | mpc add
    ;;
  *) exit ;;
  esac
  mpc random on
  mpc repeat on
  mpc play 2>/dev/null
  ;;
"playlist")
  mpc clear
  mpc load "$(d_playlist)" 2>/dev/null || exit
  mpc random on
  mpc repeat on
  mpc play 2>/dev/null
  ;;
"queue")
  ! mpc playlist | grep -q '.' && exit
  mpc play "$(d_queue)" 2>/dev/null || exit
  ;;
*) exit ;;
esac

sleep 0.5 && mpc volume 50