Skip to content

Commit 745f780

Browse files
committed
use ggrep on macos
handle missing xdg-user-dir
1 parent 76b605f commit 745f780

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

impd

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,14 @@ fetch_title() {
240240
echo "$title"
241241
}
242242

243+
_grep() {
244+
if command -v ggrep >/dev/null 2>&1; then
245+
ggrep "$@"
246+
else
247+
grep "$@"
248+
fi
249+
}
250+
243251
fetch_episode_number() {
244252
local -r file=${1:?}
245253
{
@@ -421,7 +429,7 @@ find_external_subtitles() {
421429

422430
if [[ -z $dir ]] || [[ ! -d $dir ]]; then
423431
echo "Error: video directory '$dir' is invalid." >&2
424-
exit 1
432+
return 1
425433
fi
426434

427435
find "$dir" \
@@ -957,11 +965,15 @@ mkplaylist() {
957965

958966
grep_mpd_dir() {
959967
# https://wiki.archlinux.org/index.php/Music_Player_Daemon#Configuration
960-
if command -v ggrep >/dev/null; then
961-
ggrep -Pos 'music_directory\s*"?\K[^"]*(?="?)' -- ~/.config/mpd/mpd.conf 2>/dev/null
968+
_grep -Pos 'music_directory\s*"?\K[^"]*(?="?)' -- ~/.config/mpd/mpd.conf 2>/dev/null
969+
}
970+
971+
find_xdg_music_dir() {
972+
# macOS doesn't have xdg-user-dir, so we'll use ~/Music as the default
973+
if command -v xdg-user-dir >/dev/null 2>&1; then
974+
xdg-user-dir MUSIC 2>/dev/null
962975
else
963-
# Fallback for macOS - extract music directory path from mpd.conf
964-
grep 'music_directory' ~/.config/mpd/mpd.conf 2>/dev/null | sed -E 's/.*music_directory[[:space:]]*"?([^"]*).*/\1/'
976+
echo "$HOME/Music"
965977
fi
966978
}
967979

@@ -1090,7 +1102,16 @@ ensure_dirs() {
10901102
"$tmp_dir"
10911103
}
10921104

1105+
check_macos_grep() {
1106+
# if running macOS, ggrep must be installed to support Perl-compatible regular expressions.
1107+
if [[ "$(uname)" == "Darwin" ]] && ! command -v ggrep >/dev/null 2>&1; then
1108+
echo "ggrep is not installed. install it with brew." >&2
1109+
exit 1
1110+
fi
1111+
}
1112+
10931113
main() {
1114+
check_macos_grep
10941115
# Load configuration file, then apply settings omitted in the config file.
10951116
read_config_file
10961117

0 commit comments

Comments
 (0)