From dc6da8fdf8208e3b4dc45490e2d90b518beb84ac Mon Sep 17 00:00:00 2001 From: Bruno Sutic Date: Sat, 14 Mar 2015 22:12:53 +0100 Subject: [PATCH] Require tmux 1.9 to use the plugin --- CHANGELOG.md | 1 + logging.tmux | 2 +- scripts/check_tmux_version.sh | 78 ++++++++++++++++++++++++++++++++ scripts/shared.sh | 4 ++ scripts/tmux_pane_screenshot.sh | 9 ++-- scripts/tmux_proper_pipe_pane.sh | 7 ++- scripts/tmux_scrollback_clear.sh | 9 ++-- scripts/tmux_scrollback_dump.sh | 9 ++-- scripts/variables.sh | 4 ++ 9 files changed, 111 insertions(+), 12 deletions(-) create mode 100755 scripts/check_tmux_version.sh diff --git a/CHANGELOG.md b/CHANGELOG.md index b86a117..a7eb185 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### master - update readme - extract default keys and key options to `variables.sh` file +- require tmux version 1.9 or greater to use the plugin ### v1.0.0, 2014-11-09 - improve `capture-pane`. It is now piped directly to the target file. diff --git a/logging.tmux b/logging.tmux index a0bbfef..07769b9 100755 --- a/logging.tmux +++ b/logging.tmux @@ -2,8 +2,8 @@ CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -source "$CURRENT_DIR/scripts/shared.sh" source "$CURRENT_DIR/scripts/variables.sh" +source "$CURRENT_DIR/scripts/shared.sh" setup_pipe_pane_key_binding() { local key=$(get_tmux_option "$pipe_pane_key_option" "$default_pipe_pane_key") diff --git a/scripts/check_tmux_version.sh b/scripts/check_tmux_version.sh new file mode 100755 index 0000000..b0aedec --- /dev/null +++ b/scripts/check_tmux_version.sh @@ -0,0 +1,78 @@ +#!/usr/bin/env bash + +VERSION="$1" +UNSUPPORTED_MSG="$2" + +get_tmux_option() { + local option=$1 + local default_value=$2 + local option_value=$(tmux show-option -gqv "$option") + if [ -z "$option_value" ]; then + echo "$default_value" + else + echo "$option_value" + fi +} + +# Ensures a message is displayed for 5 seconds in tmux prompt. +# Does not override the 'display-time' tmux option. +display_message() { + local message="$1" + + # display_duration defaults to 5 seconds, if not passed as an argument + if [ "$#" -eq 2 ]; then + local display_duration="$2" + else + local display_duration="5000" + fi + + # saves user-set 'display-time' option + local saved_display_time=$(get_tmux_option "display-time" "750") + + # sets message display time to 5 seconds + tmux set-option -gq display-time "$display_duration" + + # displays message + tmux display-message "$message" + + # restores original 'display-time' value + tmux set-option -gq display-time "$saved_display_time" +} + +# this is used to get "clean" integer version number. Examples: +# `tmux 1.9` => `19` +# `1.9a` => `19` +get_digits_from_string() { + local string="$1" + local only_digits="$(echo "$string" | tr -dC '[:digit:]')" + echo "$only_digits" +} + +tmux_version_int() { + local tmux_version_string=$(tmux -V) + echo "$(get_digits_from_string "$tmux_version_string")" +} + +unsupported_version_message() { + if [ -n "$UNSUPPORTED_MSG" ]; then + echo "$UNSUPPORTED_MSG" + else + echo "Error, Tmux version unsupported! Please install Tmux version $VERSION or greater!" + fi +} + +exit_if_unsupported_version() { + local current_version="$1" + local supported_version="$2" + if [ "$current_version" -lt "$supported_version" ]; then + display_message "$(unsupported_version_message)" + exit 1 + fi +} + +main() { + local supported_version_int="$(get_digits_from_string "$VERSION")" + local current_version_int="$(tmux_version_int)" + exit_if_unsupported_version "$current_version_int" "$supported_version_int" +} +main diff --git a/scripts/shared.sh b/scripts/shared.sh index bdf0347..90247a8 100644 --- a/scripts/shared.sh +++ b/scripts/shared.sh @@ -39,3 +39,7 @@ remove_empty_lines_from_end_of_file() { local temp=$(cat $file) printf '%s\n' "$temp" > "$file" } + +supported_tmux_version_ok() { + $CURRENT_DIR/check_tmux_version.sh "$SUPPORTED_VERSION" +} diff --git a/scripts/tmux_pane_screenshot.sh b/scripts/tmux_pane_screenshot.sh index 5393095..603f582 100755 --- a/scripts/tmux_pane_screenshot.sh +++ b/scripts/tmux_pane_screenshot.sh @@ -9,10 +9,13 @@ default_name="tmux-screen-capture-#{session_name}-#{window_index}-#{pane_index}- path_option="@screenshot_path" name_option="@screenshot_filename" -source $CURRENT_DIR/shared.sh -source $CURRENT_DIR/capture_pane_helpers.sh +source "$CURRENT_DIR/variables.sh" +source "$CURRENT_DIR/shared.sh" +source "$CURRENT_DIR/capture_pane_helpers.sh" main() { - capture_pane "Screen capture" + if supported_tmux_version_ok; then + capture_pane "Screen capture" + fi } main diff --git a/scripts/tmux_proper_pipe_pane.sh b/scripts/tmux_proper_pipe_pane.sh index 3ad467b..096fdb8 100755 --- a/scripts/tmux_proper_pipe_pane.sh +++ b/scripts/tmux_proper_pipe_pane.sh @@ -8,7 +8,8 @@ default_log_name="tmux-#{session_name}-#{window_index}-#{pane_index}-%Y%m%dT%H%M log_path_option="@pipe_pane_path" log_name_option="@pipe_pane_filename" -source $CURRENT_DIR/shared.sh +source "$CURRENT_DIR/variables.sh" +source "$CURRENT_DIR/shared.sh" get_log_path() { get_tmux_option "$log_path_option" "$default_log_path" @@ -65,6 +66,8 @@ toggle_pipe_pane() { } main() { - toggle_pipe_pane + if supported_tmux_version_ok; then + toggle_pipe_pane + fi } main diff --git a/scripts/tmux_scrollback_clear.sh b/scripts/tmux_scrollback_clear.sh index 08f0da1..4406794 100755 --- a/scripts/tmux_scrollback_clear.sh +++ b/scripts/tmux_scrollback_clear.sh @@ -2,10 +2,13 @@ CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -source $CURRENT_DIR/shared.sh +source "$CURRENT_DIR/variables.sh" +source "$CURRENT_DIR/shared.sh" main() { - tmux clear-history - display_message "Pane history cleared!" + if supported_tmux_version_ok; then + tmux clear-history + display_message "Pane history cleared!" + fi } main diff --git a/scripts/tmux_scrollback_dump.sh b/scripts/tmux_scrollback_dump.sh index ed15f85..5cdcaf2 100755 --- a/scripts/tmux_scrollback_dump.sh +++ b/scripts/tmux_scrollback_dump.sh @@ -9,10 +9,13 @@ default_name="tmux-history-#{session_name}-#{window_index}-#{pane_index}-%Y%m%dT path_option="@scrollback_dump_path" name_option="@scrollback_dump_filename" -source $CURRENT_DIR/shared.sh -source $CURRENT_DIR/capture_pane_helpers.sh +source "$CURRENT_DIR/variables.sh" +source "$CURRENT_DIR/shared.sh" +source "$CURRENT_DIR/capture_pane_helpers.sh" main() { - capture_pane "History" + if supported_tmux_version_ok; then + capture_pane "History" + fi } main diff --git a/scripts/variables.sh b/scripts/variables.sh index 0f1c259..9a5cad7 100644 --- a/scripts/variables.sh +++ b/scripts/variables.sh @@ -1,3 +1,7 @@ +SUPPORTED_VERSION="1.9" + +# Key binding options and defaults + pipe_pane_key_option="@pipe-pane-key" default_pipe_pane_key="P" # Shift-p