GtkDialog - tips

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Message
Author
User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

#1441 Post by don570 »

echo command can have options . I would try echo -n

or maybe echo -e and then use \n for a new line.

http://linuxcommand.org/lc3_man_pages/echoh.html
_____________________________________________

User avatar
Argolance
Posts: 3767
Joined: Sun 06 Jan 2008, 22:57
Location: PORT-BRILLET (Mayenne - France)
Contact:

#1442 Post by Argolance »

Thanks.
In the script above, the temporary text file is built at startup using the echo command only as example, but in reality, this text file is a list of files (built with the find command) which can have spaces in their path and in their name (example: /root/.moonchild productions/pale moon/xxx). It is these files that are problematic and are processed in segments instead of in one piece.
What could be done to avoid this?

User avatar
SFR
Posts: 1800
Joined: Wed 26 Oct 2011, 21:52

#1443 Post by SFR »

Argolance wrote:What could be done to avoid this?
This, for example:

Code: Select all

while read -r LINE; do
LIST="${LIST}
   <checkbox>
      <variable>$LINE</variable>
      <default>true</default>
      <label>$LINE</label>
      <action>if false disable:$LINE</action>
      <action>sed -i 's/$LINE//' /tmp/checkbox_test</action>
      <action>refresh:TEXT</action>
   </checkbox>
"
done < /tmp/checkbox_test
Greetings!
[color=red][size=75][O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource[/size][/color]
[b][color=green]Omnia mea mecum porto.[/color][/b]

User avatar
Argolance
Posts: 3767
Joined: Sun 06 Jan 2008, 22:57
Location: PORT-BRILLET (Mayenne - France)
Contact:

#1444 Post by Argolance »

Oh yes, thank you SFR! :D

some1
Posts: 117
Joined: Thu 17 Jan 2013, 11:07

#1445 Post by some1 »

Argolance,SFR:Nice :)
----
Argolance:
this text file is a list of files (built with the find command) which can have spaces in their path and in their name (example: /root/.moonchild productions/pale moon/xxx).
With your demo - try som slashes in the DATA:

Code: Select all

echo "My_tailor_is_rich
My/tailor/is/NOT/rich" > /tmp/checkbox_test 
 
and consider this:

Code: Select all

 <action>sed -i 's/$LINE//' /tmp/checkbox_test</action>
In the sed command -you can use | or some other char - instead of the default- /-s -but...?

The i-switch in sed may be convenient - but sed is in reality rewriting the whole file using a tempfile - which is moved to be the updated input-file.
Alternatively-we can emulate that -using awk,grep -v or a read-loop to update the input-file via a tempfile every time we choose a $LINE to be excluded.


----
Anyway -Nice demo.

User avatar
Argolance
Posts: 3767
Joined: Sun 06 Jan 2008, 22:57
Location: PORT-BRILLET (Mayenne - France)
Contact:

#1446 Post by Argolance »

Bonjour,
some1 wrote:In the sed command -you can use | or some other char - instead of the default- /-s -but...?
Yes indeed: my "real" script (allowing user to set the font of the UI of browsers like Seamonkey, Firefox, Pale Moon and some others, according to the current GTK theme or any else) uses %, because of the / that are in the found files paths (profiles.ini)...

Code: Select all

CSS_FILE="$(cat /tmp/PROF_PATHS)"

while read -r LINE; do
PROFILES_LIST="${PROFILES_LIST}
	<checkbox tooltip-text=" $(gettext "If unchecked, the 'userChrome.css' configuration file of this profile will not be modified") ">
		<variable>$LINE</variable>
		<default>true</default>
		<label>$LINE</label>
		<action>if false disable:$LINE</action>
		<action>sed -i 's%${LINE}%%' /tmp/PROF_PATHS</action>
	</checkbox>
"
done < /tmp/PROF_PATHS
Cordialement.

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

#1447 Post by don570 »

There is no need to use the read command. Your original script will work with a small change....

Last nite I read Advanced Bash manual
and the answer is to change the IFS

Here's an example with filenames rather than data in a file....


