You are not logged in.

#181 2020-08-05 07:26:12

Grover
New Member
Registered: 2020-08-03
Posts: 4

Re: The great yad hacking thread

Offline

#182 2020-08-05 17:56:10

ohnonot
...again
Registered: 2015-09-29
Posts: 5,592

Re: The great yad hacking thread

^ Looking good, thanks!

Offline

#183 2021-07-07 02:31:49

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 12,560
Website

Re: The great yad hacking thread

list: hidden columns are handy to store data

Discovered yad's hidden columns for lists recently so sharing  here, in case it's useful for someone.

In a checklist, or radiolist, you want to show the user some "pretty names" that they can understand easily, but when you get their choice(s) back you usually want some solid data, like a complete filepath. Instead of putting the data in an array and searching for it based on the pretty name the user chose (an associative array would actually work OK) you can store it in the yad list as a hidden column.

Then use --print-column to output just the hidden "data" column, and --separator='' so each line of yad's output is only the content of the hidden column - no problem with escaping spaces etc.

Put the contents of the list in an array in a do loop, like for <something>; do yadlist+=( "$check" "$pretty_name" "$real_data"); done and inside the yad command --column="Select:CHK" --column="Name:TEXT" --column=path:HD "${yadlist[@]}".

Trivial example: list the files in current directory, display the filename only, return the full paths of any files selected:

unset yadlist
for f in ./*
do
    [[ -d $f ]] && continue # files only
    yadlist+=(FALSE "${f##*/}" "$(readlink -f "$f")")
done
choice=$(yad --list --text="Choose some files" --center --borders=20 --width=400 --height=500 --checklist --column="Select:CHK" --column="Name:TEXT" --column=path:HD "${yadlist[@]}" --separator='' --print-column=3 --button=OK:0 --button=Cancel:1 )
echo "$choice"
/home/john/auth1_fms_19164
/home/john/jgmenu.log

...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), now on Bluesky, there's also some GitStuff )

Introduction to the Bunsenlabs Boron Desktop

Offline

#184 2021-07-08 04:37:04

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 12,560
Website

Re: The great yad hacking thread

^Yes newlines in data make life difficult because so many tools use them to separate items - yad too. In the case of filenames, I'd be inclined to just reject such names because the chance of a legitimate file having such a name is surely very low?
Thanks for pointing out --quoted-output which makes things clearer.

I have a directory full of strangely-named files for testing things like this:

john@lithium:~/text/oddnames$ for i in *; do [[ -d $i ]] && continue; echo "Filename: ${i}"; done
Filename: "
Filename: $HOME
Filename: a; rm b
Filename: b`whoami`c
Filename: f\\
Filename: "file with"quotes&
Filename: "fish & chips" <what's that?>
Filename: line
break
Filename: New Empty File
Filename: &quot;&gt;&lt;&apos; %percent%20end
Filename: s"s
Filename: 日本語.txt

(These are the real filenames, no escaping has been applied.)
Running the yad code I suggested above makes the yad display wrong for many of these:
aRZcIbO.png
But the filepaths in the hidden column are OK without any escaping:

john@lithium:~/text/oddnames$ echo "$choice"
/data/john/Dropbox/text/oddnames/"
/data/john/Dropbox/text/oddnames/$HOME
/data/john/Dropbox/text/oddnames/a; rm b
/data/john/Dropbox/text/oddnames/b`whoami`c
/data/john/Dropbox/text/oddnames/f\\
/data/john/Dropbox/text/oddnames/"file with"quotes&
/data/john/Dropbox/text/oddnames/"fish & chips" <what's that?>
/data/john/Dropbox/text/oddnames/line
break
/data/john/Dropbox/text/oddnames/New Empty File
/data/john/Dropbox/text/oddnames/&quot;&gt;&lt;&apos; %percent%20end
/data/john/Dropbox/text/oddnames/s"s
/data/john/Dropbox/text/oddnames/日本語.txt

Escaping ampersands was not enough to fix the displayed names though. While ' and " seem OK, < and > have to be escaped too, then the display is OK:

