YAD - tips'n'tricks

For discussions about programming, and for programming questions and advice


Moderator: Forum moderators

User avatar
stemsee
Posts: 658
Joined: Sun Jul 26, 2020 8:11 am
Location: lattitude 0
Has thanked: 162 times
Been thanked: 104 times
Contact:

Re: YAD - tips'n'tricks

Post by stemsee »

misko_2083 wrote: Thu Mar 10, 2022 3:26 pm

Screw the development of yad, who can worry about that now, I hope he and his family make it alive.

Probably keeps his mind balanced, and focused.

Going to the war? You are romanticizing silly, you don't know what the war is. It's nothing close to the movies.

I would be quite ok ... I rarely watch movies.

User avatar
misko_2083
Posts: 196
Joined: Wed Dec 09, 2020 11:59 pm
Has thanked: 10 times
Been thanked: 20 times

Re: YAD - tips'n'tricks

Post by misko_2083 »

stemsee wrote: Thu Mar 10, 2022 3:49 pm
misko_2083 wrote: Thu Mar 10, 2022 3:26 pm

Screw the development of yad, who can worry about that now, I hope he and his family make it alive.

Probably keeps his mind balanced, and focused.

That comment was aiming your concerns about the development of yad.
It's selfish to think like that Stemsee.

stemsee wrote: Thu Mar 10, 2022 3:49 pm

I would be quite ok ... I rarely watch movies.

Alrighty then. Ace Ventura: Pet Detective(1994)

Do you want to exit the Circus? The Harsh Truth
https://www.youtube.com/watch?v=ZJwQicZHp_c

User avatar
Grey
Posts: 2003
Joined: Wed Jul 22, 2020 12:33 am
Location: Russia
Has thanked: 75 times
Been thanked: 365 times

Re: YAD - tips'n'tricks

Post by Grey »

Hey, why did everyone stop adding something like "now back to work (scripts, yad)" at the end of their post :) ?
This approach made it possible to turn off the forest path, but continue to see its contours in the night darkness. Well, that is, it was possible not to stray far from the topic.

Fossapup OS, Ryzen 5 3600 CPU, 64 GB RAM, GeForce GTX 1050 Ti 4 GB, Sound Blaster Audigy Rx with amplifier + Yamaha speakers for loud sound, USB Sound Blaster X-Fi Surround 5.1 Pro V3 + headphones for quiet sound.

User avatar
stemsee
Posts: 658
Joined: Sun Jul 26, 2020 8:11 am
Location: lattitude 0
Has thanked: 162 times
Been thanked: 104 times
Contact:

Re: YAD - tips'n'tricks

Post by stemsee »

misko_2083 wrote: Thu Mar 10, 2022 4:51 pm
stemsee wrote: Thu Mar 10, 2022 3:49 pm
misko_2083 wrote: Thu Mar 10, 2022 3:26 pm

Screw the development of yad, who can worry about that now, I hope he and his family make it alive.

Probably keeps his mind balanced, and focused.

That comment was aiming your concerns about the development of yad.
It's selfish to think like that Stemsee.

My meaning is that sometimes if I'm confronted with a problem out of my control instead of being anxious and pacing up and down, I work on a gui or program which is a productive way of dealing with stress. I am not concerned about the development of yad, simply because yad has limitations. Only by learning a full-featured gui program can the potential of an interface be fully utilised, like gtkdialog, qt etc

Yes he does have other things to keep him busy, so why does he continue development at a time like this? I presume it is to keep his mind off of stressful things out of his control.

Now back on topic!

User avatar
misko_2083
Posts: 196
Joined: Wed Dec 09, 2020 11:59 pm
Has thanked: 10 times
Been thanked: 20 times

Re: YAD - tips'n'tricks

Post by misko_2083 »

I have this script to shuffle backgrounds.
It creates a notification icon and right click menu.
Selecting One Minute spawns run_cmd process and selecting Off kills it.
But there is a persistent sleep command process left.
It will eventually exit when the counter ends but is there a way to kill it when the parent process (run_cmd) is killed?
Image

Code: Select all

#!/usr/bin/env bash

# add handler to manage process shutdown
on_exit () {
    trap SIGINT              # Resore signal handling for SIGINT
    echo "quit" >&3
    rm -f $PIPE
    local proc_id="$(ps -eo pid,cmd | grep "bash -c run_cmd *" | grep -v "grep" | awk '{ print $1 }')"
    for pid in $(tac -s" " <<< "$proc_id "); do
        kill -SIGTERM $pid
    done
}
export -f on_exit

trap 'on_exit' 0 1 2 15

# function to change the wallpaper
function change_wallpaper() {
    feh --bg-scale "$(find "$HOME/Pictures/wallpapers" -type f -name '*.jpg' -o -name '*.png' | shuf -n 1)"
}
export -f change_wallpaper

