YAD - Tips

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Message
Author
lika
Posts: 7
Joined: Tue 07 Aug 2018, 01:30

#451 Post by lika »

fredx181 wrote: There is some output, but not all, I think commenting out some lines starting at line 41, change to:

Code: Select all

   while read line; do
#     if [[ "$(echo $line | grep '[0-9]*%')" ]];then
#       percent=$(echo $line | awk '{print $2}')
#        echo "${percent%.*}%" >> "$progress_pipe"
        echo "$line" >> "$progress_pipe"
#     fi
Then I see all output from tar.
Fred
Thank you.
I'll see.

User avatar
misko_2083
Posts: 114
Joined: Tue 08 Nov 2016, 13:42

#452 Post by misko_2083 »

Thanks fredx181, don't know why I thought that DIR field in the form dialog doesn't quote the path. :roll:

lika Added pv command to display the progress. One more dependancy to install but I see no other way for now.

X window button is managed by the window manager so intercepting it is impossible. It's only possible to get the exit status 252 when the window is closed via X.That's why I added undecoratzed to pprevent the paned dialog from closing and --no-escape to prevent closing the same dialog if Escape was pressed. That is the simplest solution. Anything else would be too complex.

To use the checkboxes in the function install_app you would need to check if $2 and $3 are true or false.

The script now displays extra dialogs and prevents them from opening multiple times e.g.if the cancel and abort buttons are clicked several times. This is working for me:

Code: Select all

#!/bin/bash

if ! hash pv; then
   yad --text "install pv first"
   exit 1
fi

export faextraxt='bash -c "install_app %1 %2 %3 %4"'

# We need this to store the main proces ID
export main_proc_id=$(mktemp -u --tmpdir fpid.XXXXXXXX)

export progress_pipe=$(mktemp -u --tmpdir ftd.XXXXXXXX)
mkfifo "$progress_pipe"

export percentage_pipe=$(mktemp -u --tmpdir ftd2.XXXXXXXX)
mkfifo "$percentage_pipe"

export form_pipe=$(mktemp -u --tmpdir ftd3.XXXXXXXX)
mkfifo "$form_pipe"

trap "rm -f $progress_pipe $percentage_pipe $form_pipe $main_proc_id" EXIT

fakey=$(($RANDOM * $$))

function install_app
{
# Form fields are read in a cycle 1-4, each cycle sets new values

# Disables all the form fields when the install starts
echo "@disabled@" > "$form_pipe"
echo "@disabled@" > "$form_pipe"
echo "@disabled@" > "$form_pipe"
echo "@disabled@" > "$form_pipe"

echo "#Selected extraction path: $1" >> "$progress_pipe"
echo "#" >> "$progress_pipe"
echo "#Add a shortcut in the applications menu : $2" >> "$progress_pipe"
echo "#Add a desktop shortcut : $3" >> "$progress_pipe"
echo "#" >> "$progress_pipe"


  echo "#Preparing to extract..." >> "$progress_pipe"

   >"$main_proc_id"
   while read -r line; do
     # If line is a integer number
     if [[ "$line" == +([0-9]) ]];then
        echo "$line" >> "$progress_pipe"
     else
        echo "#$line" >> "$progress_pipe"
     fi

   done < "$percentage_pipe" &
   LOOP_PID="$!"

    # main process example for a demonstration purpose
    # for i in {1..100}; do echo "$i %" 2>&1 >> $percentage_pipe; sleep .1; done & echo $! > "$main_proc_id"
    
    pv -n archive.tar.xz 2>$percentage_pipe | xzcat | tar xp -C "$1" -v 2>&1 >> $percentage_pipe & echo $! > "$main_proc_id"

    # Wait here
    # Wait returns exit status ${?}
    # It's stderr is redirected to /dev/null to prevent kill command to print messages
    wait $(<$main_proc_id) 2>/dev/null

    if [[ "$?" = 0 ]]
    then
         echo "100%" >> "$progress_pipe"
         echo "#Install completed." >> "$progress_pipe"
         kill "$LOOP_PID"
         >$main_proc_id
         yad --text="Install successfull" --button="gtk-ok:0"  &
    elif [[ ! -s "$main_proc_id" ]]; then
         echo "#Install canceled." >> "$progress_pipe"
         kill "$LOOP_PID"
         >$main_proc_id
    else
         echo "#Install error." >> "$progress_pipe"
         kill "$LOOP_PID"
         >$main_proc_id
    fi

    # Resets the field values when the install ends
    echo "$1" > $form_pipe
    echo "$2" > $form_pipe
    echo "$3" > $form_pipe
    echo "$faextraxt &" > $form_pipe
}
export -f install_app

