mirror of
https://github.com/eth-p/bat-extras.git
synced 2024-12-12 17:20:40 +01:00
Updated batgrep to use the new bat 0.12 "snip" component
This commit is contained in:
parent
1c9eb2cb82
commit
b09f38190c
78
lib/version.sh
Normal file
78
lib/version.sh
Normal file
@ -0,0 +1,78 @@
|
||||
#!/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() {
|
||||
bat --version | cut -d ' ' -f 2
|
||||
return
|
||||
}
|
||||
|
||||
# Compares two version strings.
|
||||
# Arguments:
|
||||
# 0 -- The version to compare.
|
||||
# 1 -- The comparison operator (same as []).
|
||||
# 2 -- The version to compare with.
|
||||
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" ];
|
||||
return $?;
|
||||
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
|
||||
-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;;
|
||||
esac
|
||||
|
||||
version_compare__recurse "$v_minor" "$operator" "$c_minor"
|
||||
}
|
||||
|
||||
|
@ -8,6 +8,7 @@
|
||||
LIB="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/../lib"
|
||||
source "${LIB}/print.sh"
|
||||
source "${LIB}/opt.sh"
|
||||
source "${LIB}/version.sh"
|
||||
# -----------------------------------------------------------------------------
|
||||
SEP="$(printc "%{DIM}%$(tput cols)s%{CLEAR}" | sed "s/ /─/g")"
|
||||
RG_ARGS=()
|
||||
@ -17,6 +18,12 @@ FILES=()
|
||||
OPT_CONTEXT_BEFORE=2
|
||||
OPT_CONTEXT_AFTER=2
|
||||
OPT_FOLLOW=true
|
||||
BAT_STYLE="header,numbers"
|
||||
|
||||
# Set options based on the bat version.
|
||||
if version_compare "$(bat_version)" -gt "0.12"; then
|
||||
BAT_STYLE="${BAT_STYLE},snip"
|
||||
fi
|
||||
|
||||
# Parse arguments.
|
||||
while shiftopt; do
|
||||
@ -99,7 +106,7 @@ do_print() {
|
||||
bat "${BAT_ARGS[@]}" \
|
||||
"${LAST_LR[@]}" \
|
||||
"${LAST_LH[@]}" \
|
||||
--style="header,numbers" \
|
||||
--style="${BAT_STYLE}" \
|
||||
--paging=never \
|
||||
"$LAST_FILE"
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user