|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +VERSION="$1" |
| 4 | +UNSUPPORTED_MSG="$2" |
| 5 | + |
| 6 | +get_tmux_option() { |
| 7 | + local option=$1 |
| 8 | + local default_value=$2 |
| 9 | + local option_value=$(tmux show-option -gqv "$option") |
| 10 | + if [ -z "$option_value" ]; then |
| 11 | + echo "$default_value" |
| 12 | + else |
| 13 | + echo "$option_value" |
| 14 | + fi |
| 15 | +} |
| 16 | + |
| 17 | +# Ensures a message is displayed for 5 seconds in tmux prompt. |
| 18 | +# Does not override the 'display-time' tmux option. |
| 19 | +display_message() { |
| 20 | + local message="$1" |
| 21 | + |
| 22 | + # display_duration defaults to 5 seconds, if not passed as an argument |
| 23 | + if [ "$#" -eq 2 ]; then |
| 24 | + local display_duration="$2" |
| 25 | + else |
| 26 | + local display_duration="5000" |
| 27 | + fi |
| 28 | + |
| 29 | + # saves user-set 'display-time' option |
| 30 | + local saved_display_time=$(get_tmux_option "display-time" "750") |
| 31 | + |
| 32 | + # sets message display time to 5 seconds |
| 33 | + tmux set-option -gq display-time "$display_duration" |
| 34 | + |
| 35 | + # displays message |
| 36 | + tmux display-message "$message" |
| 37 | + |
| 38 | + # restores original 'display-time' value |
| 39 | + tmux set-option -gq display-time "$saved_display_time" |
| 40 | +} |
| 41 | + |
| 42 | +# this is used to get "clean" integer version number. Examples: |
| 43 | +# `tmux 1.9` => `19` |
| 44 | +# `1.9a` => `19` |
| 45 | +get_digits_from_string() { |
| 46 | + local string="$1" |
| 47 | + local only_digits="$(echo "$string" | tr -dC '[:digit:]')" |
| 48 | + echo "$only_digits" |
| 49 | +} |
| 50 | + |
| 51 | +tmux_version_int() { |
| 52 | + local tmux_version_string=$(tmux -V) |
| 53 | + echo "$(get_digits_from_string "$tmux_version_string")" |
| 54 | +} |
| 55 | + |
| 56 | +unsupported_version_message() { |
| 57 | + if [ -n "$UNSUPPORTED_MSG" ]; then |
| 58 | + echo "$UNSUPPORTED_MSG" |
| 59 | + else |
| 60 | + echo "Error, Tmux version unsupported! Please install Tmux version $VERSION or greater!" |
| 61 | + fi |
| 62 | +} |
| 63 | + |
| 64 | +exit_if_unsupported_version() { |
| 65 | + local current_version="$1" |
| 66 | + local supported_version="$2" |
| 67 | + if [ "$current_version" -lt "$supported_version" ]; then |
| 68 | + display_message "$(unsupported_version_message)" |
| 69 | + exit 1 |
| 70 | + fi |
| 71 | +} |
| 72 | + |
| 73 | +main() { |
| 74 | + local supported_version_int="$(get_digits_from_string "$VERSION")" |
| 75 | + local current_version_int="$(tmux_version_int)" |
| 76 | + exit_if_unsupported_version "$current_version_int" "$supported_version_int" |
| 77 | +} |
| 78 | +main |
0 commit comments