Using comments in gtkdialog

For discussions about programming, and for programming questions and advice


Moderator: Forum moderators

Post Reply
User avatar
MochiMoppel
Posts: 1245
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 22 times
Been thanked: 446 times

Using comments in gtkdialog

Post 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.

User avatar
AntonioPt
Posts: 220
Joined: Wed Aug 11, 2021 7:41 pm
Has thanked: 99 times
Been thanked: 37 times

Re: Using comments in gtkdialog

Post 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

Why astronauts use Linux
Because you can't open windows in space

User avatar
MochiMoppel
Posts: 1245
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 22 times
Been thanked: 446 times

Re: Using comments in gtkdialog

Post 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 :lol:

User avatar
AntonioPt
Posts: 220
Joined: Wed Aug 11, 2021 7:41 pm
Has thanked: 99 times
Been thanked: 37 times

Re: Using comments in gtkdialog

Post 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 :D
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

Attachments
compress_initrd.gz
(5.33 KiB) Downloaded 37 times

Why astronauts use Linux
Because you can't open windows in space

User avatar
AntonioPt
Posts: 220
Joined: Wed Aug 11, 2021 7:41 pm
Has thanked: 99 times
Been thanked: 37 times

Re: Using comments in gtkdialog

Post 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
Attachments
comments.png
comments.png (338.53 KiB) Viewed 826 times

Why astronauts use Linux
Because you can't open windows in space

User avatar
MochiMoppel
Posts: 1245
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 22 times
Been thanked: 446 times

Re: Using comments in gtkdialog

Post 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 :lol:

User avatar
BarryK
Posts: 2748
Joined: Tue Dec 24, 2019 1:04 pm
Has thanked: 136 times
Been thanked: 750 times

Re: Using comments in gtkdialog

Post 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"

User avatar
user1234
Posts: 415
Joined: Sat Feb 26, 2022 5:48 am
Location: Somewhere on earth
Has thanked: 156 times
Been thanked: 90 times

Re: Using comments in gtkdialog

Post 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 :lol:

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.

PuppyLinux 🐾 gives new life to old computers ✨

step
Posts: 549
Joined: Thu Aug 13, 2020 9:55 am
Has thanked: 57 times
Been thanked: 198 times
Contact:

Re: Using comments in gtkdialog

Post 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.

User avatar
MochiMoppel
Posts: 1245
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 22 times
Been thanked: 446 times

Re: Using comments in gtkdialog

Post by MochiMoppel »

step wrote: Tue Mar 05, 2024 10:30 am

Vim 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.

Post Reply

Return to “Programming”