From cde50d4d9221c4f7ec3b0ac7c46e12205bc252e9 Mon Sep 17 00:00:00 2001 From: Bruno Sutic Date: Thu, 28 Aug 2014 00:43:31 +0200 Subject: [PATCH] Command strategies; restore vim sessions Closes #4 --- CHANGELOG.md | 1 + scripts/process_restore_helpers.sh | 49 ++++++++++++++++++++++++++++-- scripts/session_restorer.sh | 6 ++-- strategies/vim_session.sh | 23 ++++++++++++++ 4 files changed, 74 insertions(+), 5 deletions(-) create mode 100755 strategies/vim_session.sh diff --git a/CHANGELOG.md b/CHANGELOG.md index f631d69..3535505 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - user option for disabling pane process restoring - enable whitelisting processes that will be restored - expand readme with configuration options +- enable command strategies; enable restoring vim sessions ### v0.0.4, 2014-08-26 - restore pane layout for each window diff --git a/scripts/process_restore_helpers.sh b/scripts/process_restore_helpers.sh index ac9f8c2..bc72ffa 100644 --- a/scripts/process_restore_helpers.sh +++ b/scripts/process_restore_helpers.sh @@ -11,9 +11,13 @@ default_proc_list="vim emacs man less more tail top htop irssi" restore_processes_option="@session-saver-processes" restore_processes="" +# Defines part of the user variable. Example usage: +# set -g @session-saver-strategy-vim "session" +restore_process_strategy_option="@session-saver-strategy-" + restore_pane_processes_enabled() { local restore_processes="$(get_tmux_option "$restore_processes_option" "$restore_processes")" - if [ $restore_processes == "false" ]; then + if [ "$restore_processes" == "false" ]; then return 1 else return 0 @@ -25,13 +29,26 @@ restore_pane_process() { local session_name="$2" local window_number="$3" local pane_index="$4" + local dir="$5" if _process_should_be_restored "$pane_full_command"; then tmux switch-client -t "${session_name}:${window_number}" tmux select-pane -t "$pane_index" - tmux send-keys "$pane_full_command" "C-m" + + if _strategy_exists "$pane_full_command"; then + local strategy_file="$(_get_strategy_file "$pane_full_command")" + local strategy_command="$($strategy_file "$pane_full_command" "$dir")" + tmux send-keys "$strategy_command" "C-m" + # tmux send-keys "Strategy! $pane_full_command $strategy_file" + # tmux send-keys "Strategy! $strategy_command" + else + # just invoke the command + tmux send-keys "$pane_full_command" "C-m" + fi fi } +# private functions below + _process_should_be_restored() { local pane_full_command="$1" if _restore_all_processes; then @@ -74,3 +91,31 @@ _restore_list() { echo "$default_processes $user_processes" fi } + +_strategy_exists() { + local pane_full_command="$1" + local strategy="$(_get_command_strategy "$pane_full_command")" + if [ -n "$strategy" ]; then # strategy set? + local strategy_file="$(_get_strategy_file "$pane_full_command")" + [ -e "$strategy_file" ] # strategy file exists? + else + return 1 + fi +} + +_get_command_strategy() { + local pane_full_command="$1" + local command="$(_just_command "$pane_full_command")" + get_tmux_option "${restore_process_strategy_option}${command}" "" +} + +_just_command() { + echo "$1" | cut -d' ' -f1 +} + +_get_strategy_file() { + local pane_full_command="$1" + local strategy="$(_get_command_strategy "$pane_full_command")" + local command="$(_just_command "$pane_full_command")" + echo "$CURRENT_DIR/../strategies/${command}_${strategy}.sh" +} diff --git a/scripts/session_restorer.sh b/scripts/session_restorer.sh index 502f1ee..8e6ba90 100755 --- a/scripts/session_restorer.sh +++ b/scripts/session_restorer.sh @@ -109,10 +109,10 @@ restore_all_sessions() { restore_all_pane_processes() { if restore_pane_processes_enabled; then local pane_full_command - awk 'BEGIN { FS="\t"; OFS="\t" } /^pane/ && $11 !~ "^:$" { print $2, $3, $7, $11; }' $(last_session_path) | - while IFS=$'\t' read session_name window_number pane_index pane_full_command; do + awk 'BEGIN { FS="\t"; OFS="\t" } /^pane/ && $11 !~ "^:$" { print $2, $3, $7, $8, $11; }' $(last_session_path) | + while IFS=$'\t' read session_name window_number pane_index dir pane_full_command; do pane_full_command="$(remove_first_char "$pane_full_command")" - restore_pane_process "$pane_full_command" "$session_name" "$window_number" "$pane_index" + restore_pane_process "$pane_full_command" "$session_name" "$window_number" "$pane_index" "$dir" done fi } diff --git a/strategies/vim_session.sh b/strategies/vim_session.sh new file mode 100755 index 0000000..83912b6 --- /dev/null +++ b/strategies/vim_session.sh @@ -0,0 +1,23 @@ +#!/usr/bin/env bash + +# "vim session strategy" +# +# Restores a vim session from 'Session.vim' file, if it exists. +# If 'Session.vim' does not exist, it falls back to invoking the original +# command. + +ORIGINAL_COMMAND="$1" +DIRECTORY="$2" + +vim_session_file_exists() { + [ -e "${DIRECTORY}/Session.vim" ] +} + +main() { + if vim_session_file_exists; then + echo "vim -S" + else + echo "$ORIGINAL_COMMAND" + fi +} +main