You are not logged in.

#26 2016-01-21 03:55:10

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 7,100
Website

Re: Yad notes

Thank you xaos52 for your input which is, as always, much appreciated.
You caught the missing -r from read that I discovered this morning when trying to put linebreaks in text with \n, and caught some more things I hadn't noticed:
*) that argsetter doesn't need to be declared outside the helper function
*) using "${FUNCNAME[1]}" to check the helper isn't being misused
*) nice idea to add the return value to make each --button unique, and BASH_REMATCH to pick out the parts of a string

But there were a couple of bugs:
*) line 55
[[ -z "$value" ]] && continue
this causes all options without values to be lost! There are many, and also the functions are intended to accept a plain string of text as default value for --text=.
*) yad_question's default buttons are lost. In my version they were added to the arguments for the helper function:

yad_question(){
    local yadargs=()
    _yad_setargs '--button=Yes:0' '--button=No:1' "$@"
    yad  "${yadargs[@]}"
}

*) The buttons' return values are not being attached to the output. When processing the arguments of  _yad_setargs, only the button text is put into $value, not the return value ("ID"). ie line 59
value="${BASH_REMATCH[1]}"
needs to be
value="${BASH_REMATCH[1]}:${BASH_REMATCH[2]}"
...but no! That just puts value where it was originally, so the line isn't needed at all in fact.

I also have a couple of further suggestions:
*) It seems to me there is no need to go through two do loops when processing the default argument arrays and then when processing "$@" - they can all be put in the same do loop and processed the same way:

for i in "${yad_common_args[@]}" "${yad_popup_args[@]}" "$@"
do
    IFS='=' read -r key value <<<"$i"
###etc

*) I don't think the hasElement() function is necessary. To test if a certain key exists in argsetter this is enough:
[[ ${argsetter["$key"]+X} ]] && ...
but anyway, is there any harm in unsetting an array element which doesn't exist? No error messages appear and the command returns 0.
*) I think this
[[ "$arg" =~ ^(--button)([0-9]+)$ ]] && arg="${BASH_REMATCH[1]}"
could be simplified to this
[[ "$arg" = --button* ]] && arg="--button"
*) Is there any reason to use this
if [[ ! -z "${argsetter[$i]}" ]]

instead of this?
if [[ "${argsetter[$i]}" ]]

Anyway, combining our ideas I propose this:

yad_common_args=('--window-icon=distributor-logo-bunsenlabs' '--center' '--borders=20')
yad_popup_args=('--undecorated' '--fixed' '--on-top')

yad_question(){
    local yadargs=()
    _yad_setargs '--button=Yes:0' '--button=No:1' "$@"
    yad "${yadargs[@]}"
}
yad_info(){
    local yadargs=()
    _yad_setargs '--button=OK:0' "$@"
    yad "${yadargs[@]}"
}
yad_error(){
    local yadargs=()
    _yad_setargs '--button=OK:0' '--image=dialog-error' "$@"
    yad "${yadargs[@]}"
}

_yad_setargs(){
    # Catch direct calls
    case "${FUNCNAME[1]}" in
        yad_question|yad_info|yad_error)
            :;;
        *)
            echo "$0:${FUNCNAME[0]}: this function is only to be called by yad_question, yad_info or yad_error."
            return 1
            ;;
    esac
    local i key value
    local button_with_id_pattern='^(.*):([0-9]+)$'
    declare -A argsetter
    local argsetter=()
    for i in "${yad_common_args[@]}" "${yad_popup_args[@]}" "$@"
    do
        IFS='=' read -r key value <<<"$i"
        [[ "$key" = --button ]] &&
            [[ "$value" =~ $button_with_id_pattern ]] &&
            key=--button"${BASH_REMATCH[2]}"
        [[ $value = 0 ]] && {
            [[ ${argsetter["$key"]+X} ]] && unset "argsetter[$key]"
            continue
        }
        [[ $key = --* ]] || {
            argsetter[--text]="$key"
            continue
        }
        argsetter[$key]="$value"
    done
    i=
    for i in "${!argsetter[@]}"
    do
        local arg="$i"
        [[ "$arg" = --button* ]] && arg="--button"
        if [[ ${argsetter[$i]} ]]
        then
            yadargs+=("$arg=${argsetter[$i]}")
        else
            yadargs+=("$arg")
        fi
    done
}