# add handler for tray icon left click
function on_click() {
    yad --form --on-top --timeout=5 --geometry="400x70-10-35" \
        --field="Shuffle Wallpaper:fbtn" 'bash -c change_wallpaper' \
        --image-on-top --no-markup --text="Click to shuffle wallpaper" \
        --undecorated --no-buttons --skip-taskbar
}
export -f on_click

# run cmd
function run_cmd() {
    local proc_id="$(ps -eo pid,cmd | grep "bash -c run_cmd *" | grep -v "grep" | awk '{ print $1 }')"
    case $1 in
        off) for pid in $(tac -s" " <<< "$proc_id "); do
                     kill -SIGTERM $pid
             done ;;
        *)   if [[ "$(echo $proc_id | wc -w)" -gt "2" ]] ; then
                 for pid in $(tac -s" " <<< "$proc_id " | head -2); do
                     kill -SIGTERM $pid
                 done
             fi ;;
    esac

    while true ; do
        change_wallpaper
        sleep $1
    done
}
export -f run_cmd

PIPE=/tmp/${0##*/}.$RANDOM
mkfifo $PIPE
# attach a file descriptor to the file
exec 3<> $PIPE

# create the notification icon
yad --notification --no-markup     \
    --listen                       \
    --command="bash -c on_click" 2> /dev/null <&3 & NPID=$!

# set notification right-click menu
echo "menu:Off! bash -c 'run_cmd off'!$LAUNCHDIR/icons/pause_play.png|| \
One Minute! bash -c 'run_cmd 60'!$LAUNCHDIR/icons/pause_play.png| \
Five Minutes! bash -c 'run_cmd 5m'!$LAUNCHDIR/icons/edit.png| \
Ten Minutes! bash -c 'run_cmd 10m'!$LAUNCHDIR/icons/volume.png| \
Half An Hour! bash -c  'run_cmd 30m'!$LAUNCHDIR/icons/record.png| \
One Hour! bash -c  'run_cmd 60m'!$LAUNCHDIR/icons/record.png| \
Two Hours! bash -c  'run_cmd 2h'!$LAUNCHDIR/icons/record.png| \
Four Hours! bash -c  'run_cmd 4h'!$LAUNCHDIR/icons/record.png| \
Eight Hours! bash -c  'run_cmd 8h'!$LAUNCHDIR/icons/record.png| \
Once a Day! bash -c  'run_cmd 24h'!$LAUNCHDIR/icons/record.png|| \
Exit!quit!$LAUNCHDIR/icons/quit.png " >&3

# set notification icon
echo "icon:/usr/share/icons/Tango/16x16/places/desktop.png" >&3

# wait for notification to quit
wait $NPID 2>/dev/null

Do you want to exit the Circus? The Harsh Truth
https://www.youtube.com/watch?v=ZJwQicZHp_c

User avatar
stemsee
Posts: 658
Joined: Sun Jul 26, 2020 8:11 am
Location: lattitude 0
Has thanked: 162 times
Been thanked: 104 times
Contact:

Re: YAD - tips'n'tricks

Post by stemsee »

maybe ...

Code: Select all

on_exit () {
    trap SIGINT              # Resore signal handling for SIGINT
    echo "quit" >&3
    rm -f $PIPE
    local proc_id="$(ps -eo pid,cmd | grep "bash -c run_cmd *" | grep -v "grep" | awk '{ print $1 }')"
    for pid in $(tac -s" " <<< "$proc_id ") $(pgrep -n sleep); do
       kill -SIGTERM $pid
    done
}
export -f on_exit
User avatar
misko_2083
Posts: 196
Joined: Wed Dec 09, 2020 11:59 pm
Has thanked: 10 times
Been thanked: 20 times

Re: YAD - tips'n'tricks

Post by misko_2083 »

Thank you but it's not a problem in function on_exit but terminating sleep when a top most option in the right click menu is selected.
It need a trap to kill it and we find out process id with the jobs, bash builtin.
Sleep will not receive the signal so we run sleep in the background and use wait to receive the signal.
And it still didn't trigger the trap until exit is called at the end.
Trap + sleep to background + wait + exit, all that only to SIGTERM sleep. :roll:

Code: Select all

    kill_sleep() {
        kill -SIGTERM $(jobs -p)
        exit #$
    }
    trap kill_sleep SIGTERM

    while true ; do
        change_wallpaper
        sleep $1 &
        wait
    done
    exit

I've changed the name of run_cmd function to wallpaper_shuffler. It's descriptive and run_cmd was only an arbitrary name.
I've split the function and made a separate one to kill the wallpaper_shuffler because why not.

Code: Select all

#!/usr/bin/env bash

# add handler to manage process shutdown
on_exit () {
    trap SIGINT              # Resore signal handling for SIGINT
    echo "quit" >&3
    rm -f $PIPE
    local proc_id="$(ps -eo pid,cmd | grep "bash -c wallpaper_shuffler *" | grep -v "grep" | awk '{ print $1 }')"
    for pid in $(tac -s" " <<< "$proc_id "); do
        kill -SIGTERM $pid
    done
}
export -f on_exit

trap 'on_exit' 0 1 2 15

# function to change the wallpaper
function change_wallpaper() {
    feh --bg-scale "$(find "$HOME/Pictures/wallpapers" -type f -name '*.jpg' -o -name '*.png' | shuf -n 1)"
}
export -f change_wallpaper

# add handler for tray icon left click
function on_click() {
    yad --form --on-top --timeout=5 --geometry="400x70-10-35" \
        --field="Shuffle Wallpaper:fbtn" 'bash -c change_wallpaper' \
        --image-on-top --no-markup --text="Click to shuffle wallpaper" \
        --undecorated --no-buttons --skip-taskbar
}
export -f on_click

# on kill command handler
on_kill() {
    local proc_id="$(ps -eo pid,cmd | grep "bash -c wallpaper_shuffler *" | grep -v "grep" | awk '{ print $1 }')"
    for pid in $proc_id ; do
        kill -SIGTERM $pid
    done 
}
export -f on_kill

# run shuffler
function wallpaper_shuffler() {
    # handle sleep leftover process
    kill_sleep() {
        kill -SIGTERM $(jobs -p)
        exit #$
    }
    trap kill_sleep SIGTERM

    # fetch all processes this function spawns
    local proc_id="$(ps -eo pid,cmd | grep "bash -c wallpaper_shuffler *" | grep -v "grep" | awk '{ print $1 }')"
    # If the are more than two kill them but leave this one
    if [[ "$(echo $proc_id | wc -w)" -gt "2" ]] ; then
        # reverse order, skip the last two pids
        for pid in $(tac -s" " <<< "$proc_id " | head -2); do
            kill -SIGTERM $pid
         done
    fi
    while true ; do
        change_wallpaper
        sleep $1 &
        wait
    done
    exit
}
export -f wallpaper_shuffler


PIPE=/tmp/${0##*/}.$RANDOM
mkfifo $PIPE
# attach a file descriptor to the file
exec 3<> $PIPE

# create the notification icon
yad --notification --no-markup     \
    --listen                       \
    --command="bash -c on_click" 2> /dev/null <&3 & NPID=$!

# set notification right-click menu
echo "menu:Off! bash -c 'on_kill'!$LAUNCHDIR/icons/pause_play.png|| \
One Minute! bash -c 'wallpaper_shuffler 60'!$LAUNCHDIR/icons/pause_play.png| \
Five Minutes! bash -c 'wallpaper_shuffler 5m'!$LAUNCHDIR/icons/edit.png| \
Ten Minutes! bash -c 'wallpaper_shuffler 10m'!$LAUNCHDIR/icons/volume.png| \
Half An Hour! bash -c  'wallpaper_shuffler 30m'!$LAUNCHDIR/icons/record.png| \
One Hour! bash -c  'wallpaper_shuffler 60m'!$LAUNCHDIR/icons/record.png| \
Two Hours! bash -c  'wallpaper_shuffler 2h'!$LAUNCHDIR/icons/record.png| \
Four Hours! bash -c  'wallpaper_shuffler 4h'!$LAUNCHDIR/icons/record.png| \
Eight Hours! bash -c  'wallpaper_shuffler 8h'!$LAUNCHDIR/icons/record.png| \
Once a Day! bash -c  'wallpaper_shuffler 24h'!$LAUNCHDIR/icons/record.png|| \
Exit!quit!$LAUNCHDIR/icons/quit.png " >&3

# set notification icon
echo "icon:/usr/share/icons/Tango/16x16/places/desktop.png" >&3

# wait for notification to quit
wait $NPID 2>/dev/null

Do you want to exit the Circus? The Harsh Truth
https://www.youtube.com/watch?v=ZJwQicZHp_c

step
Posts: 516
Joined: Thu Aug 13, 2020 9:55 am
Has thanked: 50 times
Been thanked: 184 times
Contact:

Re: YAD - tips'n'tricks

Post by step »

@misko_2083 :idea: for ((i=0; i< 3; i++)); do yad --no-buttons --splash --timeout=1; echo "wake up, do something"; done

User avatar
misko_2083
Posts: 196
Joined: Wed Dec 09, 2020 11:59 pm
Has thanked: 10 times
Been thanked: 20 times

Re: YAD - tips'n'tricks

Post by misko_2083 »

step wrote: Fri Mar 25, 2022 10:50 am

@misko_2083 :idea: for ((i=0; i< 3; i++)); do yad --no-buttons --splash --timeout=1; echo "wake up, do something"; done

Blinking yad while waiting.
Some suggest read command. Don't know if that is a good idea.
https://unix.stackexchange.com/question ... p-command/

Do you want to exit the Circus? The Harsh Truth
https://www.youtube.com/watch?v=ZJwQicZHp_c

step
Posts: 516
Joined: Thu Aug 13, 2020 9:55 am
Has thanked: 50 times
Been thanked: 184 times
Contact:

Re: YAD - tips'n'tricks

Post by step »

misko_2083 wrote: Fri Mar 25, 2022 12:11 pm
step wrote: Fri Mar 25, 2022 10:50 am

@misko_2083 :idea: for ((i=0; i< 3; i++)); do yad --no-buttons --splash --timeout=1; echo "wake up, do something"; done

Blinking yad while waiting.

Not exactly. My point was that you can avoid sleep altogether by using yad instead of sleep. Yad can be killed very easily while it's waiting without traps. Prrof:

Code: Select all

date +%s; yad --timeout=100 & ; kill %; date +%s
1648211273
[1] 20595
[1]  + 20595 terminated  yad --timeout=100
1648211273

You get the idea. Besides, talking about yad in the "YAD - tips'n'tricks" topic has its own merits ;) Of course, this yad wouldn't replace your main yad dialog; this yad would replace your use of sleep.

You wrote:

Blinking yad

On my system (specs: Fatdog64-812, openbox, lxqt-panel, yad 0.42.42 gtk3) yad --no-buttons --splash --timeout N is invisible. Is it visible on yours? What's blinking? What are your system specs?

Some suggest read command. Don't know if that is a good idea.
https://unix.stackexchange.com/question ... p-command/

Let me take a look, I'll be back...

User avatar
misko_2083
Posts: 196
Joined: Wed Dec 09, 2020 11:59 pm
Has thanked: 10 times
Been thanked: 20 times

Re: YAD - tips'n'tricks

Post by misko_2083 »

step wrote: Fri Mar 25, 2022 12:35 pm

On my system (specs: Fatdog64-812, openbox, lxqt-panel, yad 0.42.42 gtk3) yad --no-buttons --splash --timeout N is invisible. Is it visible on yours? What's blinking? What are your system specs?

for ((i=0; i< 3; i++)); do yad --timeout=1;done
yad opens and waits for a second timeout kill it X3 :D
Too much resources.
But it can be useful in some cases.

Code: Select all

if yad --timeout=10 --timeout-indicator=bottom --text="Proceed or cancel.\nThis will automatically proceed in 10 seconds"; then echo "do something"; fi

Do you want to exit the Circus? The Harsh Truth
https://www.youtube.com/watch?v=ZJwQicZHp_c

step
Posts: 516
Joined: Thu Aug 13, 2020 9:55 am
Has thanked: 50 times
Been thanked: 184 times
Contact:

Re: YAD - tips'n'tricks

Post by step »

misko_2083 wrote: Fri Mar 25, 2022 12:49 pm
step wrote: Fri Mar 25, 2022 12:35 pm

On my system (specs: Fatdog64-812, openbox, lxqt-panel, yad 0.42.42 gtk3) yad --no-buttons --splash --timeout N is invisible. Is it visible on yours? What's blinking? What are your system specs?

for ((i=0; i< 3; i++)); do yad --timeout=1;done
yad opens and waits for a second timeout kill it X3 :D
Too much resources.
But it can be useful in some cases.

Code: Select all

if yad --timeout=10 --timeout-indicator=bottom --text="Proceed or cancel.\nThis will automatically proceed in 10 seconds"; then echo "do something"; fi

I think we're talking about different things. OK, never mind. Back to your previous question, you wrote:

Blinking yad
[...]
Some suggest read command. Don't know if that is a good idea.
https://unix.stackexchange.com/question ... p-command/

So it seems you want to avoid busy waiting in your script? This question isn't strictly about yad. It's a general shell programming question that would probably get more reads (answers) in a dedicated thread. But I read the stackexchange you posted, and it seems to me that bolt's blog post can be studied and an ingenious solution be found in it. While the bash process is reading, it's essentially off the kernel scheduler, so technically bolt's idea is not-busy-waiting as it can get. It's for linux only, it should be noted.

I can offer another idea. I'm not sure how your yad script works -- if it's supposed to run only once while multiple timeouts take place, or if it's a main loop that runs yad then "sleeps" then run yads... and so on. In either case you may be able to think of your script as an action connected to a non-expiring timer. And in puppyland where can one easily get a programmable timer with shell actions? The gtkdialog timer widget is an answer. Look at the "timer_advanced" sample script under /usr/share/doc/gtkdialog/examples/timer (also available in woof-CE on github). You can associate any shell command to run when the timer expires (in a loop if necessary). So, basically you can use gtkdialog as an equivalent of while true; do echo "wake up, do something"; sleep 60l done. You could replace yad altogether with gtkdialog or keep gtkdialog and yad separate, each running in its own process. The disadvantage of using gtkdialog (or even a separate bash process) to tick the timer outside your main yad GUI process is that you will need to distribute two separate scripts for a single application. In these cases I typically bundle the scripts together as a Rox AppDir (or you could use a portable bundle, such as an AppImage).

User avatar
misko_2083
Posts: 196
Joined: Wed Dec 09, 2020 11:59 pm
Has thanked: 10 times
Been thanked: 20 times

Re: YAD - tips'n'tricks

Post by misko_2083 »

It runs a loop, when another right click menu item is selected it kills it and starts another loop.
The trouble was that sleep was continuing to run.
All sorted out now thanks Step.

Do you want to exit the Circus? The Harsh Truth
https://www.youtube.com/watch?v=ZJwQicZHp_c

User avatar
misko_2083
Posts: 196
Joined: Wed Dec 09, 2020 11:59 pm
Has thanked: 10 times
Been thanked: 20 times

Re: YAD - tips'n'tricks

Post by misko_2083 »

@step I added a switch in a form dialog today.
Image

I didn't know --changed action was added, there is --changed-action callback for switch, check, and combo fields.

Code: Select all

yad --form --field="Check":CHK --field="Switch":SW --field="Switch 2":SW "FALSE" "TRUE" "TRUE" --changed-action 'yad --text="%1 %2 %3"'

It can slow down the dialog but for some quick commands it's great.

Do you want to exit the Circus? The Harsh Truth
https://www.youtube.com/watch?v=ZJwQicZHp_c

step
Posts: 516
Joined: Thu Aug 13, 2020 9:55 am
Has thanked: 50 times
Been thanked: 184 times
Contact:

Re: YAD - tips'n'tricks

Post by step »

misko_2083 wrote: Wed Apr 13, 2022 11:17 pm

@step I added a switch in a form dialog today.

It looks great.

I didn't know --changed action was added, there is --changed-action callback for switch, check, and combo fields.

Neither did I. I don't follow the changes in the official yad project regularly. From time to time I review them and fish for changes that can be added to gtkdialog gtk2 without too much fuss. What yad version are you using? Mine is

Code: Select all

# yad --version
0.42.42 (GTK+ 2.24.33)
User avatar
misko_2083
Posts: 196
Joined: Wed Dec 09, 2020 11:59 pm
Has thanked: 10 times
Been thanked: 20 times

Re: YAD - tips'n'tricks

Post by misko_2083 »

step wrote: Thu Apr 14, 2022 4:41 am

What yad version are you using?

The latest from master branch with Gtk3.
There are new dialogs and a lot of options to try out.

Victor is also working on ydesk, have you tried it?
https://github.com/v1cont/ydesk

Do you want to exit the Circus? The Harsh Truth
https://www.youtube.com/watch?v=ZJwQicZHp_c

step
Posts: 516
Joined: Thu Aug 13, 2020 9:55 am
Has thanked: 50 times
Been thanked: 184 times
Contact:

Re: YAD - tips'n'tricks

Post by step »

misko_2083 wrote: Thu Apr 14, 2022 11:18 am

Victor is also working on ydesk, have you tried it?
https://github.com/v1cont/ydesk

No, I haven't. I wasn't aware of ydesk. Thanks.

User avatar
misko_2083
Posts: 196
Joined: Wed Dec 09, 2020 11:59 pm
Has thanked: 10 times
Been thanked: 20 times

Re: YAD - tips'n'tricks

Post by misko_2083 »

Next I'm going to make image set and clear from stdin in the picture dialog.

Do you want to exit the Circus? The Harsh Truth
https://www.youtube.com/watch?v=ZJwQicZHp_c

User avatar
stemsee
Posts: 658
Joined: Sun Jul 26, 2020 8:11 am
Location: lattitude 0
Has thanked: 162 times
Been thanked: 104 times
Contact:

Re: YAD - tips'n'tricks

Post by stemsee »

I want to program a simple looping drum machine using yad as the frontend. The idea being that when a button is pressed it plays an associated sound and that re-occurs as part of a loop of 4/8/16/32 bars. So it is additive.

I think that yad --form or yad --icons might provide a usable gui.

Any input welcome. This image is yad --form with svg colour images which would be replaced with various percussion svg images

drum.png
drum.png (23.27 KiB) Viewed 2139 times
User avatar
misko_2083
Posts: 196
Joined: Wed Dec 09, 2020 11:59 pm
Has thanked: 10 times
Been thanked: 20 times

Re: YAD - tips'n'tricks

Post by misko_2083 »

stemsee wrote: Fri Jun 17, 2022 2:54 pm

I want to program a simple looping drum machine using yad as the frontend. The idea being that when a button is pressed it plays an associated sound and that re-occurs as part of a loop of 4/8/16/32 bars. So it is additive.

I think that yad --form or yad --icons might provide a usable gui.

Any input welcome. This image is yad --form with svg colour images which would be replaced with various percussion svg images

drum.png

Well at least you can use it for prank call if nothing else. :D
Maybe a paned dialog would be good with another form in the other tab to list, play and delete selected sounds.
I don't know if there are Unicode emojis for all the instruments but it an option to consider instead of svg.
It would load faster and take less space.

Maybe it's a good idea to start backing scripts and C from me from here and elsewhere, cause they'll be gone soon.
Thought to give you heads up. I not an administrator to give you some time, it's all up to them.

Thanks for everything, it was fun. :D

Do you want to exit the Circus? The Harsh Truth
https://www.youtube.com/watch?v=ZJwQicZHp_c

User avatar
stemsee
Posts: 658
Joined: Sun Jul 26, 2020 8:11 am
Location: lattitude 0
Has thanked: 162 times
Been thanked: 104 times
Contact:

Re: YAD - tips'n'tricks

Post by stemsee »

Thanks Misko!

Well, I'm not sure what you refer to by mentioning that your scripts will be gone soon ... I hope not!

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

Yad features added recently include --line for use in text-info. Jumps straight to a line number.
Pango markup available for use in yad --form --field=:TXT .... this means custom fonts and colours within a form!

I want splitter position to be as a percentage which is enforced as yad gui is resized.

User avatar
stemsee
Posts: 658
Joined: Sun Jul 26, 2020 8:11 am
Location: lattitude 0
Has thanked: 162 times
Been thanked: 104 times
Contact:

Re: YAD - tips'n'tricks

Post by stemsee »

Yad --form is a very versatile option. You could write a very interactive frontend using only yad --form.

One field type is NUM. You can set the values which determine how the input arrows operate.
for example

Code: Select all

yad --form --field="Value":NUM "10[!10..200[!4[!1]]]"

the first number 10 is the initial value displayed.
10..200 is the number range to make available
4 is the step size, so clicking the up arrow would result in 10 14 18 22 ... etc up to 200 (198). This corresponds to the seq command

Code: Select all

seq 10 4 200

the last 1 limits the decimal precision to 1 decimal place 0.1 . I do not know the maximum value possible.

step
Posts: 516
Joined: Thu Aug 13, 2020 9:55 am
Has thanked: 50 times
Been thanked: 184 times
Contact:

Re: YAD - tips'n'tricks

Post by step »

stemsee wrote: Thu Jul 07, 2022 9:18 am

[...] I do not know the maximum value possible.

Experimentally and apparently, with yad-master --form --field='Value':NUM '10[!10..200[!4[!100]]]'

screen-20220710.png
screen-20220710.png (7.35 KiB) Viewed 2458 times

I can count 20 decimal places in the screenshot above.

User avatar
misko_2083
Posts: 196
Joined: Wed Dec 09, 2020 11:59 pm
Has thanked: 10 times
Been thanked: 20 times

Re: YAD - tips'n'tricks

Post by misko_2083 »

step wrote: Sun Jul 10, 2022 11:05 am

Experimentally and apparently, with yad-master --form --field='Value':NUM '10[!10..200[!4[!100]]]'

:roll:

stemsee wrote: Thu Jul 07, 2022 9:18 am

for example

Code: Select all

yad --form --field="Value":NUM "10[!10..200[!4[!1]]]"

:roll:

What's in brackets is optional. [ ]
It's a standard in all manuals.
When you have brackets inside the brackets it mean that optional value has an optional sub-value.

Simplest value is like this:

Code: Select all

yad --form --field='Value':NUM '10'

then you can set boundaries, minimal and maximum values:

Code: Select all

yad --form --field='Value':NUM '10!10..200'

next is increment, default increment is 1

Code: Select all

yad --form --field='Value':NUM '10!10..200!5'

the last one sets the precision, how many places behind zero.

Code: Select all

yad --form --field='Value':NUM '10!10..200!5!2'

if there is the last sub-value, then the increment can be a floating.

Code: Select all

yad --form --field='Value':NUM '10!10..200!0.01!2'

Code: Select all

yad --form --field='Value':NUM '10!10..200!1.01!2'

100 is a lot, 20 looks like a maximum.

Code: Select all

yad --form --field='Value':NUM '10!10..200!0.001!100'
10.00999999999999978684|

also a maximum precision is 0.0000000001

Do you want to exit the Circus? The Harsh Truth
https://www.youtube.com/watch?v=ZJwQicZHp_c

User avatar
MochiMoppel
Posts: 1128
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 18 times
Been thanked: 366 times

Re: YAD - tips'n'tricks

Post by MochiMoppel »

In this thread I'm playing around with "simple" colors. I'm using gtkdialog, but I figured that yad may also be a suitable GUI.

Experimenting with my version 0.36.2 it appears that only the yad List option is suitable. Has the advantage that the color value of a selected list item can be sent to stdout (in gtkdialog there is no pango capable list/tree widget)

Speed *seems* to be faster than gtkdialog, but that's only because yad displays the first lines while it is still busy to render the rest of the list. Gtkdialog would wait until it has rendered the whole list and only then display the result. In the end they are both slow.

What I find annoying is that yad sends the pango markup to stdout instead of the resulting text string. For following code the output would be something like <big><tt>#00c</tt></big> instead of #00c . Would require another pipe to sed to clean it up. Maybe this is fixed in newer versions? In comparison gtkdialog would output only the unformatted result but not the markup.

Code: Select all

#!/bin/bash
P='0 3 6 9 c f'
for a in $P ;do for b in $P;do for c in $P;do
	COLORS+="<span font='24' bgcolor='#$a$b$c'>      <span font='12' fgcolor='#000'>text</span><span font='12' fgcolor='#fff'>text</span>      </span>\n<big><tt>#$a$b$c</tt></big>\n"
done;done;done
printf "$COLORS" | yad --list --separator= --column='215 "Web Safe" Colors' --column=Values --print-column=2 --geometry=300x600
simple_colors_using_yad.png
simple_colors_using_yad.png (24.28 KiB) Viewed 2396 times
User avatar
fredx181
Posts: 2626
Joined: Tue Dec 03, 2019 1:49 pm
Location: holland
Has thanked: 291 times
Been thanked: 1028 times
Contact:

Re: YAD - tips'n'tricks

Post by fredx181 »

MochiMoppel wrote:

In this thread I'm playing around with "simple" colors. I'm using gtkdialog, but I figured that yad may also be a suitable GUI.
.....

Nice, thanks!

What I find annoying is that yad sends the pango markup to stdout instead of the resulting text string.

Workaround (hide (and print) dummy column without the pango markup) I found for that (EDIT: I'm using yad 0.40) :

Code: Select all

P='0 3 6 9 c f'
for a in $P ;do for b in $P;do for c in $P;do
	COLORS+="<span font='24' bgcolor='#$a$b$c'>      <span font='12' fgcolor='#000'>text</span><span font='12' fgcolor='#fff'>text</span>      </span>\n#$a$b$c\n<big><tt>#$a$b$c</tt></big>\n"
done;done;done
printf "$COLORS" | yad --list --separator= --column='215 "Web Safe" Colors' --column=dummy --column=Values --hide-column=2 --print-column=2 --geometry=300x600

EDIT:

.... yad displays the first lines while it is still busy to render the rest of the list

Here's another variation that will bring up the list directly :

Code: Select all

P='0 3 6 9 c f'
for a in $P ;do for b in $P;do for c in $P;do
	COLORS+="<span font='24' bgcolor='#$a$b$c'>      <span font='12' fgcolor='#000'>text</span><span font='12' fgcolor='#fff'>text</span>      </span>\n#$a$b$c\n<big><tt>#$a$b$c</tt></big>\n"
done;done;done
COLORS=$(printf "$COLORS")
OLDIFS="$IFS"
IFS=$'\n'
yad --list --separator= --column='215 "Web Safe" Colors' --column=dummy --column=Values $COLORS --hide-column=2 --print-column=2 --geometry=300x600
IFS="$OLDIFS"
User avatar
misko_2083
Posts: 196
Joined: Wed Dec 09, 2020 11:59 pm
Has thanked: 10 times
Been thanked: 20 times

Re: YAD - tips'n'tricks

Post by misko_2083 »

^There is already a rumour in the forum circles you are making a paned dialog for this.
When can we expect the official release?

Do you want to exit the Circus? The Harsh Truth
https://www.youtube.com/watch?v=ZJwQicZHp_c

User avatar
MochiMoppel
Posts: 1128
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 18 times
Been thanked: 366 times

Re: YAD - tips'n'tricks

Post by MochiMoppel »

@fredx181 Great :thumbup2:
The new version is really fast. Makes it possible to render the 512 colors pastell pattern in reasonable time.

I tried to make it even faster by skipping the printf subshell and making it POSIX compliant, so it will run in busybox ash:

Code: Select all

#!/bin/ash
P='8 9 a b c d e f'
for a in $P ;do for b in $P;do for c in $P;do
	COLORS=$COLORS"#$a$b$c|<span font='24' bgcolor='#$a$b$c'>      <span font='12' fgcolor='#000'>text</span><span font='12' fgcolor='#fff'>text</span>      </span>|<big><tt>#$a$b$c</tt></big>|"
done;done;done
IFS=\|
yad --list --separator= --no-rules-hint --column=dummy --column='512 Pastell Colors' --column=Values $COLORS --hide-column=1 --print-column=1 --geometry=300x600 
User avatar
misko_2083
Posts: 196
Joined: Wed Dec 09, 2020 11:59 pm
Has thanked: 10 times
Been thanked: 20 times

Re: YAD - tips'n'tricks

Post by misko_2083 »

MochiMoppel wrote: Sat Jul 16, 2022 4:30 am

@fredx181 Great :thumbup2:
The new version is really fast. Makes it possible to render the 512 colors pastell pattern in reasonable time.

I tried to make it even faster by skipping the printf subshell and making it POSIX compliant, so it will run in busybox ash:

Code: Select all

#!/bin/ash
P='8 9 a b c d e f'
for a in $P ;do for b in $P;do for c in $P;do
	COLORS=$COLORS"#$a$b$c|<span font='24' bgcolor='#$a$b$c'>      <span font='12' fgcolor='#000'>text</span><span font='12' fgcolor='#fff'>text</span>      </span>|<big><tt>#$a$b$c</tt></big>|"
done;done;done
IFS=\|
yad --list --separator= --no-rules-hint --column=dummy --column='512 Pastell Colors' --column=Values $COLORS --hide-column=1 --print-column=1 --geometry=300x600 

It can select multiple columns as well:

Code: Select all

P="8 9 a b c d e f"
for a in $P ;do for b in $P;do for c in $P;do
        COLORS=$COLORS"#$a$b$c|<span font='24' bgcolor='#$a$b$c'>      <span font='12' fgcolor='#000'>text</span><span font='12' fgcolor='#fff'>text</span>      </span>|FALSE|<big><tt>#$a$b$c</tt></big>|"
done;done;done
IFS=\|
yad --list --separator= --no-rules-hint --column=dummy --column='512 Pastell Colors' --column=Pick:CHK --column=Values $COLORS --hide-column=1 --print-all --geometry=320x600 | awk '/TRUE/{print substr($1,1,4)}'

Mochi I gave my best but couldn't trick Fred to do something like next :D

Code: Select all

#!/bin/ash
OIFS=$IFS
key=`awk 'BEGIN { srand(); printf("%d%d\n", 256 * rand(), 256 * rand())}'`

P="8 9 a b c d e f"
for a in $P ;do for b in $P;do for c in $P;do
        COLORS=$COLORS"#$a$b$c|<span font='24' bgcolor='#$a$b$c'>      <span font='12' fgcolor='#000'>text</span><span font='12' fgcolor='#fff'>text</span>      </span>|FALSE|<big><tt>#$a$b$c</tt></big>|"
done;done;done

IFS=\|
yad --plug=$key --tabnum=1 --list --separator= --no-rules-hint --column=dummy --column='512 Pastell Colors' --column=Pick:CHK --column=Values $COLORS --hide-column=1 --print-all | awk '/TRUE/{print "Pastel", substr($1,1,4)}' &
IFS=$OIFS

COLORS=

P="0 3 6 9 c f"
for a in $P ;do for b in $P;do for c in $P;do
        COLORS=$COLORS"#$a$b$c|<span font='24' bgcolor='#$a$b$c'>      <span font='12' fgcolor='#000'>text</span><span font='12' fgcolor='#fff'>text</span>      </span>|FALSE|<big><tt>#$a$b$c</tt></big>|"
done;done;done

IFS=\|
yad --plug=$key --tabnum=2 --list --separator= --no-rules-hint --column=dummy --column='215 "Web Safe" Colors' --column=Pick:CHK --column=Values $COLORS --hide-column=1 --print-all | awk '/TRUE/{print "Web", substr($1,1,4)}' &
IFS=$OIFS

yad --notebook --key=$key --tab="Pastell" --tab="Web Safe" --geometry=375x600

Do you want to exit the Circus? The Harsh Truth
https://www.youtube.com/watch?v=ZJwQicZHp_c

User avatar
MochiMoppel
Posts: 1128
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 18 times
Been thanked: 366 times

Re: YAD - tips'n'tricks

Post by MochiMoppel »

misko_2083 wrote: Sun Jul 17, 2022 5:22 am

It can select multiple columns as well:

Code: Select all

P="8 9 a b c d e f"
for a in $P ;do for b in $P;do for c in $P;do
        COLORS=$COLORS"#$a$b$c|<span font='24' bgcolor='#$a$b$c'>      <span font='12' fgcolor='#000'>text</span><span font='12' fgcolor='#fff'>text</span>      </span>|FALSE|<big><tt>#$a$b$c</tt></big>|"
done;done;done
IFS=\|
yad --list --separator= --no-rules-hint --column=dummy --column='512 Pastell Colors' --column=Pick:CHK --column=Values $COLORS --hide-column=1 --print-all --geometry=320x600 | awk '/TRUE/{print substr($1,1,4)}'

Less "awk"ward ;) :

Code: Select all

#!/bin/ash
P='8 9 a b c d e f'
for a in $P ;do for b in $P;do for c in $P;do
	COLORS=$COLORS"|#$a$b$c|<span font='24' bgcolor='#$a$b$c'>      <span font='12' fgcolor='#000'>text</span><span font='12' fgcolor='#fff'>text</span>      </span>|<big><tt>#$a$b$c</tt></big>|"
done;done;done
IFS=\|
yad --list --checklist --separator= --no-rules-hint --column= --column=dummy --column='512 Pastell Colors' --column=Values $COLORS --hide-column=2 --print-column=2 --geometry=330x600 
Post Reply

Return to “Programming”