doctorfree / MusicPlayerPlus

Featureful ncurses based MPD client inspired by ncmpc with integration for Beets, spectrum visualization,Bandcamp/Soundcloud, asciimatics, cantata, and more
https://musicplayerplus.dev
MIT License
69 stars 1 forks source link

[FEATURE] Use pre-existing ncmpcpp user configuration where possible #5

Closed doctorfree closed 1 year ago

doctorfree commented 1 year ago

MusicPlayerPlus installs an enhanced fork of the character based MPD client ncmpcpp called mpcplus. The mpcplus client has its own configuration file ~/.config/mpcplus/config which is a customized version of a vanilla ncmpcpp config. If the user has previously installed ncmpcpp and applied user customized settings to the ncmpcpp configuration, these are not applied to mpcplus. The user has to manually apply these preferences to the mpcplus config.

The desired feature request is for MusicPlayerPlus to detect an existing ncmpcpp config file and apply any user customized preferences to the mpcplus config.

doctorfree commented 1 year ago

One approach to detect ncmpcpp user customizations would be to diff ~/.config/ncmpcpp/config with /usr/share/doc/ncmpcpp/config. Also diff ~/.config/ncmpcpp/bindings with /usr/share/doc/ncmpcpp/bindings.

doctorfree commented 1 year ago

First crack at a script to merge user customized ncmpcpp config settings with ~/.config/npcplus/config:

#!/bin/bash
#
# set_prev_ncmpcpp.sh - retrieve previously configured user preferences
#                       for MusicPlayerPlus components

USERCONF="${HOME}/.config"
NCM_USER_CONF="${USERCONF}/ncmpcpp/config"
MPC_USER_CONF="${USERCONF}/mpcplus/config"

# Syncing previous user configured ncmpcpp key bindings not yet implemented
NCM_USER_BIND="${USERCONF}/ncmpcpp/bindings"
MPC_USER_BIND="${USERCONF}/mpcplus/bindings"

excluded_ncm_opts=("mpd_music_dir" "visualizer_in_stereo" \
                   "visualizer_type" "message_delay_time")
previous_comments="## Previously configured ncmpcpp user preferences"

[ -f "${NCM_USER_BIND}" ] && {
  numb=`grep -v ^# "${NCM_USER_BIND}" | grep -v -e '^[[:space:]]*$' | wc -l`
  [ ${numb} -gt 0 ] && {
    if [ -f "${MPC_USER_BIND}" ]
    then
      printf "\nIntegration of previously configured ncmpcpp bindings is not"
      printf "yet implemented.\nA manual merge of the configured key bindings"
      printf "in:\n\n${NCM_USER_BIND} and ${MPC_USER_BIND}"
      printf "\n\nwill be necessary.\n"
    else
      [ -d "${USERCONF}/mpcplus" ] || mkdir -p "${USERCONF}/mpcplus"
      cp "${NCM_USER_BIND}" "${MPC_USER_BIND}"
    fi
  }
}

[ -f "${NCM_USER_CONF}" ] && [ -f "${MPC_USER_CONF}" ] && {
  grep "${previous_comments}" "${MPC_USER_CONF}" > /dev/null && exit 0
  grep -v ^# "${NCM_USER_CONF}" | while read ncmconf
  do
    option=`echo "${ncmconf}" | awk -F '=' ' { print $1 } '`
    option=`echo ${option} | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'`
    [[ " ${excluded_ncm_opts[*]} " =~ " ${option} " ]] && continue
    [ "${option}" ] && {
      oval=`echo "${ncmconf}" | awk -F '=' ' { print $2 } '`
      oval=`echo ${oval} | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'`
      [ "${oval}" ] && {
        mval=`grep "^${option} " "${MPC_USER_CONF}" | \
                awk -F '=' ' { print $2 } '`
        mval=`echo ${mval} | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'`
        [ "${mval}" == "${oval}" ] || {
          # Use the previously configured value for this option
          grep -v "^${option} " "${MPC_USER_CONF}" > /tmp/ncm$$
          grep "${previous_comments}" /tmp/ncm$$ > /dev/null || {
            echo "${previous_comments}" >> /tmp/ncm$$
          }
          echo "${ncmconf}" >> /tmp/ncm$$
          cp /tmp/ncm$$ "${MPC_USER_CONF}"
          rm -f /tmp/ncm$$
        }
      }
    }
  done
}