unset yadlist; for f in *; do [[ -d $f ]] && continue; name=${f##*/}; name=${name//&/&amp;}; name=${name//</&lt;}; name=${name//>/&gt;}; yadlist+=(FALSE "$name" "$(readlink -f "$f")"); done

cGjA5hD.png
With --quoted-output:

john@lithium:~/text/oddnames$ echo "$choice"
'/data/john/Dropbox/text/oddnames/"fish & chips" <what'\''s that?>'

But I'm not sure how helpful the quoting really is, though, unless you plan to feed that string straight into a shell command. If the data really has to contain linebreaks then I agree it's best to put it into a separate array, though I might be more inclined to use an associative array to map keys to data. Just my personal preference:

declare -A filelist
filelist[$name]=$data

And use the returned name(s) to access the data. In that case there's no need for a hidden column.

But my original idea (not to re-invent a file selector) was to use yad instead of an array, store the data in the hidden column and make the code a little bit simpler. As long as there are no linebreaks in the data that works.


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), now on Bluesky, there's also some GitStuff )

Introduction to the Bunsenlabs Boron Desktop

Offline

#185 2021-07-08 11:05:25

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 12,560
Website

Re: The great yad hacking thread

misko_2083 wrote:

The bash builtin ${@@Q} helps

Thanks for that! Something (else) I didn't know.
It's buried deep in the manual:

       ${parameter@operator}
              Parameter transformation.  The expansion is either a  transforma‐
              tion of the value of parameter or information about parameter it‐
              self, depending on the value of operator.   Each  operator  is  a
              single letter:

              Q      The  expansion  is a string that is the value of parameter
                     quoted in a format that can be reused as input.
              E      The expansion is a string that is the value  of  parameter
                     with  backslash  escape  sequences  expanded  as  with the
                     $'...' quoting mechanism.
              P      The expansion is a string that is the result of  expanding
                     the  value of parameter as if it were a prompt string (see
                     PROMPTING below).
              A      The expansion is a string in the  form  of  an  assignment
                     statement  or  declare  command  that,  if evaluated, will
                     recreate parameter with its attributes and value.
              a      The expansion is a string consisting of flag values repre‐
                     senting parameter's attributes.

Your odd named file would be printed like this:

'/data/john/Dropbox/text/oddnames/"fish & chips" <what'\''\'\'''\''s that?>'

You seem to have put it through an extra level of quoting there.

john@lithium:~$ s='/data/john/Dropbox/text/oddnames/"fish & chips" <what'\''s that?>'
john@lithium:~$ echo "$s"
/data/john/Dropbox/text/oddnames/"fish & chips" <what's that?>
john@lithium:~$ echo "${s@Q}"
'/data/john/Dropbox/text/oddnames/"fish & chips" <what'\''s that?>'
# there's also printf %q
john@lithium:~$ printf '%q\n' "$s"
/data/john/Dropbox/text/oddnames/\"fish\ \&\ chips\"\ \<what\'s\ that\?\>

---
Anyway, the more I think about it, the more I like your idea of putting messy data in an array, with an index in yad's hidden column. It feels quite solid.


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), now on Bluesky, there's also some GitStuff )

Introduction to the Bunsenlabs Boron Desktop

Offline

#186 2021-07-09 05:26:00

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 12,560
Website

Re: The great yad hacking thread

Changes in bash 5.1: https://lists.gnu.org/archive/html/info … 00003.html

Anyway in our 5.0.3 already, ${parameter@Q} looks somewhat useful in things like pipemenus, where a word might hold a filepath:

menuItem "$label" "bl-text-editor ${filepath@Q}"

It's not really doing a great deal - surrounds with single quotes and escapes any single quotes inside with '\'' - but it helps keep the code tidy. smile I will start using it.

Looking over the other operators available, maybe E might be useful occasionally?

              E      The expansion is a string that is the value  of  parameter
                     with  backslash  escape  sequences  expanded  as  with the
                     $'...' quoting mechanism.

If you spot any others that look handy, please share.

