diff --git a/check-version.sh b/check-version.sh new file mode 100755 index 0000000..d69a23e --- /dev/null +++ b/check-version.sh @@ -0,0 +1,96 @@ +#!/usr/bin/env bash + +# Compare the current tmux version against a given version. +# Exit: 0 if comparison is true, 1 if false + +set -euo pipefail + +usage() { + echo "usage: $0 " >&2 + echo "operators: < <= == >= >" >&2 +} + +if [[ ${1:-} == "-h" || ${1:-} == "--help" ]]; then + usage + exit 0 +fi + +if [[ $# -ne 2 ]]; then + usage + exit 1 +fi + +operator="$1" +target_version="$2" + +# Extract tmux version (e.g., "tmux 3.3a" -> "3.3a") +current_version=$(tmux -V | sed 's/^tmux //') + +# Compare two version strings +# Returns: -1 if v1 < v2, 0 if v1 == v2, 1 if v1 > v2 +compare_versions() { + local v1="$1" + local v2="$2" + + # Strip trailing letters (e.g., "3.3a" -> "3.3") and save them + local v1_suffix="${v1##*[0-9]}" + local v2_suffix="${v2##*[0-9]}" + v1="${v1%[a-zA-Z]*}" + v2="${v2%[a-zA-Z]*}" + + # Split into components + IFS='.' read -ra v1_parts <<< "$v1" + IFS='.' read -ra v2_parts <<< "$v2" + + # Compare numeric parts + local max_len=$(( ${#v1_parts[@]} > ${#v2_parts[@]} ? ${#v1_parts[@]} : ${#v2_parts[@]} )) + for ((i = 0; i < max_len; i++)); do + local p1="${v1_parts[i]:-0}" + local p2="${v2_parts[i]:-0}" + if ((p1 < p2)); then + echo -1 + return + elif ((p1 > p2)); then + echo 1 + return + fi + done + + # Numeric parts equal, compare suffixes (no suffix < a < b < ...) + # A letter suffix indicates a patch release, so 3.6a > 3.6 + if [[ -z "$v1_suffix" && -n "$v2_suffix" ]]; then + echo -1 + return + elif [[ -n "$v1_suffix" && -z "$v2_suffix" ]]; then + echo 1 + return + elif [[ "$v1_suffix" < "$v2_suffix" ]]; then + echo -1 + return + elif [[ "$v1_suffix" > "$v2_suffix" ]]; then + echo 1 + return + fi + + echo 0 +} + +result=$(compare_versions "$current_version" "$target_version") + +case "$operator" in + '<') + [[ "$result" -eq -1 ]] && exit 0 || exit 1 ;; + '<=') + [[ "$result" -le 0 ]] && exit 0 || exit 1 ;; + '==') + [[ "$result" -eq 0 ]] && exit 0 || exit 1 ;; + '>=') + [[ "$result" -ge 0 ]] && exit 0 || exit 1 ;; + '>') + [[ "$result" -eq 1 ]] && exit 0 || exit 1 ;; + *) + echo "error: invalid operator: $operator" >&2 + usage + exit 1 + ;; +esac diff --git a/tmux.conf b/tmux.conf index bb04aeb..11c7fac 100644 --- a/tmux.conf +++ b/tmux.conf @@ -28,7 +28,7 @@ set -g status-interval 2 set -g default-terminal "tmux-256color" # Enable true-color -if '[[ "$TMUX_VERSION" < "3.2" ]]' \ +if '~/.config/tmux/check-version.sh "<" 3.2' \ 'set-option -as terminal-features ",xterm*:Tc"' \ 'set-option -as terminal-features ",xterm*:RGB"' @@ -49,7 +49,7 @@ unbind -T copy-mode-vi MouseDragEnd1Pane # Enable changing cursor shape per pane, vim or zsh should emit escape # sequences to change cursor shape. # iTerm2 only requires this before tmux 2.9 -if '[ -n $ITERM_PROFILE ] && [[ "$TMUX_VERSION" < "2.9" ]]' \ +if '[ -n $ITERM_PROFILE ] && ~/.config/tmux/check-version.sh "<" 2.9' \ 'set -ga terminal-overrides "*:Ss=\E]1337;CursorShape=%p1%d\7"' # VTE compatible terminals. if '[ ! -n $ITERM_PROFILE ]' \ @@ -66,7 +66,7 @@ bind a run-shell ~/.local/share/tmux/layouts/window-auto if -b '[ "`uname`" = "Darwin" ]' \ 'set -g default-command "reattach-to-user-namespace -l $SHELL"' -if -b '[[ "$TMUX_VERSION" > "3.2" ]]' { +if -b '~/.config/tmux/check-version.sh ">" 3.2' { # Open a popup with running the default shell bind T display-popup -S fg=#54546D -b rounded -d '#{pane_current_path}' -E $SHELL # Open a popup with session creator/switcher