exit 0
doctorfree commented 1 year ago

Several configuration settings are required for cover art display. These along with the music library location and a few others cannot be modified and are not updated by a merge. Otherwise, this seems to work and I am closing this issue.

The script that merges any previous ncmpcpp user configuration currently looks like:

#!/bin/bash
#
# set_prev_ncmpcpp.sh - merge previously configured ncmpcpp user preferences
#                       with $HOME/.config/mpcplus/config

USERCONF="${HOME}/.config"
NCM_USER_CONF="${USERCONF}/ncmpcpp/config"
MPC_USER_CONF="${USERCONF}/mpcplus/config"
UEB_USER_CONF="${USERCONF}/mpcplus/ueberzug/config"

# Syncing previous user configured ncmpcpp key bindings not yet implemented
NCM_USER_BIND="${USERCONF}/ncmpcpp/bindings"
MPC_USER_BIND="${USERCONF}/mpcplus/bindings"

# Ignore any previous ncmpcpp setting for these options
excluded_ncm_opts=("mpd_music_dir" "visualizer_in_stereo" \
                   "visualizer_type" "message_delay_time" \
                   "visualizer_look" "execute_on_song_change" \
                   "startup_screen" "startup_slave_screen" \
                   "startup_slave_screen_focus" "locked_screen_width_part")
# Prepend this comment to any appended previous ncmpcpp settings
previous_comments="## Previously configured ncmpcpp user preferences"

[ -f "${NCM_USER_CONF}" ] && [ -f "${MPC_USER_CONF}" ] && {
  # Only allowed to be run once if changes to mpcplus/config were made
  grep "${previous_comments}" "${MPC_USER_CONF}" > /dev/null && exit 0
  grep -v ^# "${NCM_USER_CONF}" | while read ncmconf
  do
    option=`echo "${ncmconf}" | awk -F '=' ' { print $1 } '`
    option=`echo ${option} | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'`
    [[ " ${excluded_ncm_opts[*]} " =~ " ${option} " ]] && continue
    [ "${option}" ] && {
      oval=`echo "${ncmconf}" | awk -F '=' ' { print $2 } '`
      oval=`echo ${oval} | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'`
      [ "${oval}" ] && {
        mval=`grep "^${option} " "${MPC_USER_CONF}" | \
                awk -F '=' ' { print $2 } '`
        mval=`echo ${mval} | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'`
        [ "${mval}" == "${oval}" ] || {
          # Use the previously configured value for this option
          for userconf in ${MPC_USER_CONF} ${UEB_USER_CONF}
          do
            [ -f "${userconf}" ] && {
              cat "${userconf}" | \
                sed -e "s/^${option} /#${option} /" > /tmp/ncm$$
              grep "${previous_comments}" /tmp/ncm$$ > /dev/null || {
                echo "${previous_comments}" >> /tmp/ncm$$
              }
              echo "${ncmconf}" >> /tmp/ncm$$
              cp /tmp/ncm$$ "${userconf}"
              rm -f /tmp/ncm$$
            }
          done
        }
      }
    }
  done
}

[ -f "${NCM_USER_BIND}" ] && {
  numb=`grep -v ^# "${NCM_USER_BIND}" | grep -v -e '^[[:space:]]*$' | wc -l`
  [ ${numb} -gt 0 ] && {
    if [ -f "${MPC_USER_BIND}" ]
    then
      printf "\n\nIntegration of previously configured ncmpcpp bindings is not"
      printf " yet implemented.\nManually merge the configured key bindings in:"
      printf "\n\n\t${NCM_USER_BIND}\ninto\n\t${MPC_USER_BIND}\n"
    else
      [ -d "${USERCONF}/mpcplus" ] || mkdir -p "${USERCONF}/mpcplus"
      cp "${NCM_USER_BIND}" "${MPC_USER_BIND}"
    fi
  }
}

exit 0