2019-09-07 03:05:19 +02:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# bat-extras | Copyright (C) 2019 eth-p | MIT License
|
|
|
|
#
|
|
|
|
# Repository: https://github.com/eth-p/bat-extras
|
|
|
|
# Issues: https://github.com/eth-p/bat-extras/issues
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
# Gets the current bat version.
|
|
|
|
bat_version() {
|
2019-09-09 02:43:29 +02:00
|
|
|
"$BAT" --version | cut -d ' ' -f 2
|
2019-09-07 03:05:19 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
# Compares two version strings.
|
|
|
|
# Arguments:
|
2019-09-27 00:37:11 +02:00
|
|
|
# 1 -- The version to compare.
|
|
|
|
# 2 -- The comparison operator (same as []).
|
|
|
|
# 3 -- The version to compare with.
|
2019-09-07 03:05:19 +02:00
|
|
|
version_compare() {
|
|
|
|
local version="$1"
|
|
|
|
local compare="$3"
|
|
|
|
|
|
|
|
if ! [[ "$version" =~ \.$ ]]; then
|
|
|
|
version="${version}."
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! [[ "$compare" =~ \.$ ]]; then
|
|
|
|
compare="${compare}."
|
|
|
|
fi
|
|
|
|
|
|
|
|
version_compare__recurse "$version" "$2" "$compare"
|
|
|
|
return $?
|
|
|
|
}
|
|
|
|
|
|
|
|
version_compare__recurse() {
|
|
|
|
local version="$1"
|
|
|
|
local operator="$2"
|
|
|
|
local compare="$3"
|
|
|
|
|
|
|
|
# Extract the leading number.
|
|
|
|
local v_major="${version%%.*}"
|
|
|
|
local c_major="${compare%%.*}"
|
|
|
|
|
|
|
|
# Extract the remaining numbers.
|
|
|
|
local v_minor="${version#*.}"
|
|
|
|
local c_minor="${compare#*.}"
|
|
|
|
|
|
|
|
# Compare the versions specially if the final number has been reached.
|
|
|
|
if [[ -z "$v_minor" && -z "$c_minor" ]]; then
|
|
|
|
[ "$v_major" $operator "$c_major" ];
|
2020-03-31 23:51:00 +02:00
|
|
|
return $?
|
2019-09-07 03:05:19 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Insert zeroes where there are missing numbers.
|
|
|
|
if [[ -z "$v_minor" ]]; then
|
|
|
|
v_minor="0."
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -z "$c_minor" ]]; then
|
|
|
|
c_minor="0."
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Compare the versions.
|
|
|
|
# This is an early escape case.
|
|
|
|
case "$operator" in
|
2020-04-01 21:47:21 +02:00
|
|
|
-eq) [[ "$v_major" -ne "$c_major" ]] && return 1 ;;
|
|
|
|
-ne) [[ "$v_major" -ne "$c_major" ]] && return 0 ;;
|
|
|
|
-ge | -gt) [[ "$v_major" -lt "$c_major" ]] && return 1
|
|
|
|
[[ "$v_major" -gt "$c_major" ]] && return 0 ;;
|
|
|
|
-le | -lt) [[ "$v_major" -gt "$c_major" ]] && return 1
|
|
|
|
[[ "$v_major" -lt "$c_major" ]] && return 0 ;;
|
2019-09-07 03:05:19 +02:00
|
|
|
esac
|
|
|
|
|
|
|
|
version_compare__recurse "$v_minor" "$operator" "$c_minor"
|
|
|
|
}
|