---
Escaping for yad text: I remembered that BL ship, in /usr/lib/bunsen/common/bl-includes, a function pangoEscape(). It was meant for passing text to jgmenu but should work just as well with yad. If you have the file on your system, just source it, or else copy/paste the function in:

pangoEscape() {
    local string="${1//&/&amp;}"
    string="${string//</&lt;}"
    string="${string//>/&gt;}"
    printf '%s' "$string"
}

Then you can use it like eg

yadlist+=(FALSE "$(pangoEscape "$name") "$data")

to escape ampersands, < and >. I'm now going to go over all my yad dialogs that might have to display weird filenames and apply it.  wink


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), now on Bluesky, there's also some GitStuff )

Introduction to the Bunsenlabs Boron Desktop

Offline

#187 2021-07-10 02:50:56

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 12,560
Website

Re: The great yad hacking thread

^thanks!

So @Q does more than just wrap single quotes - it also replaces linebreaks with \n. Good to be aware of - I'm not sure if I'd always want that to be done.

E - I'm not sure how usefull it is
It expands all of escaped characters.

Well, one obvious use might be to unescape a string that had been quoted with @Q?


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), now on Bluesky, there's also some GitStuff )

Introduction to the Bunsenlabs Boron Desktop

Offline

#188 2021-07-19 14:50:34

Sector11
Mod Squid Tpyo Knig
From: Upstairs
Registered: 2015-08-20
Posts: 8,011

Re: The great yad hacking thread

OFF TOPIC

misko_2083 wrote:

Yes but I was thinking about other realms of usefulness apart from the obvious one. smile

OK, in that case: Einstein's theory of Yad

      y²
E = ----- + d⁴
      ½a

Just kidding here!
because everyone need a chuckle every now and then.

Have a GREAT Monday.

We now return you to the topic at hand.
ON TOPIC


Debian 12 Beardog, SoxDog and still a Conky 1.9er

Offline

#189 2021-10-19 21:05:25

malm
jgmenu developer
Registered: 2016-10-13
Posts: 735
Website

Re: The great yad hacking thread

@Misko - hope you're getting better.

I came across gtkdialog and thought you might be interested.

There is a cool script here wwals by 01micko with some screenshots.

Offline

#190 2021-10-21 22:03:38

sleekmason
zoom
Registered: 2018-05-22
Posts: 1,103
Website

Re: The great yad hacking thread

Howdy all!

I found this nice little script below here on page 5 of this thread . . . https://forums.bunsenlabs.org/viewtopic … 631#p80631

And am changing it to fit what I need but have run into two issues.
The first is using $HOME in the script rather than /home/sleekmason.  When run in a terminal, the below code works just fine, but not in the yad script?

run from the script I get:

conky: cannot open $HOME/.config/conky/dark.conf: No such file or directory

Where in a terminal this works just fine.


Here's the script:

#!/bin/bash

yad --title "Conky Chooser" --button=gtk-close:1 --form --center --on-top --width=250 --height=220 --window-icon=applications-system \
    --form \
    --field="Conky Light:BTN" 'conky -c "$HOME/.config/conky/light.conf"' \
    --field="Conky Dark:BTN" 'conky -c "$HOME/.config/conky/dark.conf"' \
    --field="Conky None:BTN" 'conky -c "$HOME/.config/conky/none.conf"' \
    --field="Restart Conky":fbtn "killall -SIGUSR1 conky " \
    --field="Kill Conky":fbtn "killall conky" \

The second item is putting two commands together:
What I want to do is basically:

killall conky && conky -c  $HOME/.config/conky/dark.conf

So the conky's do not populate on top of each other.  Unfortunately I get an error here as well:

conky: no process found
&&: no process found
conky: no process found

So, I'm stuck a bit.  Could somebody who codes a bit set this right for me?

Last edited by sleekmason (2021-10-22 20:13:34)

Offline

#191 2021-10-22 08:33:12

damo
....moderator....
Registered: 2015-08-20
Posts: 6,734

Re: The great yad hacking thread

Have you tried setting $HOME to the absolute path, instead of using the variable? That is what the original script used


Be Excellent to Each Other...
The Bunsenlabs Lithium Desktop » Here
FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Offline