The only remaining issue, I think, is that because the order of elements in an associative array is undefined there is no way to put the buttons with OK first, for example. But, I'm thinking developers who want to add extra buttons to the basic pop-up dialogues will often be writing their own yad commands anyway - possibly using the default arrays yad_common_args yad_popup_args.


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )

Introduction to the Bunsenlabs Lithium Desktop

Offline

#27 2016-01-21 09:38:30

xaos52
The Good Doctor
From: Planet of the @pes
Registered: 2015-09-30
Posts: 695

Re: Yad notes

Hi John,
You are right on all accounts. My apologies for the hasty work.

*) I don't think the hasElement() function is necessary. To test if a certain key exists in argsetter this is enough:
[[ ${argsetter["$key"]+X} ]] && ...
but anyway, is there any harm in unsetting an array element which doesn't exist? No error messages appear and the command returns 0.

OK.
No harm at all, except me being anxious to **not** remove anything that is **not there**

*) I think this
[[ "$arg" =~ ^(--button)([0-9]+)$ ]] && arg="${BASH_REMATCH[1]}"
could be simplified to this
[[ "$arg" = --button* ]] && arg="--button"

I try to avoid using the same constant text more than once. It is a reflex.
Because, when the text changes, you have to remember to change it in all places.
I have been bitten by this more than once, especially when I was in a rush.

*) Is there any reason to use this
if [[ ! -z "${argsetter[$i]}" ]]
instead of this?
if [[ "${argsetter[$i]}" ]]

None whatsoever. I normally use the latter as well.

Now I was thinking there might be an even easier way to

1. Control the order of the buttons
2. Reset any button that was previously set
   by letting the caller specify the order in which he/she wants the buttons
   using --button0, --button1, etc
   All we have to do then is strip off that sequence number in the last phase.
   And it allows us to reset all buuttons, including ones that have a command
   in stead of an ID.

Here is the new proposal, based on your last one. Note the sort of the keys of argsetter.
Buttons do not need special treatment any more.
Resetting button1 can now be done by appending argument --button1=0

yad_common_args=('--window-icon=distributor-logo-bunsenlabs' '--center' '--borders=20')
yad_popup_args=('--undecorated' '--fixed' '--on-top')

yad_question(){
    local yadargs=()
    _yad_setargs '--button0=Yes:0' '--button1=No:1' "$@"
    yad "${yadargs[@]}"
}
yad_info(){
    local yadargs=()
    _yad_setargs '--button0=OK:0' "$@"
    yad "${yadargs[@]}"
}
yad_error(){
    local yadargs=()
    _yad_setargs '--button0=OK:0' '--image=dialog-error' "$@"
    yad "${yadargs[@]}"
}

_yad_setargs(){
    # Catch direct calls
    case "${FUNCNAME[1]}" in
        yad_question|yad_info|yad_error)
            :;;
        *)
            echo "$0:${FUNCNAME[0]}: this function is only to be called by yad_question, yad_info or yad_error."
            return 1
            ;;
    esac
    local i key value
    declare -A argsetter
    local argsetter=()
    for i in "${yad_common_args[@]}" "${yad_popup_args[@]}" "$@"
    do
        IFS='=' read -r key value <<<"$i"
        [[ $value = 0 ]] && {
            [[ ${argsetter["$key"]+X} ]] && unset "argsetter[$key]"
            continue
        }
        [[ $key = --* ]] || {
            argsetter[--text]="$key"
            continue
        }
        argsetter[$key]="$value"
    done
    i=key=

    # Sort argsetter on keys to respect the button order
    while
        read -r i
    do
        local arg="$i"
        [[ "$arg" = --button* ]] && arg="--button"
        if [[ ${argsetter[$i]} ]]
        then
            yadargs+=("$arg=${argsetter[$i]}")
        else
            yadargs+=("$arg")
        fi
    done < <(for key in "${!argsetter[@]}"; do printf '%s\n' "$key"; done | sort )
}