Filenames with embedded whitespace can cause globbing to choke. David Wheeler shows how to avoid many such pitfalls.

1 IFS="$(printf '\n\t')" # Remove space.
2
3 # Correct glob use:
4 # Always use for-loop, prefix glob, check if exists file.
5 for file in ./* ; do # Use ./* ... NEVER bare *
6 if [ -e "$file" ] ; then # Check whether file exists.
7 COMMAND ... "$file" ...
8 fi
9 done
10
11 # This example taken from David Wheeler's site, with permission.
Here's script with two extra lines...

Code: Select all

#!/bin/sh

echo  -n "My_tailor_is_rich
My tailor is NOT rich" > /tmp/checkbox_test

FILE="`cat /tmp/checkbox_test`"
I=$IFS; IFS="$(printf '\n\t')" 

for LINE in $FILE; do
LIST="${LIST}
   <checkbox>
      <variable>$LINE</variable>
      <default>true</default>
      <label>$LINE</label>
      <action>if false disable:$LINE</action>
      <action>sed -i 's/$LINE//' /tmp/checkbox_test</action>
      <action>refresh:TEXT</action>
   </checkbox>
"
done
IFS=$I
export MAIN="
<window window_position=\"1\">
   <vbox>
      <hbox border-width=\"5\">
         <frame Text (/tmp/checkbox_test)>
            <edit>
            <input file>/tmp/checkbox_test</input>
            <variable>TEXT</variable>
            </edit>
         </frame>
         <frame Checkboxes>
            ${LIST}
         </frame>
      </hbox>
      <hbox>
      <button><input file stock=\"gtk-undo\"></input>
         <label>$(gettext 'Reset')</label>
         <action>EXIT:restart</action>
      </button>      
         <button cancel></button>
      </hbox>
   </vbox>
</window>
"
I=$IFS; IFS=""
for STATEMENTS in  $(gtkdialog --program=MAIN); do
   eval $STATEMENTS
done
IFS=$I

[ "$EXIT" = "restart" ] && $0

User avatar
Argolance
Posts: 3767
Joined: Sun 06 Jan 2008, 22:57
Location: PORT-BRILLET (Mayenne - France)
Contact:

#1448 Post by Argolance »

Hello,
Great! :)
These "tips" are both welcome and work perfectly! I think we're never done learning...
Thank you a lot.

Cordialement.

User avatar
smokey01
Posts: 2813
Joined: Sat 30 Dec 2006, 23:15
Location: South Australia :-(
Contact:

#1449 Post by smokey01 »

Is there some way to make this work?

Code: Select all

<button>
	<label>Test</label>
	<action>if [ -e /tmp/file ]; then state="true"; else state="false"; fi</action>
	<action>echo $state</action>
</button>
I want to check if a file exists, if it does make the variable true, else false.
The variable is always blank.

TIA

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#1450 Post by MochiMoppel »

Code: Select all

<button> 
    <label>Test</label> 
    <action>if [ -e /tmp/file ]; then state="true"; else state="false"; fi; echo $state</action> 
</button>
Every <action> runs in its own subshell and variables created in such subshell are usable only in this subshell.

User avatar
smokey01
Posts: 2813
Joined: Sat 30 Dec 2006, 23:15
Location: South Australia :-(
Contact:

#1451 Post by smokey01 »

MochiMoppel wrote:

Code: Select all

<button> 
    <label>Test</label> 
    <action>if [ -e /tmp/file ]; then state="true"; else state="false"; fi; echo $state</action> 
</button>
Every <action> runs in its own subshell and variables created in such subshell are usable only in this subshell.
Thanks MochiMoppel.

Is it possible to use this method to enable/disable buttons.
EG: If the file exists disable the button else enable it.

Code: Select all

<button>
	<label>Status</label>
	<variable>STATUS</variable>
	<action>if [ -e /tmp/file ]; then disable:STATUS; else enable:STATUS; fi;</action>
</button>
I get this error:
sh: disable:STATUS: command not found
Thanks

User avatar
zigbert
Posts: 6621
Joined: Wed 29 Mar 2006, 18:13
Location: Valåmoen, Norway
Contact:

#1452 Post by zigbert »

Smokey
You're missing the conditional option.

Code: Select all

<action condition="file_is_false( /tmp/file )">disable:STATUS</action>

User avatar
smokey01
Posts: 2813
Joined: Sat 30 Dec 2006, 23:15
Location: South Australia :-(
Contact:

#1453 Post by smokey01 »

zigbert wrote:Smokey
You're missing the conditional option.

Code: Select all

<action condition="file_is_false( /tmp/file )">disable:STATUS</action>
At first I couldn't get it to work.

The penny has just dropped. It's reading the contents of a file to see if it's true or false. I thought it was checking to see if a file existed.

This will do nicely.

Thanks again.
Last edited by smokey01 on Wed 12 Dec 2018, 11:05, edited 1 time in total.

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#1454 Post by MochiMoppel »

Doesn't work for me either. I'm not even sure what file_is_false means. Not existent, not readable?

What should always work is the command_is_true condition:

Code: Select all

<action condition="command_is_true( if [ -e /tmp/file ]; then  echo true ;fi)">disable:STATUS</action>
or shorter:

Code: Select all

<action condition="command_is_true( [ -e /tmp/file ] && echo true)">disable:STATUS</action>

User avatar
smokey01
Posts: 2813
Joined: Sat 30 Dec 2006, 23:15
Location: South Australia :-(
Contact:

#1455 Post by smokey01 »

MochiMoppel wrote:Doesn't work for me either. I'm not even sure what file_is_false means. Not existent, not readable?

What should always work is the command_is_true condition:

Code: Select all

<action condition="command_is_true( if [ -e /tmp/file ]; then  echo true ;fi)">disable:STATUS</action>
or shorter:

Code: Select all

<action condition="command_is_true( [ -e /tmp/file ] && echo true)">disable:STATUS</action>
Ah this one is good too as it checks to see if a file exists rather than reading the contents of the file.

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#1456 Post by MochiMoppel »

smokey01 wrote:Ah this one is good too as it checks to see if a file exists rather than reading the contents of the file.
What do you mean by "good too"? It's what you tried to achieve with your example. Reading the contents is not what you asked for.

User avatar
smokey01
Posts: 2813
Joined: Sat 30 Dec 2006, 23:15
Location: South Australia :-(
Contact:

#1457 Post by smokey01 »

MochiMoppel wrote:
smokey01 wrote:Ah this one is good too as it checks to see if a file exists rather than reading the contents of the file.
What do you mean by "good too"? It's what you tried to achieve with your example. Reading the contents is not what you asked for.
You are quite correct and thanks.

I'm trying to automate the process a little more. If I explain the full requirements it might make more sense.

Attached is a logic map for the buttons. Red means disabled and Green enabled. There is actually a stop button which should stop the server and client and enable client and server buttons but not affect Teacher and Student buttons. The Teacher and Student buttons need to be enabled after their apps are closed. It's actually the same app but with different configuration. Not sure how to do this. Currently I have these buttons being enabled with the stop button.

I'm sure the code could be a lot smarter and tidier but it mostly works.

Thanks

Code: Select all

#!/bin/sh

[ -z $GTKDIALOG ] && GTKDIALOG=gtkdialog

MAIN_DIALOG='
<window>
	<vbox>
		<hbox>
			<button>
				<label>Server</label>
				<variable>SERVER</variable>
				<action>touch /tmp/server</action>
				<action>touch /tmp/client</action>
				<action>touch /tmp/teacher</action>
				<action>disable:CLIENT</action>
				<action>disable:TEACHER</action>
				<action>disable:SERVER</action>
			</button>
			
			<button>
				<label>Client</label>
				<variable>CLIENT</variable>
				<action>touch /tmp/client</action>
				<action>touch /tmp/server</action>
				<action>touch /tmp/student</action>
				<action>disable:SERVER</action>
				<action>disable:STUDENT</action>
				<action>disable:CLIENT</action>
			</button>

			<button>
				<label>Teacher</label>
				<variable>TEACHER</variable>
				<action>touch /tmp/teacher</action>
				<action>touch /tmp/student</action>
				<action>touch /tmp/server</action>
				<action>disable:STUDENT</action>
				<action>disable:SERVER</action>
				<action>disable:TEACHER</action>
			</button>
			
			<button>
				<label>Student</label>
				<variable>STUDENT</variable>
				<action>touch /tmp/student</action>
				<action>touch /tmp/teacher</action>
				<action>touch /tmp/client</action>
				<action>disable:TEACHER</action>
				<action>disable:CLIENT</action>
				<action>disable:STUDENT</action>
			</button>
			
			<button tooltip-text="Reset all the buttons">
				<label>Stop</label>
				<variable>STOP</variable>
				<action condition="command_is_true( [ -e /tmp/server ]&& echo true && rm /tmp/server)">enable:SERVER</action>
				<action condition="command_is_true( [ -e /tmp/client ]&& echo true && rm /tmp/client)">enable:CLIENT</action>
				<action condition="command_is_true( [ -e /tmp/teacher ]&& echo true && rm /tmp/teacher)">enable:TEACHER</action>
				<action condition="command_is_true( [ -e /tmp/student ]&& echo true && rm /tmp/student)">enable:STUDENT</action>
			</button>
			
			<button ok></button>
		</hbox>
		
	</vbox>
</window>
'
export MAIN_DIALOG

case $1 in
	-d | --dump) echo "$MAIN_DIALOG" ;;
	*) $GTKDIALOG --program=MAIN_DIALOG ;;
esac
Attachments
button-map.png
(4.7 KiB) Downloaded 747 times

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#1458 Post by MochiMoppel »

zigbert wrote:Smokey
You're missing the conditional option.

Code: Select all

<action condition="file_is_false( /tmp/file )">disable:STATUS</action>
@zigbert: As smokey01 and I couldn't get this to work I would be keen to learn how this condition is supposed to work. The description in the manual makes no sense to me and I haven't found a single script or example where this condition has been used.
I suspected that "file" would relate to a monitored input file, but tests were negative.
Do you know more?

[Edit] OK, figured it out. file_is_false tests if a given file contains "false", "no" or zero. In smokey01's example the tmp files contain nothing, hence the condition is not met. At least I now understand why nobody uses this condition.

User avatar
zigbert
Posts: 6621
Joined: Wed 29 Mar 2006, 18:13
Location: Valåmoen, Norway
Contact:

#1459 Post by zigbert »

MochiMoppel wrote:At least I now understand why nobody uses this condition.
I do :oops: :lol:

User avatar
smokey01
Posts: 2813
Joined: Sat 30 Dec 2006, 23:15
Location: South Australia :-(
Contact:

#1460 Post by smokey01 »

MochiMoppel wrote:
zigbert wrote:Smokey
You're missing the conditional option.

Code: Select all

<action condition="file_is_false( /tmp/file )">disable:STATUS</action>
@zigbert: As smokey01 and I couldn't get this to work I would be keen to learn how this condition is supposed to work. The description in the manual makes no sense to me and I haven't found a single script or example where this condition has been used.
I suspected that "file" would relate to a monitored input file, but tests were negative.
Do you know more?

[Edit] OK, figured it out. file_is_false tests if a given file contains "false", "no" or zero. In smokey01's example the tmp files contain nothing, hence the condition is not met. At least I now understand why nobody uses this condition.
Once I created a file containing the word false it work as you've also discovered.

I'm still trying to find a way to enable/disable a button depending if a file exists or not in the following situation:

If I use a function that runs psip like this:

run_psip() {
touch /tmp/psip-file
psip
rm /tmp/psip-file
}
export -f run_psip

and call it from a button, the function only runs when the button is clicked.
When psip is terminated the "rm /tmp/psip-file" needs to be run after psip is closed, then a test to see if the file exists. The button in the GUI should be enabled when there is no /tmp/psip-file and disabled when there is.

I thought the <input file> option might work but apparently it only seems to display graphic files on the button widget, if they exist. I also tried it with tag attributes file-monitor and auto-refresh, no joy.

Is there are way to achieve this?

Thanks

Post Reply