#192 2021-10-22 22:25:11

sleekmason
zoom
Registered: 2018-05-22
Posts: 1,103
Website

Re: The great yad hacking thread

Okay, this works for at least the $HOME part:

#!/bin/bash

yad --title "Conky Chooser" --button=gtk-close:1 --form --center --on-top --width=250 --height=220 --window-icon=applications-system \
    --form \
    --field="Conky Light":BTN "conky -c $HOME/.config/conky/light.conf" \
    --field="Conky Dark":BTN "conky -c $HOME/.config/conky/dark.conf" \
    --field="Conky None":BTN "conky -c $HOME/.config/conky/none.conf" \
    --field="Restart Conky":fbtn "killall -SIGUSR1 conky" \
    --field="Kill Conky":fbtn "killall conky" \

Then to add a killall before conky -c . . . but, it looks like adding the killall creates its own problem. If there is no conky running, the line stops after the killall, ignoring the conky -c $HOME/.config/conky/none.conf

Maybe there is no way to make killall(or pkill) work within the line?

Last edited by sleekmason (2021-10-22 22:26:14)

Offline

#193 2021-10-23 02:03:30

sleekmason
zoom
Registered: 2018-05-22
Posts: 1,103
Website

Re: The great yad hacking thread

Figured out a good fix. Put the commands into a separate script to call from the yad script.  Works quite well.  In this case, I'm using toggles to individually turn on and off the conky's like so.

#!/bin/bash

yad --title "Conky" --button=gtk-close:1 --form --center --on-top --width=250 \
--height=305 --text="\n CHOOSE YOUR\n SELECTIONS. \n" --text-align=center \
--window-icon=applications-system \
    --form \
    --field="Conky Light":BTN "toggle.conky-light" \
    --field="Conky Dark":BTN "toggle.conky-dark" \
    --field="Conky None":BTN "toggle.conky-none" \
    --field="Restart Conky":fbtn "killall -SIGUSR1 conky" \
    --field="Kill Conky":fbtn "killall conky" \

And then for the toggle:

#!/bin/bash
# conky-light - toggle individual conky on/off

if pgrep -f conky-light.conf > /dev/null; then
    pkill -f conky-light.conf
else
    conky -c $HOME/.config/conky/conky-light.conf
fi

This gives all sorts of control from the toggle script.

Last edited by sleekmason (2021-10-23 02:16:44)

Offline

#194 2021-10-23 13:20:28

sleekmason
zoom
Registered: 2018-05-22
Posts: 1,103
Website

Re: The great yad hacking thread

@misko_2083  -  Brilliant!  Exactly what I was looking for but didn't know how to accomplish:)  Thank you for narrowing it down!

Offline

#195 2021-11-27 00:55:46

achim
Member
Registered: 2019-08-23
Posts: 40

Re: The great yad hacking thread

Hi,

The following code is a downloader version that I have further developed with yt-dlp, an alternative to youtube-dl. It works so far, only with the progress indicator in lines 30 to 40 I have a problem.
Perhaps someone from the experts present here (misko_2083 or damo) can check where the error is.
I am also interested in whether the output (e.g. lines 52, 55, 81 etc.) can also be colored.

Thanks very much