xaos52

Offline

#28 2016-01-22 04:39:17

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 7,100
Website

Re: Yad notes

xaos52 wrote:

Now I was thinking there might be an even easier way to

1. Control the order of the buttons
2. Reset any button that was previously set
   by letting the caller specify the order in which he/she wants the buttons
   using --button0, --button1, etc
   All we have to do then is strip off that sequence number in the last phase.
   And it allows us to reset all buuttons, including ones that have a command
   in stead of an ID.

Here is the new proposal, based on your last one. Note the sort of the keys of argsetter.
Buttons do not need special treatment any more.
Resetting button1 can now be done by appending argument --button1=0

Very neat! I was thinking of adding yet another array just to hold the buttons, but this is simpler and nicer.

OK I'm just going to try if we can add the numbers automatically, in the order they're specified. It would let users specify buttons the same way they do directly with yad, without having to learn a new syntax. It might be easy, but it might not...


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )

Introduction to the Bunsenlabs Lithium Desktop

Offline

#29 2016-01-28 07:58:31

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 7,100
Website

Re: Yad notes

Well, other things intervened for a while, but I was thinking a bit more about these pesky buttons, wondering what behaviour most devs would want from these functions. I eventually thought the most important aspect of a button was its ID. If a dev wants a button to return 4, say, then it's very unlikely they'll want another button also returning 4. So, buttons with the same ID are overwritten.

Then, ordering is by the order they are specified, first in the default arrays, then the predefined functions, finally in the arguments passed to the functions. If a button has a new ID it will be added to the end of the command line (and so come on the right), and if a new button has the same ID as an already defined button it will overwrite it (and stay in its original position). Commands work as well as numbers for IDs.

The button keys (--buttonID) are put into a numerically indexed array button_ids, then read out - in order - to fetch the corresponding data from argsetter. In fact the extra code is only a couple of lines. Arguments are passed in the same way as to yad itself, except for the --option=0 syntax. It seems to be working OK:

yad_common_args=('--window-icon=distributor-logo-bunsenlabs' '--center' '--borders=20')
yad_popup_args=('--undecorated' '--fixed' '--on-top')

yad_question(){
    local yadargs=()
    _yad_setargs '--button=Yes:0' '--button=No:1' "$@"
    yad "${yadargs[@]}"
}
yad_info(){
    local yadargs=()
    _yad_setargs '--button=OK:0' "$@"
    yad "${yadargs[@]}"
}
yad_error(){
    local yadargs=()
    _yad_setargs '--button=OK:0' '--image=dialog-error' "$@"
    yad "${yadargs[@]}"
}

_yad_setargs(){
    # Catch direct calls
    case "${FUNCNAME[1]}" in
        yad_question|yad_info|yad_error)
            :;;
        *)
            echo "$0:${FUNCNAME[0]}: this function is only to be called by yad_question, yad_info or yad_error."
            return 1
            ;;
    esac
    local i key value
    local button_with_id_pattern='^(.*):(.+)$'
    declare -A argsetter
    local argsetter=()
    local button_ids=()
    for i in "${yad_common_args[@]}" "${yad_popup_args[@]}" "$@"
    do
        IFS='=' read -r key value <<<"$i"
        [[ "$key" = --button && "$value" =~ $button_with_id_pattern ]] && {
            key=--button"${BASH_REMATCH[2]}"
            button_ids+=("$key")
        }
        [[ $value = 0 ]] && {
            [[ ${argsetter["$key"]+X} ]] && unset "argsetter[$key]"
            continue
        }
        [[ $key = --* ]] || {
            argsetter[--text]="$key"
            continue
        }
        argsetter[$key]="$value"
    done
    i=
    for i in "${!argsetter[@]}"
    do
        [[ "$i" = --button* ]] && continue
        if [[ ${argsetter[$i]} ]]
        then
            yadargs+=("$i=${argsetter[$i]}")
        else
            yadargs+=("$i")
        fi
    done
    i=
    for i in "${button_ids[@]}"
    do
        [[ ${argsetter[$i]} ]] && yadargs+=("--button=${argsetter[$i]}")
        unset "argsetter[$i]"
    done
}

