Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Contributed function: is-at-least



I've found this function very useful when moving among systems with zsh
versions of varying freshness, and thought it might do some good for
others too:

# $Id: is-at-least,v 1.4 2000/02/10 02:06:17 johan Exp $ -*- shell-script -*-
#
# Test whether $ZSH_VERSION (or some value of your choice, if a second argument
# is provided) is greater than or equal to x.y.z-r (in argument one). In fact,
# it'll accept any dot/dash-separated string of numbers as its second argument
# and compare it to the dot/dash-separated first argument. Leading non-number
# parts of a segment (such as the "zefram" in 3.1.2-zefram4) are not considered
# when the comparison is done; only the numbers matter. Any left-out segments
# in the first argument that are present in the version string compared are
# considered as zeroes, eg 3 == 3.0 == 3.0.0 == 3.0.0.0 and so on.
#
# Usage examples:
# is-at-least 3.1.6-15 && setopt NO_GLOBAL_RCS
# is-at-least 3.1.0 && setopt HIST_REDUCE_BLANKS
# is-at-least 586 $MACHTYPE && echo 'You could be running Mandrake!'

function is-at-least()
{
    emulate zsh ; setopt LOCAL_OPTIONS
    local IFS=".-" min_cnt=0 ver_cnt=0 part min_ver version
    min_ver=(${=1})
    version=(${=2:-$ZSH_VERSION} 0)
    while (( $min_cnt <= ${#min_ver} )) {
	while [[ "$part" != <-> ]] {
	    [[ $[++ver_cnt] > ${#version} ]] && return 0
	    part=${version[ver_cnt]##*[^0-9]}
	}
	[[ $[++min_cnt] > ${#min_ver} ]] && return 0
	(( part > min_ver[min_cnt] )) && return 0
	(( part < min_ver[min_cnt] )) && return 1
	part=''
    }
}


Some more usage examples, fetched from my ~/.zshrc:

if is-at-least 3.1.0 ; then
    setopt HIST_REDUCE_BLANKS	# on: Don't save extraneous between-word blanks in the history file

if is-at-least 3.1.2 ; then
  unsetopt KSH_AUTOLOAD		# off: Unfortunately, the zsh distribution forces me not to use this
				#      most excellent feature for now being. :-(
				# on: upon first execution of an autoloaded function, both perform
				#     its initialization code and the just defined function itself
    autoload ${^fpath}/*(.N:t)	# Autoload all functions (I wish I didn't have to)
if is-at-least 3.1.2-1 ; then
  unsetopt RM_STAR_WAIT		# off: I don't want to have to wait when removing files using globs
  unsetopt HIST_NO_FUNCTIONS	# off: I most certainly want function definitions in my history file
    setopt PROMPT_PERCENT	# on: % in prompts should expand the various prompt escapes
  unsetopt PROMPT_BANG		# off: %! is fine, thank you; I don't need an alias ! for that.
  unsetopt PRINT_EIGHT_BIT	# off: I have yet to encounter a system where my CTYPE isn't enough

if is-at-least 3.1.2-4 ; then
  unsetopt KSH_GLOB		# off: Use only zsh globbing; @*+?! get no special meaning before ()

if is-at-least 3.1.6 ; then
  unsetopt CHASE_DOTS		# off: don't resolve .. for the cd builtin looking at the filesystem
  unsetopt CHASE_LINKS		# off: don't resolve symlinks in $PWD when changing directory
#   setopt LOCAL_TRAPS		# does not make much sense outside of a function
# ZLS_COLOURS = ...

if is-at-least 3.1.6-6 ; then
    setopt LIST_PACKED		# on: shrink column widths in completion lists
  unsetopt LIST_ROWS_FIRST	# off: sort lists vertically in completion lists
fi # ZSH_VERSION >= 3.1.6-6
fi # ZSH_VERSION >= 3.1.6
fi # ZSH_VERSION >= 3.1.2-4
fi # ZSH_VERSION >= 3.1.2-1
fi # ZSH_VERSION >= 3.1.2
fi # ZSH_VERSION >= 3.1.0



Messages sorted by: Reverse Date, Date, Thread, Author