Skip to content

Instantly share code, notes, and snippets.

@mattmc3
Last active December 22, 2024 16:50

Revisions

  1. mattmc3 revised this gist Feb 8, 2023. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -31,7 +31,7 @@ zstyle -T ':example:baz' doit && echo "success" || echo "fail"

    So the short version is...

    If you want to default to 'no', and have to explicitly say 'yes', then this is the zstyle test:
    If you want to default to 'no', and have to explicitly say 'yes', then test with `zstyle -t`:

    ```zsh
    if zstyle -t ':example:foo' doit; then
    @@ -45,7 +45,7 @@ if zstyle -t ':example:baz' doit; then
    fi
    ```

    If you want to default to 'yes', and have to explicitly say 'no', then this is the zstyle test:
    If you want to default to 'yes', and have to explicitly say 'no', then test with `zstyle -T`:

    ```zsh
    if zstyle -T ':example:foo' doit; then
  2. mattmc3 revised this gist Feb 8, 2023. 1 changed file with 60 additions and 0 deletions.
    60 changes: 60 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    # zstyle booleans

    Setup foo,bar,baz boolean vars

    ```zsh
    zstyle ':example:foo' doit yes
    zstyle ':example:bar' doit no
    # leave baz unset
    ```

    `zstyle -t` succeeds if the style is defined and is 'true', 'yes', 'on', or '1'
    `zstyle -T` succeeds if the style is undefined, or is 'true', 'yes', 'on', or '1'

    `foo` and `bar` aren't very interesting because we explicitly set them.

    ```zsh
    # yes = success
    zstyle -t ':example:foo' doit && echo "success" || echo "fail"
    zstyle -T ':example:foo' doit && echo "success" || echo "fail"

    # no = fail
    zstyle -t ':example:bar' doit && echo "success" || echo "fail"
    zstyle -T ':example:bar' doit && echo "success" || echo "fail"

    # undefined = fail
    zstyle -t ':example:baz' doit && echo "success" || echo "fail"

    # undefined = success
    zstyle -T ':example:baz' doit && echo "success" || echo "fail"
    ```

    So the short version is...

    If you want to default to 'no', and have to explicitly say 'yes', then this is the zstyle test:

    ```zsh
    if zstyle -t ':example:foo' doit; then
    echo "Make it so..."
    fi
    if zstyle -t ':example:bar' doit; then
    echo "Never gonna happen"
    fi
    if zstyle -t ':example:baz' doit; then
    echo "Never gonna happen"
    fi
    ```

    If you want to default to 'yes', and have to explicitly say 'no', then this is the zstyle test:

    ```zsh
    if zstyle -T ':example:foo' doit; then
    echo "Make it so..."
    fi
    if zstyle -T ':example:bar' doit; then
    echo "Never gonna happen"
    fi
    if zstyle -T ':example:baz' doit; then
    echo "Make it so..."
    fi
    ```
  3. mattmc3 revised this gist Mar 4, 2022. 1 changed file with 37 additions and 0 deletions.
    37 changes: 37 additions & 0 deletions zstyle-bool.zsh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    # zstyle-bool

    echo "For values that can be missing or default to true, use -T"
    function test_bool_zstyle_default_true {
    if zstyle -T ':foo:bar' value; then
    echo "bar is true or unset"
    else
    echo "bar is false"
    fi
    }
    test_bool_zstyle_default_true
    zstyle ':foo:bar' value yes
    test_bool_zstyle_default_true
    zstyle ':foo:bar' value no
    test_bool_zstyle_default_true
    zstyle ':foo:bar' value 1
    test_bool_zstyle_default_true
    zstyle ':foo:bar' value ''
    test_bool_zstyle_default_true

    echo "For values that can be missing or default to false, use ! -t"
    function test_bool_zstyle_default_false {
    if ! zstyle -t ':foo:baz' value; then
    echo "baz is false or unset"
    else
    echo "baz is true"
    fi
    }
    test_bool_zstyle_default_false
    zstyle ':foo:baz' value yes
    test_bool_zstyle_default_false
    zstyle ':foo:baz' value no
    test_bool_zstyle_default_false
    zstyle ':foo:baz' value 1
    test_bool_zstyle_default_false
    zstyle ':foo:baz' value ''
    test_bool_zstyle_default_false
  4. mattmc3 revised this gist Sep 30, 2020. 1 changed file with 9 additions and 9 deletions.
    18 changes: 9 additions & 9 deletions zstyle_examples.zsh
    Original file line number Diff line number Diff line change
    @@ -4,20 +4,20 @@
    # list all zstyle settings
    zstyle -L

    # set a string value
    # store value in zstyle
    zstyle :example:favorites fruit apple

    # set an explicit string value
    zstyle -s ':example:favorites' 'computer' 'apple'
    # store multiple values in zstyle
    zstyle :example:list fruits banana mango pear

    # assign new $fav variable with -g
    # retrieve from zstyle and assign new $fav variable with -g
    zstyle -g fav ':example:favorites' fruit && echo $fav

    # be explicit about the assignment data type:
    # retrieve from zstyle and be explicit about the assignment data type:
    # -a: array, -b: boolean, -s: string
    zstyle -b ':example:favorites:vegtable' 'broccoli' no
    zstyle -a :example:list fruits myfruitlist && echo $myfruitlist

    # test with -t
    # test that a zstyle value exists with -t
    if zstyle -t ':example:favorites' 'fruit' 'apple'; then
    echo "an apple a day keeps the dr. away"
    fi
    @@ -26,7 +26,7 @@ if ! zstyle -t ':example:favorites:vegtable' 'broccoli' 'no'; then
    fi

    # delete a value with -d
    zstyle -d ':example:favorites' 'computer'
    zstyle -d ':example:favorites' 'fruit'

    # list only zstyle settings for a certain pattern
    zstyle -L ':example:favorites*'
    zstyle -L ':example:favorites*'
  5. mattmc3 revised this gist Oct 15, 2019. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions zstyle_examples.zsh
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,14 @@
    # reference: http://zsh.sourceforge.net/Doc/Release/Zsh-Modules.html#The-zsh_002fzutil-Module
    # https://unix.stackexchange.com/questions/214657/what-does-zstyle-do

    # list all zstyle settings
    zstyle -L

    # set a string value
    zstyle :example:favorites fruit apple

    # set a value with strings
    zstyle ':example:favorites' 'computer' 'apple'
    # set an explicit string value
    zstyle -s ':example:favorites' 'computer' 'apple'

    # assign new $fav variable with -g
    zstyle -g fav ':example:favorites' fruit && echo $fav
    @@ -28,4 +29,4 @@ fi
    zstyle -d ':example:favorites' 'computer'

    # list only zstyle settings for a certain pattern
    zstyle -L ':example:favorites*'
    zstyle -L ':example:favorites*'
  6. mattmc3 created this gist Oct 15, 2019.
    31 changes: 31 additions & 0 deletions zstyle_examples.zsh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    # reference: http://zsh.sourceforge.net/Doc/Release/Zsh-Modules.html#The-zsh_002fzutil-Module

    # list all zstyle settings
    zstyle -L

    # set a string value
    zstyle :example:favorites fruit apple

    # set a value with strings
    zstyle ':example:favorites' 'computer' 'apple'

    # assign new $fav variable with -g
    zstyle -g fav ':example:favorites' fruit && echo $fav

    # be explicit about the assignment data type:
    # -a: array, -b: boolean, -s: string
    zstyle -b ':example:favorites:vegtable' 'broccoli' no

    # test with -t
    if zstyle -t ':example:favorites' 'fruit' 'apple'; then
    echo "an apple a day keeps the dr. away"
    fi
    if ! zstyle -t ':example:favorites:vegtable' 'broccoli' 'no'; then
    echo "Broccoli is the deadliest plant on Earth - why, it tries to warn you itself with its terrible taste"
    fi

    # delete a value with -d
    zstyle -d ':example:favorites' 'computer'

    # list only zstyle settings for a certain pattern
    zstyle -L ':example:favorites*'