Last edited by johnraff (2016-01-28 08:00:14)


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )

Introduction to the Bunsenlabs Lithium Desktop

Offline

#30 2016-01-28 14:14:17

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

Re: Yad notes

Does this cope with a button ID which is a command, not a return code?

man yad wrote:

If ID have a non-numeric value it treats like a command and click on such button doesn't close the dialog.


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

Online

#31 2016-01-28 20:04:18

xaos52
The Good Doctor
From: Planet of the @pes
Registered: 2015-09-30
Posts: 695

Re: Yad notes

xaos52


Table of Contents
─────────────────

1 yad-scripts
.. 1.1 script
.. 1.2 script to test the script
.. 1.3 test results
..... 1.3.1 test #1
..... 1.3.2 test #2
..... 1.3.3 test #3
.. 1.4 Conclusion
.. 1.5 Style
.. 1.6 New proposal


1 yad-scripts
═════════════

1.1 script
──────────

  Good idea to use a separate array for the buttons, to retain the
  order.


1.2 script to test the script
─────────────────────────────

#!/bin/bash
source ./t37
# test #1 - adding 2 buttons with id + extra args + text
yad_question --button=OK:2 --button=NO:3 --arg1=argument1 --arg2=argument2  text1 text2 text3 
# test #2 - adding button with text
yad_question --button=command-label:command
# test #3 - reset button with id
yad_question --button=OK:2 --button=NO:3 --arg1=argument1 --arg2=argument2  text1 text2 text3 --button=0:2 
# test #4 - reset button with id that was not set previously
yad_question --button=OK:2 --button=NO:3 --arg1=argument1 --arg2=argument2  text1 text2 text3 --button=0:6 

1.3 test results
────────────────

1.3.1 test #1
╌╌╌╌╌╌╌╌╌╌╌╌╌

  OK


