-
Notifications
You must be signed in to change notification settings - Fork 351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added dunst xresources theme wrapper #525
Conversation
First glance:
Code looks quite good for the rest. |
Ok! I'm on it. |
@bebehei I've been searching and the simplest idea I have right now is to create a function with a case from this answer something like this: theme_vars () {
case "$1" in
"key1") printf '%s' "value1";;
"key2") printf '%s' "value2";;
....
esac
}
printf 'dict[key1]=%s' "$(theme_vars 'key1')" I've searched for sh POSIX standard and EDIT: Other option is to change the shebang to EDIT1: Another obstacle, there are no arrays in POSIX, so goodbye to my line-by-line file access. EDIT2: Fixed misspelling. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another obstacle, there are no arrays in POSIX, so goodbye to my line-by-line file access.
Arrays give a huge advantage in this so with that we're probably never going to get 100% POSIX compliant. In this case I don't think it matters anyway it runs on bash which covers 99% of the market and for others they can always run it in a bash subshell.
I found a few nitpicks but I have not written more than a few toy shell scripts so I am not really that qualified to review this. I'd be interested to know if @bebehei has any further input.
contrib/dunst_xr_theme_changer.sh
Outdated
def_conf=$def_conf_dir/dunstrc | ||
|
||
# User config dir and file | ||
user_conf_dir=$XDG_CONFIG_HOME/dunst |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$XDG_CONFIG_HOME
should default to $HOME/.config
if unset.
contrib/dunst_xr_theme_changer.sh
Outdated
# configuration to $user_themed_conf file. | ||
# | ||
############################################################################### | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using set -e
would probably be wise in such scripts to catch any errors.
contrib/dunst_xr_theme_changer.sh
Outdated
[ -f $user_conf ] || { | ||
printf '%s does not exist.\n.' "$user_conf" | ||
printf 'Copying default config to %s...\n' "$user_conf_dir" | ||
cp $def_conf $user_conf_dir |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should ensure that $def_conf
exists in the first place, not every distro has the default dunstrc in /usr/share/dunst/dunstrc
, in Debian for example it's in /usr/share/doc/dunstrc.gz
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any other possible default directories to include in the search? Or should I just ask the user to change $def_conf_dir
path?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did some research and realized about the following:
Distribution | Directory | file |
---|---|---|
Archlinux | /usr/share/dunst |
dunstrc |
Debian < 1.3.0-2 / Ubuntu < 18.04 | /usr/share/doc/dunst/ |
dunstrc.example.gz |
Debian >= 1.3.0-2 / Ubuntu >= 18.04 | /usr/share/doc/dunst/ |
dunstrc.gz |
contrib/dunst_xr_theme_changer.sh
Outdated
|
||
# Regular expressions | ||
re_section_line='^\[(.*)\]$' | ||
re_empty_comment_line='(^$)|(^[[:space:]]*\#)' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Semicolons can also be used as comments.
contrib/dunst_xr_theme_changer.sh
Outdated
# Regular expressions | ||
re_section_line='^\[(.*)\]$' | ||
re_empty_comment_line='(^$)|(^[[:space:]]*\#)' | ||
re_attribute_line='^[[:space:]]{4}([_[:alnum:]]+)' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why just 4 spaces? Let's support all indentation types. Something like ^(?:[[:space:]]+)?([_[:alnum:]]+)
should do the trick.
contrib/dunst_xr_theme_changer.sh
Outdated
# Current line | ||
curr_line="${conf[$idx]}" | ||
# If we are in a new section: | ||
[[ "$curr_line" =~ $re_section_line ]] && { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why use these weird contraptions from logical operators rather than the usual if/fi
pair? They are confusing at first glance.
contrib/dunst_xr_theme_changer.sh
Outdated
@@ -0,0 +1,115 @@ | |||
#!/usr/bin/env sh |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$ sh ./dunst_xr_theme_changer.sh
./dunst_xr_theme_changer.sh: 56: ./dunst_xr_theme_changer.sh: mapfile: not found
./dunst_xr_theme_changer.sh: 63: ./dunst_xr_theme_changer.sh: Syntax error: "(" unexpected
It should probably use bash
rather than sh
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, variable expansion...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
High quality in code! I only found some small nitpicks. I love the changes and look forward to merge them.
Thanks for heavily using printf
. I never knew, that echo
is such a bad command.
Could you please squash your commits, too?
contrib/dunst_xr_theme_changer.sh
Outdated
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" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As you're using single quotes in the format string, you'll print \$example_conf
and not solely $example_conf
.
contrib/dunst_xr_theme_changer.sh
Outdated
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" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As you're using single quotes in the format string, you'll print \$example_conf
and not solely $example_conf
.
contrib/dunst_xr_theme_changer.sh
Outdated
|
||
else | ||
printf 'Could not find the example config directory. | ||
\nPlease, change \$example_conf_dir variable in the script' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As you're using single quotes in the format string, you'll print \$example_conf
and not solely $example_conf
.
contrib/dunst_xr_theme_changer.sh
Outdated
|
||
set -e | ||
|
||
script_name="$(basename $0)" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"$0"
contrib/dunst_xr_theme_changer.sh
Outdated
|
||
# Function to get resource values | ||
xrdb_get () { | ||
output=$(xrdb -q | grep -e "$1" | cut -f2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quotes for $(...)
contrib/dunst_xr_theme_changer.sh
Outdated
|
||
# Check if the user config directory exists | ||
if ! [ -d "$user_conf_dir" ]; then | ||
printf '%s\n' "$user_conf_dir folder doesn't exist, creating..." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
printf 'Creating folder "%s".\n' "$user_conf_dir"
contrib/dunst_xr_theme_changer.sh
Outdated
# 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Quotes for the steamname variable? > "$user_xr_color_conf"
Edit: Covered. |
Thank you both @bebehei and @tsipinakis for your nitpicks! I think the code is cleaner now. Any other change you would like to make to the script? |
|
||
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" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This won't work, the file in ubuntu and debian is compressed with gzip you'll need to uncompress it to use it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Completely forgot 😅.
The code quality is close to bash perfectness. But the actual point, if the script works is completely unclear to me. I admit, I haven't executed the script and I'm too lazy to do it. I'd like to ask some guys from #357 to test it. @lulivi You squashed the commit. Thanks. But you also squashed the commit messages. And most of them are invalid now. e.g: "Quoted $0". There is no need to quote |
I tested the script. It seems to be working fairly well, except for 2 things:
I would suggest to put the colours in variables at the top of the config file, but dunst probably doesn't support variables, and then the user could reuse these variables in the config file. i3wm does the same thing with
Even though this script is going to work pretty well, I think you guys should check out how i3wm lets the user access the X ressource database in the config file. Anyway this is awesome apart from the little things I pointed out. |
contrib/dunst_xr_theme_changer.sh
Outdated
############################################################################### | ||
# | ||
# This script creates a dunst user configuration file in | ||
# $XDG_CONFIG_HOME/dunst/ folder, changing dunst basic teeming options |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
teeming
-> theming
(I assume)
Just because I noticed it :)
Thank you @nhatzHK for your review!
I have a test tomorrow, so I will start with the debug after it. EDIT: The first problem is solved when changing There is another problem. if the example file is called EDIT 1: I was wrong with the new problem. I just decompress |
@nhatzHK I've tried the final script, and it does not duplicate the colors for me...
Don't know why is working bad for you. EDIT: @bebehei what do you think about the squashed commits now? I tried to keep the important ones. |
I strongly recommend https://chris.beams.io/posts/git-commit/ There is no better thing. @tsipinakis, right? 😉 A good commit message could be:
|
contrib/dunst_xr_theme_changer.sh
Outdated
|
||
# Function to get resource values | ||
xrdb_get () { | ||
output="$(xrdb -q | grep -e "$1" | cut -f2)" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The generated file has the right colours, but the string is terminated on a new line, and the colour is repeated in it.
I think here is the problem. You have to make sure, that only a single line is retrieved. Currently you grep for anything matching the string $1
. It could be even the value, which matches.. Usually Xresources have *color0: #abcdef
, but you also could define XTerm*color0: #123456
. And your regex would match both.
As @nhatzHK has already mentioned: You should use another method to query the X resource database, which also takes care of the precedence of specific values.
@nhatzHK Just to verify my thesis: What does xrdb -q | grep 'color0:'
give as the output?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is right. I do have several lines ending with colorX
in my X ressource database.
$ xrdb -q | grep 'color0:'
*.color0: #0d170a ## notice the leading dot
*color0: #0d170a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The thing is: should I just get the first line?
e.g.: If you have
XTerm*color0: #123456
*.color0: #0d170a
*color0: #0d170a
then with head -n1
will get the XTerm color:
$ xrdb -q | grep "color0:" | head -n1 | cut -f 2
#123456
So maybe it is interesting to place colors at the begining or at the end. Other option is to write the exact xresource name:
$ xrdb -q | grep "*.color0:" | head -n1 | cut -f 2
#0d170a
But maybe this option will get the same problem that we have before, something like
XTerm*.color0: #123456
*.color0: #0d170a
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about we throw more regex at it?
^(?:\*?\.?[a-zA-Z0-9]+):[[:space:]]?(.*)$
or
^(?:\*?\.?color0):[[:space:]]?(.*)$
to catch wildcards and
^(?:Dunst\*?\.?[a-zA-Z0-9]+):[[:space:]]?(.*)$
or
^(?:Dunst\*?\.?color0):[[:space:]]?(.*)$
for dunst specifically.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey guys, if I read the docs right, the key should be the appres
program. No regex involved.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hum... Can't figure out how to make it work :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be as simple as replacing xrdb -q
with appres Dunt
and then using | head -n1
as you suggested. Some testing shows that it always puts the more specific match at the top.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks Nikos! I was trying something like Dunst.color0
and wasn't working. The script is fixed now.
Oh sorry, I understood you wanted me to keep relevant commits. So maybe I should follow that commit structure?
What do you think? Or should I just keep it in oneline as you suggested? |
d08615d
to
52bd9c0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think? Or should I just keep it in oneline as you suggested?
This looks perfect, commit messages are generally the individual developers choice and that guide should be read as a general guideline, not a rule.
The only other nitpick I'd mention is maybe have theme_attr_dict
at the top, under the config variables, so that everything "user editable" is in one place and one doesn't have to dig through the script to find them.
Other than that unless @bebehei has any other comments I'd say this is ready to be merged - awesome work!
That's so true. I'm on it! Edit: Also, I think I'm defining every non changing variable as |
The changes and the script looks good. I'm putting myself in the role of a user, who just wants to use this wrapper and has no clue about the usage of it:
IMO, these questions should get answered in the header. I think that'll make the difference between "there is something usable but nobody uses it" and "there is something good and everyone uses it". |
All right. Maybe I could do a more extended header. Something like:
|
Hey guys, I've made some improvements in the header (you can check the extended text and tell me what do you think) and created 2 functions to output :
EDIT: Maybe I can simplify the script by removing the help and usage functions and leaving the extended header without |
I play the nerd card first: What command line prompt do you have? If you add them, I don't have any objections and I'm glad to add this to dunst! The only question, which now came to my mind: How will this script come the user's machine. Is this a maintainer's job to put that file manually somewhere in the package or is this our job? |
The prompt is a modified version of the bash_git_prompt for fish check it out here.
Hummm... 🤔 Thats a really good question. This script is clearly an external part of dunst. It is not as integrated as |
I can only speak for debian since that's what I'm familiar with: contrib directories and similar utility scripts are usually put along with documentation in |
@tsipinakis Thanks. I guess my question is answered positively! @lulivi please add the docs, you've pasted and - if @tsipinakis has no further wishes - I'm glad to merge it! |
contrib/dunst_xr_theme_changer.sh
Outdated
## scape " characters for proper functioning of dunst config reader (for | ||
## example "#ffeegg"). | ||
## | ||
## You can define X_res variables in $HOME/.Xresources file. For in depht |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
depht
typo
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
Thanks again for the work put into this @lulivi. |
Hello.
I've been reading some discussions about allowing Command Substitution
$(...)
and X resources access from the configuration file (#357).Then, I headed to the dunst channel in freenode and
nikos
came up with the idea of creating a script to solve this problem.So I created a wrapper to change colors and some other attributes according to the Xresources theme. Any tips or improvements?