Skip to content

Commit

Permalink
Add xresources config wrapper
Browse files Browse the repository at this point in the history
Created a wrapper script to customize theme variables (colors,
fonts, etc) according with your X resources file (~/.Xresources).

As dunst is not a X exclusive notification daemon, this script
could be a workarround to solve this.

Related with: dunst-project#357
  • Loading branch information
lulivi committed Jul 10, 2018
1 parent 74bbc96 commit 07ca494
Showing 1 changed file with 186 additions and 0 deletions.
186 changes: 186 additions & 0 deletions contrib/dunst_xr_theme_changer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
#!/usr/bin/env bash
###############################################################################
#
# This script creates a dunst user configuration file in
# $XDG_CONFIG_HOME/dunst/ folder, changing dunst basic theming options
# according to your current X resources color palette.
#
# It gets your current user configuration file
# -$HOME/.conf/dunst/dunstrc, which is copied from the default
# config file in /usr/share/dunst/dunstrc if it does not exist- and
# changes the defined attributes -declared further on in the script as
# $theme_attr_dict dictionary- to the new ones. Then it dumps the new
# configuration to $user_xr_color_conf file.
#
###############################################################################

set -e

# Check if appres is installed
if [ ! "$(command -v appres)" ]; then
printf 'xorg-appres is not installed.'
exit 1
fi

readonly script_name="$(basename "$0")"

# Function to get resource values
xrdb_get () {
output="$(appres Dunst | grep "$1:" | head -n1 | cut -f2)"
default="$2"
printf '%s' "${output:-$default}"
}

# Attributes dictionary.
#
# Format:
# ["<section>-<attribute>"]="<value>|$(xrdb_get '<X_resource>' '<default>')"
#
# You have to ensure <attribute> exists in <section> in $user_conf file
declare -A theme_attr_dict=(
["global-font"]="$(xrdb_get 'font' 'Monospace') $(xrdb_get '*.font_size:' '11')"
["global-frame_width"]="$(xrdb_get 'border_width' '1')"
["global-frame_color"]="\"$(xrdb_get 'color8' '#65737e')\""

["urgency_low-background"]="\"$(xrdb_get 'color0' '#2b303b')\""
["urgency_low-foreground"]="\"$(xrdb_get 'color4' '#65737e')\""
["urgency_low-frame_color"]="\"$(xrdb_get 'color4' '#65737e')\""

["urgency_normal-background"]="\"$(xrdb_get 'color0' '#2b303b')\""
["urgency_normal-foreground"]="\"$(xrdb_get 'color2' '#a3be8c')\""
["urgency_normal-frame_color"]="\"$(xrdb_get 'color2' '#a3be8c')\""

["urgency_critical-background"]="\"$(xrdb_get 'color0' '#2b303b')\""
["urgency_critical-foreground"]="\"$(xrdb_get 'color1' '#bf616a')\""
["urgency_critical-frame_color"]="\"$(xrdb_get 'color1' '#bf616a')\""
)

# Attributes dictionary keys.
readonly valid_keys="${!theme_attr_dict[@]}"


#
# File paths
#
# User config dir and file
readonly user_conf_dir="${XDG_CONFIG_HOME:-$HOME/.config}/dunst"
readonly user_conf="$user_conf_dir/dunstrc"

# Default config dir and example file
example_conf_dir="/usr/share/dunst"
example_conf="$example_conf_dir/dunstrc"

# User xresources color config file
readonly user_xr_color_conf="$user_conf_dir/dunstrc_xr_colors"


# Check if the user config directory exists
if ! [ -d "$user_conf_dir" ]; then
printf 'Creating folder "%s".\n' "$user_conf_dir"
mkdir -p "$user_conf_dir"
fi

# Check if the user config file exists
if ! [ -f "$user_conf" ]; then
printf '"%s" file does not exist.\nChecking for config file example.' \
"$user_conf"

if [ -d "/usr/share/dunst" ]; then
# Archlinux default dir and example file
example_conf_dir="/usr/share/dunst"

if [ -f "$example_conf_dir/dunstrc" ]; then
example_conf="$example_conf_dir/dunstrc"
else
printf 'Could not find the example config file in "%s".
\nPlease, change $example_conf variable in the script.' \
"$example_conf_dir"
exit 1
fi

elif [ -d "/usr/share/doc/dunst" ]; then
# Debian/Ubuntu default dir
example_conf_dir="/usr/share/doc/dunst"

if [ -f "$example_conf_dir/dunstrc.example.gz" ]; then
# Ubuntu <= 17.10 and Debian <= 1.2.0-2 example file:
example_conf="$example_conf_dir/dunstrc.example.gz"
elif [ -f "$example_conf_dir/dunstrc.gz" ]; then
# Ubuntu >= 18.04 and Debian >= 1.3.0-2 example file:
example_conf="$example_conf_dir/dunstrc.gz"
else
printf 'Could not find the example config file in "%s".
\nPlease, change $example_conf variable in the script.' \
"$example_conf_dir"
exit 1
fi

else
printf 'Could not find the example config directory.
\nPlease, change $example_conf_dir variable in the script.'
exit 1
fi

printf 'Copying example config to "%s".\n' "$user_conf_dir"

# Get the extension to check if the file is compressed
if [[ "${example_conf##*\.}" == "gz" ]]; then
# Extract example file to user config file
gunzip -c "$example_conf" > "$user_conf"
else
cp "$example_conf" "$user_conf_dir"
fi

fi

# Regular expressions
readonly re_section_line='^\[(.*)\]$'
readonly re_empty_comment_line='(^$)|(^[[:space:]]*(\#)|(;))'
readonly re_attribute_line='^([[:space:]]*)([_[:alnum:]]+)'

# Create an array with the file lines
mapfile -t conf < "$user_conf"

# Iterate over the file lines
for idx in "${!conf[@]}"; do
# Current line
curr_line="${conf[$idx]}"
# If we are in a new section:
if [[ "$curr_line" =~ $re_section_line ]]; then
curr_section="${BASH_REMATCH[1]}"
continue
fi
# Skip the line if it is empty or has a comment
if [[ "$curr_line" =~ $re_empty_comment_line ]]; then
continue
fi
# Get the attribute in the current line
[[ "$curr_line" =~ $re_attribute_line ]]
curr_attr_name="${BASH_REMATCH[2]}"
curr_sett_name="${curr_section}-${curr_attr_name}"
# If the current attribute is not in our dictionary, continue
case "$valid_keys" in
*"$curr_sett_name"*)
conf[$idx]=" ${curr_attr_name} = ${theme_attr_dict[$curr_sett_name]}"
;;
esac
done

# Create a header for the xr_color config file
user_xr_color_conf_content="\
###################################
#
# Config file created with
# $script_name wrapper
#
###################################
"

# After everything is completed, write the new config to a file
user_xr_color_conf_content+="$(printf '%s\n' "${conf[@]}")"

printf '%s\n' "$user_xr_color_conf_content" > "$user_xr_color_conf"

printf '"%s" updated successfully.\n' "$user_xr_color_conf"

0 comments on commit 07ca494

Please sign in to comment.