1.3.2 test #2
╌╌╌╌╌╌╌╌╌╌╌╌╌

  buttons with commands: –button=command-label:command
     button-ids+=(–buttoncommand)
     argsetter[–buttoncommand]=command-label:command
     yad-arguments+=–button=comman-label:command <– correct

  but how do you reset it? perhaps it is not necessary to be able to
  reset it, because this kind of button will always be under developer
  control. (I don't see us setting a button with command as default. )


1.3.3 test #3
╌╌╌╌╌╌╌╌╌╌╌╌╌

  Resetting a button with ID does not work. The script keeps the button
  with label '0' Reason: value ="0:2" is compared against "0"


1.4 Conclusion
──────────────

  In any case we must be able to distinguish a button with ID and a
  button with command. When is it a buttun with id?
  • matches button_with_id_pattern
  • the 2nd part of the match is a number ( a number on itself is not a
    valid command)


1.5 Style
─────────


1.6 New proposal
────────────────

  Provided we agree that resetting a button with a command is _not
  provided_ by the interface, the following proposal runs all tests OK.

  We should then document the interface to avoid ambiguity.

yad_common_args=('--window-icon=distributor-logo-bunsenlabs' '--center' '--borders=20')
yad_popup_args=('--undecorated' '--fixed' '--on-top')

yad_question(){
    local -a -- yadargs=()
    _yad_setargs '--button=Yes:0' '--button=No:1' "$@"
    yad "${yadargs[@]}"
}
yad_info(){
    local -a -- yadargs=()
    _yad_setargs '--button=OK:0' "$@"
    yad "${yadargs[@]}"
}
yad_error(){
    local -a -- yadargs=()
    _yad_setargs '--button=OK:0' '--image=dialog-error' "$@"
    yad "${yadargs[@]}"
}

_yad_setargs(){
    # Catch direct calls
    case "${FUNCNAME[1]}" in
        yad_question|yad_info|yad_error)
            :;;
        *)
            echo "$0:${FUNCNAME[0]}: this function is only to be called by yad_question, yad_info or yad_error."
            return 1
            ;;
    esac
    local -- i key value
    local -- button_with_id_pattern='^(.*):([0-9]+)$'
    local -A -- argsetter=()
    local -a -- button_ids=()
    for i in "${yad_common_args[@]}" "${yad_popup_args[@]}" "$@"
    do
        IFS='=' read -r key value <<<"$i"
        if [[ "$key" = --button ]]
        then
            if [[ "$value" =~ $button_with_id_pattern ]]
            then
                # button with id
                key=--button"${BASH_REMATCH[2]}"
                button_ids+=("$key")
                [[ "${BASH_REMATCH[1]}" = 0 ]] && {
                    [[ ${argsetter["$key"]+X} ]] && unset "argsetter[$key]"
                    continue
                }
            else
                # button with command
                button_ids+=("$key")
            fi
        fi
        [[ $value = 0 ]] && {
            [[ ${argsetter["$key"]+X = X} ]] || unset "argsetter[$key]"
            continue
        }
        [[ $key = --* ]] || {
            argsetter[--text]="$key"
            continue
        }
        argsetter[$key]="$value"
    done

    for i in "${!argsetter[@]}"
    do
        [[ "$i" = --button* ]] && continue
        if [[ ${argsetter[$i]} ]]
        then
            yadargs+=("$i=${argsetter[$i]}")
        else
            yadargs+=("$i")
        fi
    done

    for i in "${button_ids[@]}"
    do
        [[ ${argsetter[$i]} ]] && yadargs+=("--button=${argsetter[$i]}")
        unset "argsetter[$i]"
    done
}

Offline

#32 2016-01-28 20:07:54

xaos52
The Good Doctor
From: Planet of the @pes
Registered: 2015-09-30
Posts: 695

Re: Yad notes

^ Testing org-mode in emacs exported to ascii text.

To make it easier to copy/paste the new proposal, here it is, formatted as code:

yad_common_args=('--window-icon=distributor-logo-bunsenlabs' '--center' '--borders=20')
yad_popup_args=('--undecorated' '--fixed' '--on-top')

yad_question(){
    local -a -- yadargs=()
    _yad_setargs '--button=Yes:0' '--button=No:1' "$@"
    yad "${yadargs[@]}"
}
yad_info(){
    local -a -- yadargs=()
    _yad_setargs '--button=OK:0' "$@"
    yad "${yadargs[@]}"
}
yad_error(){
    local -a -- yadargs=()
    _yad_setargs '--button=OK:0' '--image=dialog-error' "$@"
    yad "${yadargs[@]}"
}