function get_pid_and_kill () {
Sure_Command='yad --text=Are you sure you want to cancel the installation? --button=gtk-yes:0 --button=gtk-no:1'
Sure_Command_PID="$(ps -eo pid,cmd | grep -F "$Sure_Command" | grep -v "grep" | awk '{ print $1 }')"

if [[ -s "$main_proc_id" ]] && [[ "$Sure_Command_PID" == "" ]] && yad --text="Are you sure you want to cancel the installation?" --button="gtk-yes:0" --button="gtk-no:1"; then
  if [[ -s "$main_proc_id" ]]; then
     BCKUPID="$(<$main_proc_id)"
     >"$main_proc_id"
     kill $BCKUPID 2>/dev/null
     sleep 1
  fi
   # The special yad variable $YAD_PID stores the main window PID
   # killing that PID closes the window
  [[ "$1" == "CLOSE" ]] && kill -s SIGUSR1 $YAD_PID
elif [[ ! -s "$main_proc_id" ]] ; then
  [[ "$1" == "CLOSE" ]] && kill -s SIGUSR1 $YAD_PID
fi

}
export -f get_pid_and_kill

exec 3<> $progress_pipe
exec 4<> $percentage_pipe
exec 5<> $form_pipe

yad --plug="$fakey" --tabnum=1 --form \
    --field="Choose a folder:DIR" \
    --field="Add a shortcut in the applications menu:CHK" \
    --field="Add a desktop shortcut:CHK" \
    --field="Install!system-software-install:fbtn" --cycle-read <&5 &

    # This is the initial cycle that sets the form field values
    echo "${HOME}" > $form_pipe         # set default folder in this line
    echo "TRUE" > $form_pipe               # default first checkbox value
    echo "FALSE" > $form_pipe              # default second checkbox value
    echo "$faextraxt &" > $form_pipe

yad --plug="$fakey" --tabnum=2 --window-icon="$ICON" --progress \
--enable-log="Show log" --log-height=100 --tail --borders=5 <&3 &


yad --paned --key="$fakey" --buttons-layout=edge \
    --button="gtk-close":'bash -c "get_pid_and_kill CLOSE" 2>/dev/null' \
    --button="Abort!gtk-stop":'bash -c "get_pid_and_kill" 2>/dev/null' \
    --text="" --title="TEST" --window-icon="$ICON" --center \
    --no-escape --undecorated & MAIN_PID=$!

# Redirecting kill output from get_pid_and_kill function to /dev/null
wait $MAIN_PID 2>/dev/null

exec 3>&-
exec 4>&-
exec 5>&-

User avatar
misko_2083
Posts: 114
Joined: Tue 08 Nov 2016, 13:42

#453 Post by misko_2083 »

by the way fred, discovered something by accident.

killing $YAD_PID in the beginning of funcion install_app, closes the first tab and the second tab goes to the top of the paned window. :shock:
The progress continues to run. :lol:

Code: Select all

.....

