Page 1 of 1
Using comments in gtkdialog
Posted: Mon Jan 29, 2024 9:57 am
by MochiMoppel
The GtkDialog - tips thread in the Murga forum states
8.) Advanced Syntax - make your code readable
###################################################################################
Comments in your code
Gtkdialog doesn't support comments in its xml code. But if we convert all comments before execution, it works. The reason why I use double hash (##) for my comments, is because colors in a <span> tag might be defined as 'red' or '#FF0000'. The latter would be removed by using a single hash.
Well, using double hash (##) is not a good idea because they are used in bash constructs like ${variable##*/}
, however using the Geany default comment markers hash+tilde (#~) is safe. This method to use and then clean comment markers before passing the GUI description to gtkdialog is what I used so far.
By chance I now discovered that comments in XML style <!-- comment -->
are supported in newer versions. The comments may even span multiple lines. In BW64, using gtkdialog version 0.8.5, this works
Code: Select all
#!/bin/sh
export DIALOG='
<!-- This is a comment -->
<edit></edit>
<!-- This is
a multiline
comment -->'
gtkdialog -p DIALOG
In older versions, e.g. 0.8.4 of Slacko 5.6, it would produce a syntax error.
Though I will continue to use #~ markers for backward compatibility, the <!-...--> syntax might be useful for other users.
Re: Using comments in gtkdialog
Posted: Fri Feb 02, 2024 1:10 am
by AntonioPt
I use this in order to remove comments at the end hope it helps you
MAIN_DIALOG="$(sed "/<!--/{:a;/-->/{s/<!--.*-->//;bb;};N;ba;:b;}" <<< "${MAIN_DIALOG}")"
$GTKDIALOG -pc MAIN_DIALOG
Re: Using comments in gtkdialog
Posted: Sun Feb 04, 2024 1:53 am
by MochiMoppel
AntonioPt wrote: Fri Feb 02, 2024 1:10 am
I use this in order to remove comments at the end
Thanks @AntonioPt .You really do? I wonder why you use such comments in the first place. They are painful to create (Geany wouldn't help with automation since it will use #~ ), difficult to remove (as your code proves) and even then neither the new gtkdialog support nor your code can handle nested comments.
Honestly I still haven't found a good reasons to use <!--...-->
comments. Still searching 
Re: Using comments in gtkdialog
Posted: Sun Feb 04, 2024 2:08 am
by AntonioPt
Hi @MochiMoppel ,
Take a look to this code i made for my self to make to compress initrd i have comments on it and at the end i use that to clean it 
i just add them in my personal code only but ... hope it helps you
Remove fake gz
P.S. i use comments because i still learning bash and GTKDIALOG but is useful all so if the code is big as .... lolol
This script doesn't do nothing yet ok just keep in mind that
Re: Using comments in gtkdialog
Posted: Sun Feb 04, 2024 8:18 pm
by AntonioPt
@MochiMoppel
Here works
Code: Select all
#!/bin/sh
export DIALOG='
<!-- This is a comment -->
<edit></edit>
<!-- This is
a multiline
comment -->'
DIALOG="$(sed "/<!--/{:a;/-->/{s/<!--.*-->//;bb;};N;ba;:b;}" <<< "${DIALOG}")"
gtkdialog -p DIALOG
Re: Using comments in gtkdialog
Posted: Tue Mar 05, 2024 6:13 am
by MochiMoppel
Let's forget <!--...-->
comments. Just too many disadvantages compared to #
hash mark comments (btw the official Unicode name for the # character is NUMBER SIGN )
This is what I use to remove comments from a GUI description MAIN_DIALOG before calling the gtkdialog binary:
Code: Select all
MAIN_DIALOG=$( sed -r 's/^#.*// ; s/[[:blank:]]+#.*//' <<< $MAIN_DIALOG )
This removes all comments, no matter if they start with a single or with multiple # characters or with Geany's #~
The only time this would fail is when using comments between action tags,e.g.
Code: Select all
<action>echo hello #this is a comment</action>
Perfectly legal, but I don't do that 
Re: Using comments in gtkdialog
Posted: Tue Mar 05, 2024 9:30 am
by BarryK
Note, "<<<" works, as long as /bin/sh points to bash
If it is a symlink to busybox, it won't work.
For portability, it would be better for the shebang line to be "#!/bin/bash"
Re: Using comments in gtkdialog
Posted: Tue Mar 05, 2024 10:09 am
by user1234
Thanks @MochiMoppel for finding that out. I personally found this about a week back while reading some GTKDIALOG example code, and I was like "What about comments?", and used such comments, and they worked!
MochiMoppel wrote: Sun Feb 04, 2024 1:53 am
AntonioPt wrote: Fri Feb 02, 2024 1:10 am
I use this in order to remove comments at the end
Thanks @AntonioPt .You really do? I wonder why you use such comments in the first place. They are painful to create (Geany wouldn't help with automation since it will use #~ ), difficult to remove (as your code proves) and even then neither the new gtkdialog support nor your code can handle nested comments.
Honestly I still haven't found a good reasons to use <!--...-->
comments. Still searching 
IMO, the main reason is that these are the real comments every XML and HTML file uses, not something created only for GTKDIALOG.
So, anyone reading the code, who doesn't exactly know GTKDIALOG's syntax but knows XML/HTML one, can in that way read them.
Re: Using comments in gtkdialog
Posted: Tue Mar 05, 2024 10:30 am
by step
My use case for using <!-- -->
comments is when I don't want to mix gtkdialog XML syntax with shell syntax. Then instead of embedding a gtkdialog source as a shell variable or heredoc in a shell script, I keep the dialog in its own separate file, say dialog.xml
then run gtkdialog -f dialog.xml
from the shell script. Vim is my editor. Its sane syntax highlighting and line/block operations on XML files is an advantage when I edit medium/large gtkdialogs.
Re: Using comments in gtkdialog
Posted: Fri Mar 15, 2024 11:04 am
by MochiMoppel
step wrote: Tue Mar 05, 2024 10:30 amVim is my editor. Its sane syntax highlighting and line/block operations on XML files is an advantage when I edit medium/large gtkdialogs.
OK, in this case XML style comments make sense, though the problem of backward compatibility remains. Syntax highlighting and line/block operations are also supported in Geany.
Personally I prefer to keep everything in one file. I use section folding and other techniques to keep large GUI descriptions manageable.