_yad_setargs(){
    # Catch direct calls
    case "${FUNCNAME[1]}" in
        yad_question|yad_info|yad_error)
            :;;
        *)
            echo "$0:${FUNCNAME[0]}: this function is only to be called by yad_question, yad_info or yad_error."
            return 1
            ;;
    esac
    local -- i key value
    local -- button_with_id_pattern='^(.*):([0-9]+)$'
    local -A -- argsetter=()
    local -a -- button_ids=()
    for i in "${yad_common_args[@]}" "${yad_popup_args[@]}" "$@"
    do
        IFS='=' read -r key value <<<"$i"
        if [[ "$key" = --button ]]
        then
            if [[ "$value" =~ $button_with_id_pattern ]]
            then
                # button with id
                key=--button"${BASH_REMATCH[2]}"
                button_ids+=("$key")
                [[ "${BASH_REMATCH[1]}" = 0 ]] && {
                    [[ ${argsetter["$key"]+X} ]] && unset "argsetter[$key]"
                    continue
                }
            else
                # button with command
                button_ids+=("$key")
            fi
        fi
        [[ $value = 0 ]] && {
            [[ ${argsetter["$key"]+X = X} ]] || unset "argsetter[$key]"
            continue
        }
        [[ $key = --* ]] || {
            argsetter[--text]="$key"
            continue
        }
        argsetter[$key]="$value"
    done

    for i in "${!argsetter[@]}"
    do
        [[ "$i" = --button* ]] && continue
        if [[ ${argsetter[$i]} ]]
        then
            yadargs+=("$i=${argsetter[$i]}")
        else
            yadargs+=("$i")
        fi
    done

    for i in "${button_ids[@]}"
    do
        [[ ${argsetter[$i]} ]] && yadargs+=("--button=${argsetter[$i]}")
        unset "argsetter[$i]"
    done
}

Offline

#33 2016-01-29 05:16:33

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 7,100
Website

Re: Yad notes

damo wrote:

Does this cope with a button ID which is a command, not a return code?

IMO yes. You can add a button with a command, or overwrite the label of a pre-existing command's button.

Because my script treats return values and commands the same (ie whatever comes after the colon) you can't change the command of an existing button, but you can change its label. To change a command it would be necessary to have some handle on the button to identify it - either its label, or its position. Label, position and ID/command are the three attributes we have to play with - I just chose the last as being the one developers might be most interested in preserving, and thought it was extremely unlikely anyone would want two buttons with the same return value or command.

It's also unlikely that a default button function would already hold a command - as xaos52 pointed out - but in fact it is possible. In such a case it's possible to change the label of that command's button, by passing

--button='New Label':'old command'

@xaos52 all your tests work for me! That is, in every case the result was what I expected and what I thought developers would probably want.

xaos52 wrote:

Resetting a button with ID does not work.

It works for me, if you mean by "resetting" changing the label of a button with a certain ID. The button's position will be determined by the first time a button with that ID is specified. The button's label will be determined by its last evocation.

So I don't really see any need to distinguish between IDs and commands to be honest. (You do know that with commands, clicking the button doesn't close the window, right?)

BTW I changed button_with_id_pattern to '^(.*):(.+)$' so any string is accepted.

@xaos52 have you tested your proposed code?

        if [[ "$key" = --button ]]
        then
            if [[ "$value" =~ $button_with_id_pattern ]]
            then
                # button with id
                key=--button"${BASH_REMATCH[2]}"
                button_ids+=("$key")
                [[ "${BASH_REMATCH[1]}" = 0 ]] && {
                    [[ ${argsetter["$key"]+X} ]] && unset "argsetter[$key]"
                    continue
                }
            else
                # button with command
                button_ids+=("$key")
            fi
        fi

It seems to me only one button with command will be accepted, as it will be recorded with a $key of '--button'. My code will allow multiple command buttons to be added.


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )

Introduction to the Bunsenlabs Lithium Desktop

Offline

#34 2016-01-29 20:28:22

xaos52
The Good Doctor
From: Planet of the @pes
Registered: 2015-09-30
Posts: 695

Re: Yad notes

1. I understood 'resetting a button' to mean removing a previously set button with the same ID.
    My test #3 for me was meant to remove the button with id 2.

    But I guess it does not make much sense - I don't see anyone wanting to remove any of the 2 default buttons for a yad_question. Being able to change the labels is more than sufficient here.

    Perhaps being able to remove a previously defined button could come in handy when the user is presented with  a choice between many different options, but some should only be visible when certain conditions are met. But even then we could do without that feature.

2. Yes, I did test that part of the code, but only for one 'command button'. Yes, being sloppy again, I know. But it is hard to learn an old ape some new tricks. smile

3.  I suggest replacing

 [[ "$key" = --button && "$value" =~ $button_with_id_pattern ]] &&