function install_app
{
# Check this out
kill $YAD_PID

# Form fields are read in a cycle 1-4, each cycle sets new values

# Disables all the form fields when the install starts
#echo "@disabled@" > "$form_pipe"
#echo "@disabled@" > "$form_pipe"
#echo "@disabled@" > "$form_pipe"
#echo "@disabled@" > "$form_pipe"

....

User avatar
fredx181
Posts: 4448
Joined: Wed 11 Dec 2013, 12:37
Location: holland

#454 Post by fredx181 »

Always your scripts are very educational misko, thanks !
I'm still struggling to understand these pipes and paned thing, but I'll get there probably... some day :wink:

Fred

stemsee

#455 Post by stemsee »

fredx181 wrote:Always your scripts are very educational misko, thanks !
I'm still struggling to understand these pipes and paned thing, but I'll get there probably... some day :wink:

Fred
Ditto (echo) that sentiment!

I have many questions :?
stemsee

lika
Posts: 7
Joined: Tue 07 Aug 2018, 01:30

#456 Post by lika »

misko_2083 wrote:Added pv command to display the progress. One more dependancy to install but I see no other way for now.
Hello, misko_2083.

What about coproc, tail or stdbuf instead pv?

And:
- there are 2 times expanded "Sure_Command" in the script, maybe use "export?"
- attach the final dialog (window about successful installation) to main window? Is it possible?
- is there way to fix top border (thin line) of the progress plug? But "--fixed" option don't applied for a plug.
- and maybe make the button "Abort" inactive at the beginning and at the end of the installation?

Replace the line:

Code: Select all

pv -n 2>$percentage_pipe | tar xJfv archive.tar.xz -C "$1" 2>&1 >> $percentage_pipe & echo $! > "$main_proc_id"
and now no visual progressbar... :?

Yes, there is a "--pulsate" option, but it doesn't quite fit here.

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

#457 Post by Argolance »

Bonsoir,
yad --text doesn't let to display text the way it is directly formatted in the script using \n for break lines but it is automatically wrapped! How could this behavior be bypassed?
Thank you for your attention.

Cordialement.

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

#458 Post by smokey01 »

Argolance wrote:Bonsoir,
yad --text doesn't let to display text the way it is directly formatted in the script using \n for break lines but it is automatically wrapped! How could this behavior be bypassed?
Thank you for your attention.

Cordialement.
It does here in Ver 0.36.3:

Code: Select all

yad --text "One\nTwo\nThree\nFour\nFive"
If this is what you mean.

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

#459 Post by MochiMoppel »

@Argolance: I assume that your question refers to your Xdialog example here.
Unlike Xdialog yad does not seem to autoexpand the window but breaks lines at word boundaries. A simple way to mimic Xdialog would be to replace ordinary spaces with non-breaking spaces. Would need more sophistication if your text contains tabs.

Code: Select all

MSG1="$(gettext 'YaPI (Yet another Puppy Installer) supports 3 alternative installation types and mounts your drives seeking every installable Pup iso:')" 
MSG2="$(gettext '(1) FRUGAL***** 
    The files vmlinuz, initrd.gz and pup_xxx.sfs (plus, as needed, sfs files starting with an a, f, y, or z) are copied to a chosen partition. 
    This partition will retain, undisturbed, all existing information currently held in the form of directories and files. 
    This can be any type of partition, for example: MSDOS, Windows (FAT, NTFS) or Linux (EXT2, EXT3, EXT4 or REISERFS). 
    THIS IS RECOMMENDED AS THE BEST OPTION for most users. IT IS ALSO THE SAFEST as with this method nothing is overwritten.')\n\n 
  $(gettext 'or, (2) SUPERFLOPPY (simple but destructive without care and needs a special process if wanting to return the drive to normal use) 
    Wnich takes over the entire storage device. It has neither MBR, master boot record, nor PARTITIONS and only a SINGLE file system. 
    It uses part of the storage device space for its frugal installation and the remaining space is used for data storage.')"

nbsp=$'\xc2\xa0'  
MSG1=${MSG1//' '/$nbsp}
MSG2=${MSG2//' '/$nbsp}

yad --text "$MSG1"\n\n"$MSG2"

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

#460 Post by Argolance »

Bonjour,

@smokey01
Excuse me if my explanation wasn't clear. :oops:

@MochiMoppel
As there is a --wrap option, I thought that without this option, text would be non wrapped, that is to say, "normally" displayed as formatted using break lines \n or not formatted (without any \n), just new line every new sentence ("word boundaries"?), just like Xdialog or your example do (what I was looking for!).

Thank you both.

Cordialement.

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

#461 Post by MochiMoppel »

Argolance wrote:As there is a --wrap option, I thought that without this option, text would be non wrapped
--wrap is an option for the --text-info dialog. If you don't use this option and if the text is wider than the window width, a horizontal scrollbar is added. Window size does not change.

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

#462 Post by Argolance »

OK, thanks! Everything is clear now... :)

User avatar
fredx181
Posts: 4448
Joined: Wed 11 Dec 2013, 12:37
Location: holland

#463 Post by fredx181 »

MochiMoppel wrote:Unlike Xdialog yad does not seem to autoexpand the window but breaks lines at word boundaries. A simple way to mimic Xdialog would be to replace ordinary spaces with non-breaking spaces. Would need more sophistication if your text contains tabs.
Code:
....
That's a great workaround, thanks !
I always adjust --width=... to get the text in full width (no-wrap), but it's very much depending on the font size that is configured on the system, so may not work as intended if configured font size is very large.

Fred

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

#464 Post by MochiMoppel »

fredx181 wrote:That's a great workaround
Works also with gtkdialog:

Code: Select all

export MSG2
echo '<text><input>echo -e "$MSG2"</input></text>' | gtkdialog -s
Even when font sizes are the same, with gettexted lines you never know how long they will be in translated texts, so hard coding an appropriate window size would be impossible.

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

#465 Post by Argolance »

Bonjour,
fredx181 wrote:I always adjust --width=... to get the text in full width (no-wrap).
Strange: I also tried this trick found somewhere on the web, but it doesn't work:

Code: Select all

Impossible to analyze the command line: Impossible to analyze the entire value " " for --width
Running:

Code: Select all

# yad --version
0.33.1 (GTK+ 2.24.10)
Cordialement.

User avatar
fredx181
Posts: 4448
Joined: Wed 11 Dec 2013, 12:37
Location: holland

#466 Post by fredx181 »

Argolance wrote:Bonjour,
fredx181 wrote:I always adjust --width=... to get the text in full width (no-wrap).
Strange: I also tried this trick found somewhere on the web, but it doesn't work:

Code: Select all

Impossible to analyze the command line: Impossible to analyze the entire value " " for --width
Running:

Code: Select all

# yad --version
0.33.1 (GTK+ 2.24.10)
Cordialement.
Ah, sorry, I just meant "--width=<some-value>" for example: --width=800
(and test if e.g. 800 is enough to display the text "full width" without wrapping it, otherwise increase the value)
But Mochi's workaround does it automatically.

Fred

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

#467 Post by Argolance »

Bonjour,
fredx181 wrote:Ah, sorry, I just meant "--width=<some-value>" for example: --width=800
Thank you.
In the script I am tweaking, a single Xdialog function is used for different text strings:

Code: Select all

msg () { yad $WIDTH --window-icon=/usr/share/icons/hicolor/48x48/apps/yapi.png --button=gtk-ok --title YaPI --text="$1" 0 0 ; }
So, there are different possible values given to the variable WIDTH I added, according to the chosen text.
Example:

Code: Select all

MSG1="xxxxxxx xxx xxxxxxxxxx xxxxxxxxxxx xx xxxxxxxxx xxx xxxxxxxxxx xxxxx xxx xxxxxxx xxxx xxx xxxxxxx"
MSG2="xxxxxxxxxxxxxxxxx xx xxxxxxxxxxxxxxxxxxxxxxx"
WIDTH="--width=800"
msg "$MSG1 \n\n $MSG2"
... and WIDTH="" or no variable at all, if I want the text to be automatically wrapped by yad.

Cordialement.

User avatar
Mike Walsh
Posts: 6351
Joined: Sat 28 Jun 2014, 12:42
Location: King's Lynn, UK.

#468 Post by Mike Walsh »

Afternoon, all.

Now, then; need a wee bit of advice here, if it so please y'all.

As you all probably know by now, I'm pretty much a neophyte at Bash scripting. I've used YAD successfully in recent months for the occasional info or selector box GUI (courtesy of Grant's comprehensive guide, and the odd snippet I've found on The Linux Rain); easy enough when the required output is simply to run a single command.

What I'm not so clear about is how to capture the output of certain YAD commands.....and then to assign that output to a variable, which can then be used as part of an exec command, later in the same script.

(Remember; beginner here, so excuse me if I seem to be asking absolutely noobish-type questions. We all have to start somewhere..!)

------------------------------------

In particular, something I'm working on at the moment is making use of these two YAD commands:-

Code: Select all

yad --form --field="MDIR:MDIR" /root
.....which brings up the directory selector dialog, and lets you select a specific directory, and this one:-

Code: Select all

yad --form --field=LABEL:NUM "10"
.....or a close variation on it, to allow selection of a numeric value.

In a nutshell, how do you 'capture' the output of these two commands? And, having captured that output, how can I then 'assign' it to a variable?

I'm thinking something like the following for the first example:-

Code: Select all

 --form --field="Choose image directory:-:MDIR" /root =`$DIR` \
.....but I've probably got the principle of the 'backticks' (`) rather muddled up. In fact probably the entire concept of variables totally wrong... A wee bit of clarification would be very much appreciated!

I have several ideas for GUIs I'd like to do for various things, but until I can get this business of capturing outputs and assigning variables straightened out in my head, I admit I'm just whistling in the wind.....


Mike. :wink:

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

#469 Post by MochiMoppel »

Code: Select all

#!/bin/bash
read MYNUMBER MYDIR <<< $(yad --form --separator=$'\n' --field=LABEL:NUM "10" --field="MDIR:MDIR" /root)
echo "$MYNUMBER"
echo "$MYDIR"
yad is a bit special as it returns values separated by "|" characters. Changing this character to a linefeed $'\n' returns all values on a separate line and makes it easier to assign the values to variables.

User avatar
fredx181
Posts: 4448
Joined: Wed 11 Dec 2013, 12:37
Location: holland

#470 Post by fredx181 »

Hi Mike,
I see while I was writing this that MochiMoppel replied already.
Anyway here's sort of how I would do it.
This should do to get DIR variable:

Code: Select all

SETUP=$(yad --form --field="Choose image directory:-:MDIR" /root)
# The output of yad has delimiter "|"
echo $SETUP # full output
export DIR="`echo $SETUP | cut -d "|" -f 1`"
echo $DIR
Say you have 2 fields, then , e.g. (note the "f 2" in cut command for OUTDIR)

Code: Select all

SETUP=$(yad --form --field="Choose image directory:-:MDIR" /root \
--field="Choose output directory:-:MDIR" /root)
echo $SETUP # full output, next will separate them
export DIR="`echo $SETUP | cut -d "|" -f 1`"
export OUTDIR="`echo $SETUP | cut -d "|" -f 2`"
echo $DIR
echo $OUTDIR
Fred
Last edited by fredx181 on Tue 04 Sep 2018, 15:21, edited 1 time in total.

Post Reply