Skip to content

Preparing yadL scripts for yadU

step- edited this page Feb 9, 2023 · 1 revision

Preparing yadL scripts for yadU

yadL scripts refers to shell scripts that run yadL. Preparing means writing such scripts with an eye to a future requirement to be able to run the script also with yadU. There are some differences between the two - details - and it is a good idea to plan ahead, even if today you think you will not need to run your script with yadU. Of course, preparing in such a way involves careful coding and consideration for differences, ultimately more work. Hopefully, reading this page will make your coding easier and faster.

The basics

Determining yad version with function required_yad

Your script should known upfront if it is running with yadL, yadU, GTK+-2 or GTK+-3, and what yad capabilities are available. The require_yad POSIX shell function asserts a minimum yad version, and sets some global variables to give you that information. You can then use the global variables in conditional statements.

Usage

# setting YAD is optional - if unset it defaults to "yad"
# in POSIX shell do it this way:
export YAD="/path/to/yad"
require_yad 0 42 43
# instead in bash and zsh you can do it this way:
YAD="/path/to/yad" require_yad 0 42 43

# either way, after calling require_yad the following variables will be set:
echo "\$?=$?"
echo "YAD_VERCAP=$YAD_VER_CAP"
echo "YAD_STOCK_BTN=$YAD_STOCK_BTN"

Example

if ! require_yad 0 42 43; then
	echo >&2 "yad not found or yad build too old"
else
	case "$YAD_VER_CAP" in
		'0.'*'gtk2'* ) echo "running a yadL GTK+-2 build!" ;;
		'0.'*'gtk3'* ) echo "running a yadL GTK+-3 build!" ;;
		'0.'*        ) echo "running a yadL build!" ;;
		*            ) echo "running a yadU build!" ;;
		esac
fi

Function

require_yad () { # $1-x $2-y $3-z {{{1 => $YAD_VER_CAP = 'x y z'':gtk'('2'|'3')':'() / $YAD_STOCK_BTN = ('gtk'|'yad')
	local x="${1:?}" y="${2:?}" z="${3:?}" IFS info
	unset YAD_VER_CAP YAD_STOCK_BTN
	info="$(${YAD:-yad} --version)"
	[ -n "$info" ] || return 1

	# yad --version sample output (one line):
	# line 1: "0.42.43 (GTK+ 3.24.31)"
	set -- $info
	IFS="."; set -- $1 # version x.y.z
	YAD_VER_CAP="$1 $2 ${3:-0}"

	case "$info" in
		*'GTK+ 2'* ) YAD_VER_CAP="$YAD_VER_CAP:gtk2" ;;
		*'GTK+ 3'* ) YAD_VER_CAP="$YAD_VER_CAP:gtk3" ;;
	esac

	### frequently-used :capa:bili:ties that can be tricky to assess
	[ \( $1 -eq 0 -a $2 -eq 42 -a ${3:-0} -ge 16 \) -o \( $1 -eq 0 -a $2 -gt 42 \) -o $1 -gt 0 ] &&
		YAD_VER_CAP="$YAD_VER_CAP:text-lang"
	[ \( $1 -eq 0 -a $2 -eq 42 -a ${3:-0} -ge 25 \) -o \( $1 -eq 0 -a $2 -gt 42 \) -o $1 -gt 0 ] &&
		YAD_VER_CAP="$YAD_VER_CAP:selectable-labels" # is present but broken in earlier versions

	# for version 0.x it's --button=gtk-ok otherwise it's --button=yad-ok
	[ "$1" = '0' ] && YAD_STOCK_BTN='gtk' || YAD_STOCK_BTN='yad'

	[ \( $1 -eq $x -a $2 -eq $y -a ${3:-0} -ge $z \) -o \( $1 -eq $x -a $2 -gt $y \) -o $1 -gt $x ]
}

Stock icons and yad buttons

You need to know that while yadL still supports GTK+ stock button names (in GTK+-2 and GTK+-3 builds), yadU does not because GTK+-3 deprecated stock icons in favor of themed icons. So Yadu replaced the stock button prefix "gtk-" with its own "yad-" prefix.

Function require_yad sets variable YAD_STOCK_BTN with the correct prefix for the yad binary at hand.

Example

export YAD='yad'
if ! required_yad 0 42 43 # or whatever minimum version you need
then
	unset YAD
	# handle error ...
fi

$YAD --center --button="$YAD_STOCK_BTN-ok"

Width of long label exceeds screen width

You need to read this open yadU issue and decide for yourself how it can affect your script and what to do. Make no mistake, this is not a yadL vs yadU issue, it is a GTK+-2 vs. GTK+-3 issue that frequently occurs when you move your scripts to GTK+-3 builds.