with

[[ "$key" == --button ]] && [[  "$value" =~ $button_with_id_pattern ]] && {

In the first form both conditions are tested, even if the first condition evaluates to false. You can verify that by using bash -x.
In the second form only the first condition will be tested if it evaluates to false.

Offline

#35 2016-01-29 20:29:33

xaos52
The Good Doctor
From: Planet of the @pes
Registered: 2015-09-30
Posts: 695

Re: Yad notes

I sent above message twice.
Time  to quit for today.

Offline

#36 2016-01-30 05:51:15

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 7,100
Website

Re: Yad notes

Dr. xaos your input is always most appreciated. My testing with bash -x, however...

xaos52 wrote:

3.  I suggest replacing

 [[ "$key" = --button && "$value" =~ $button_with_id_pattern ]] &&

with

[[ "$key" == --button ]] && [[  "$value" =~ $button_with_id_pattern ]] && {

In the first form both conditions are tested, even if the first condition evaluates to false. You can verify that by using bash -x.
In the second form only the first condition will be tested if it evaluates to false.

In a terminal:

john@raffles4:~$ set -x
++ promptCommand
++ '[' rxvt-unicode = xterm ']'
++ syncHistory
++ builtin history -a
++ HISTFILESIZE=20000
++ builtin history -c
++ builtin history -r
john@raffles4:~$ [[ a = b && 1 = 1 ]] && echo yes
+ [[ a = b ]]
++ promptCommand
++ '[' rxvt-unicode = xterm ']'
++ syncHistory
++ builtin history -a
++ HISTFILESIZE=20000
++ builtin history -c
++ builtin history -r
john@raffles4:~$ [[ a = a && 1 = 2 ]] && echo yes
+ [[ a = a ]]
+ [[ 1 = 2 ]]
++ promptCommand
++ '[' rxvt-unicode = xterm ']'
++ syncHistory
++ builtin history -a
++ HISTFILESIZE=20000
++ builtin history -c
++ builtin history -r

(Please ignore all that extra history stuff. We discussed that long ago on the BBQ you might remember. I left it in because I don't want to edit the evidence!)

The second test is only performed if the first one succeeds, even if both are contained within the same pair of brackets. For some reason, my understanding was that it was more efficient to do multiple tests this way, rather than separately.


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )

Introduction to the Bunsenlabs Lithium Desktop

Offline

#37 2016-01-30 12:09:44

xaos52
The Good Doctor
From: Planet of the @pes
Registered: 2015-09-30
Posts: 695

Re: Yad notes

Dr. xaos your input is always most appreciated. My testing with bash -x, however...

You are being too kind, John.

Oh No, not again. Another slip up. ops

my understanding was that it was more efficient to do multiple tests this way, rather than separately

Is probably true - calling 1 command keyword in stead of 2.
Though the [[  is a builtin is a keyword, so I don't think you will notice the difference.
(Quickly checking now if that is true or not. No, it is not. It is a reserved word!)

There goes my bash guru status. smile

Seriously, I will try to refrain.
I am too impulsive. Posting stupidities as a result.

Offline

#38 2016-01-31 07:35:45

hhh
Meep!
Registered: 2015-09-17
Posts: 10,128
Website

Re: Yad notes

Life is not Kubrick, you don't just pick up a femur and start bashing skulls first try.

https://vimeo.com/152336237

Offline

#39 2016-02-04 05:37:26

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 7,100
Website

Re: Yad notes

Anyway @damo I'll drop that stuff into bl-include.cfg next time the package is due for an upgrade, so you can try using those shortcut functions in some future script if you feel like it.

There's no hurry at all to rewrite scripts that are already working fine, though, unless you want to change something else anyway...


...elevator in the Brain Hotel, broken down but just as well...
( a boring Japan blog (currently paused), idle Twitterings and GitStuff )

Introduction to the Bunsenlabs Lithium Desktop

Offline

Board footer

Powered by FluxBB