#!/bin/bash
#
fd="/home/achim/bin/icons"
TITLE="Video Downloader by Achim"                 # dialog title
#
cd $HOME/Videos/Youtube-Videos
#
export ytdownload='@bash -c "download_video %1"'
#
# We need this to store the youtube PID
export ytdpid=$(mktemp -u --tmpdir ytpid.XXXXXXXX)
#
export ytdpipe=$(mktemp -u --tmpdir ytd.XXXXXXXX)
mkfifo "$ytdpipe"
export ytdpipetwo=$(mktemp -u --tmpdir ytd2.XXXXXXXX)
mkfifo "$ytdpipetwo"
#
trap "rm -f $ytdpipe $ytdpipetwo $ytdpid" EXIT
#
ytdkey=$(($RANDOM * $$))
#
function download_video
{
  echo "2:@disable@"
# Check if the URL is valid with the spider
if wget -q --spider "$1"; then
  echo "#Download wird vorbereitet..." >> "$ytdpipe"
#
   >"$ytdpid"
   while read line; do
     if [[ "$(echo $line | grep '[0-9]*%')" ]];then
        percent=$(echo $line | awk '{print $2}')
        echo "${percent%.*}%" >> "$ytdpipe"
     fi
#
     if [[ "$(echo $line | grep '\[download\]')" ]];then
        progress=$(echo $line | awk '{$1=""; print $0}')
        echo "#$progress" >> "$ytdpipe"
     fi
   done < "$ytdpipetwo" &
   LOOP_PID="$!"
#
    yt-dlp -f 'bv*[ext=mp4]+ba[ext=m4a]/b[ext=mp4] / bv*+ba/b' "$1" 2>&1 >> $ytdpipetwo & echo $! > "$ytdpid"
    
    #yt-dlp -f 'bv,ba' -o '%(title)s.f%(format_id)s.%(ext)s' "$1" 2>&1 >> $ytdpipetwo & echo $! > "$ytdpid"
    #yt-dlp -f 'bestvideo [ext = mp4] + bestaudio [ext = m4a] / bestvideo + bestaudio' --merge-output-format mp4 --newline -i -o "%(title)s.%(ext)s" "$1" 2>&1 >> $ytdpipetwo & echo $! > "$ytdpid"
    wait $!
#
    if [[ "$?" = 0 ]]
      then
         echo "100%" >> "$ytdpipe"
         echo "#Download erfolgreich abgeschlossen!" >> "$ytdpipe"
         kill "$LOOP_PID"
    elif [[ ! -s "$ytdpid" ]]; then
         echo "#Download abgebrochen!" >> "$ytdpipe"
         kill "$LOOP_PID"
    else
         echo "#Download Fehler!" >> "$ytdpipe"
         kill "$LOOP_PID"
    fi
else
    echo "#ungültige URL!" >> "$ytdpipe"
fi
#
    echo "2:$ytdownload"
}
export -f download_video
#
function ytdl_version () {
    echo "#Versionscheck der letzten Version" >> "$ytdpipe"
    sudo wget https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp -O /tmp/yt2-dlp && sudo chmod a+rx /tmp/yt2-dlp /dev/null
    if [[ "$?" -ne "0" ]]; then
       echo "#Kann keine Verbindung zum Server herstellen!" >> "$ytdpipe"
    fi
    sleep 3
    ytdlcv=$(yt-dlp --version)
    ytdllv=$(/tmp/yt2-dlp --version)
    

    if [[ "$ytdlcv" == "$ytdllv" ]]; then
    echo "#yt-dlp ist aktuell - Version $ytdlcv " >> "$ytdpipe"
    else
    echo "#Es ist ein neues yt-dlp Update verfügbar!" >> "$ytdpipe"
    sudo cp -a /tmp/yt2-dlp  /usr/local/bin/yt-dlp    
    echo "Die yt-dlp wird aktualisiert..." | GTK_THEME="gtk-3.0" yad --borders=50 \
   --window-icon="$fd/Movies-icon24.png" --title="Update" \
   --text-info --posx=700 --posy=600 --width=540 --no-buttons --timeout=4 --timeout-indicator=Bottom
   echo "#Es wurde die neueste yt-dlp Version $ytdllv installiert!" >> "$ytdpipe"
    sleep 5   
    fi
 #   
}
#
function get_pid_and_kill () {
if [[ -s "$ytdpid" ]]; then
  BCKUPID="$(<$ytdpid)"
  >"$ytdpid"
  kill $BCKUPID 2>/dev/null
fi
}
export -f get_pid_and_kill
#
exec 3<> $ytdpipe
exec 4<> $ytdpipetwo
#
GTK_THEME="gtk-3.0" yad --plug="$ytdkey" \
	--tabnum=1 \
	--image-on-top \
    --image="$fd/Gnome-Video-X-Generic-64.png" \
	--form \
	--field "Bitte hier die Video-Url eingeben: ":CE "https://www.youtube.com/" \
    --command=CMD \
    --field="Download!$fd/Gnome-Emblem-Downloads-32.png:fbtn" "$ytdownload" &    
#
GTK_THEME="gtk-3.0" yad --plug="$ytdkey" \
	--tabnum=2 \
    --progress \
    --borders=6 <&3 &
#
  ytdl_version &
#
GTK_THEME="gtk-3.0" yad --paned --key="$ytdkey"  \
    --width=1500 \
	--height=400 \
    --title="$TITLE" \
	--center --borders=6 \
    --text="" \
    --window-icon="$fd/youtube-dl-gui64.png" \
	--button="Download stoppen!$fd/Status-dialog-error-icon24.png":"bash -c get_pid_and_kill" \
    --button=" Schließen!$fd/Apps-session-logout-icon.png":1
#
#
if exist file *.part &>/dev/null
then
rm *part*
fi 
#
ret=$?
#
if [[ $ret -eq 252 ]]; then
    if file *.part &>/dev/null
    then
    rm *part*
    fi 
fi
#
[[ $ret -eq 1 ]] && exit 0
#
exec 3>&-
exec 4>&-
#
get_pid_and_kill
exit 0

Last edited by achim (2021-11-27 02:19:41)

Offline

#196 2021-11-30 06:33:09

achim
Member
Registered: 2019-08-23
Posts: 40

Re: The great yad hacking thread

@misko_2083
I am currently reading about your illness and, of course, I wish you a speedy recovery. You are probably still young and you will probably survive this terrible virus story better than an older person. I belong to this group, which is why there was also the booster vaccination.

All the best for you and get really well again.

Offline

#197 2021-12-11 00:04:23

achim
Member
Registered: 2019-08-23
Posts: 40

Re: The great yad hacking thread

misko_2083 wrote:

Well, thank you achim, I'm very good now.

That makes me very happy!

    yt-dlp -f 'bv*[ext=mp4]+ba[ext=m4a]/b[ext=mp4] / bv*+ba/b' "$1" 2>&1 >> $ytdpipetwo & echo $! > "$ytdpid"

No, that's a misunderstanding. This line (number 43) is present in the script. Except for the progress bar in lines 30 to 40, almost everything works. I can't find the bug.

I also asked whether the output (e.g. lines 52, 55, 81 etc.) could also be designed in color.

Offline

#198 2021-12-12 00:51:41

achim
Member
Registered: 2019-08-23
Posts: 40

Re: The great yad hacking thread

Thank you @misko_2083, the progress bar is now working. In order to achieve a colored text output, however, I still have to experiment a little.

Last edited by achim (2021-12-12 00:53:09)

Offline

#199 2022-03-01 15:44:30

rasat
New Member
Registered: 2022-03-01
Posts: 2

Re: The great yad hacking thread

I have been searching for a forum that discusses yad scripts and examples. Found it in this thread.

I am creating one Fvwm extension installer with "yad --checklist --list". Each checklist item has a different image. My question is, can "--image" option be used multiple times? To change the image when highlighting a checklist item. Here are a few checklist items.

#!/bin/bash
yad --title "Extension Installer" --checklist --list --width=900 --height=650 --separator=, --column=Y/N --column=No: \
--column="Ext type" --column=Extension --column=Description \

false 101 Functions "Auto Hide List" "Short description." --image=auto-hide.png \
false 102 Functions "Auto Move Windows" "Short description." --image=auto-move.png \
false 201 Styles "Diary Border Style" "Short description." --image=diary-style.png \
false 202 Styles "DiaryThumbnails" "Short description."  --image=diary-thumb.png \

extension-Installer.png

Last edited by rasat (2022-03-01 15:54:39)

Offline

#200 2022-03-03 11:02:59

sleekmason
zoom
Registered: 2018-05-22
Posts: 1,103
Website

Re: The great yad hacking thread

Not aware if images can be used in the manner you are suggesting, but maybe icons can?. For my own uses, I create icons with the desired size and image and just point to them directly for my buttons and so forth.

Offline

Board footer

Powered by FluxBB