11 Commits

12 changed files with 165 additions and 44 deletions

View File

@ -2,6 +2,17 @@
### master
### v0.2.0, 2014-08-29
- bugfix: with vim 'session' strategy, if the session file does not exist - make
sure vim does not contain `-S` flag
- enable restoring programs with arguments (e.g. "rails console") and also
processes that contain program name
- improve `irb` restore strategy
### v0.1.0, 2014-08-28
- refactor checking if saved tmux session exists
- spinner while tmux sessions are restored
### v0.0.5, 2014-08-28
- restore pane processes
- user option for disabling pane process restoring

12
CONTRIBUTING.md Normal file
View File

@ -0,0 +1,12 @@
### Contributing
Code contributions are welcome!
### Reporting a bug
If you find a bug please report it in the issues. When reporting a bug please
attach:
- a file symlinked to `~/.tmux/sessions/last`.
- your `.tmux.conf`
- if you're getting an error paste it to a [gist](https://gist.github.com/) and
link it in the issue

View File

@ -66,13 +66,21 @@ You should now be able to use the plugin.
### Configuration
By default, only a conservative list of programs is restored:
`vim emacs man less more tail top htop irssi`.
Only a conservative list of programs is restored by default:
`vi vim emacs man less more tail top htop irssi irb pry "~rails console"`.
Open a github issue if you think some other program should be on the default list.
- Restore additional programs by putting the following in `.tmux.conf`:
- Restore additional programs with the setting in `.tmux.conf`:
set -g @session-saver-processes 'ssh telnet myprogram'
set -g @session-saver-processes 'ssh psql mysql sqlite3'
- Programs with arguments should be double quoted:
set -g @session-saver-processes 'some_program "git log"'
- Start with tilde to restore a program whose process contains target name:
set -g @session-saver-processes 'some_program "~rails server"'
- Don't restore any programs:
@ -94,10 +102,8 @@ present.
### Reporting bugs and contributing
Code contributions are welcome!
If you find a bug please report it in the issues. When reporting a bug please
attach a file symlinked to `~/.tmux/sessions/last`.
Both contributing and bug reports are welcome. Please check out
[contributing guidelines](CONTRIBUTING.md).
### Credits

View File

@ -46,6 +46,10 @@ supported_tmux_version_ok() {
$CURRENT_DIR/check_tmux_version.sh "$SUPPORTED_VERSION"
}
remove_first_char() {
echo "$1" | cut -c2-
}
# path helpers
sessions_dir() {

View File

@ -1,20 +1,3 @@
# default processes that are restored
default_proc_list_option="@session-saver-default-processes"
default_proc_list="vim emacs man less more tail top htop irssi"
# User defined processes that are restored
# 'false' - nothing is restored
# ':all:' - all processes are restored
#
# user defined list of programs that are restored:
# 'my_program foo another_program'
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
@ -71,12 +54,22 @@ _restore_all_processes() {
_process_on_the_restore_list() {
local pane_full_command="$1"
local restore_list="$(_restore_list)"
# TODO: make this work without eval
eval set $(_restore_list)
local proc
for proc in $restore_list; do
if [[ "$pane_full_command" =~ (^$proc) ]]; then
for proc in "$@"; do
if _proc_starts_with_tildae "$proc"; then
proc="$(remove_first_char "$proc")"
# regex matching the command makes sure `$proc` string is somewhere the command string
if [[ "$pane_full_command" =~ ($proc) ]]; then
return 0
fi
else
# regex matching the command makes sure process is a "word"
if [[ "$pane_full_command" =~ (^${proc} ) ]] || [[ "$pane_full_command" =~ (^${proc}$) ]]; then
return 0
fi
fi
done
return 1
}
@ -92,6 +85,10 @@ _restore_list() {
fi
}
_proc_starts_with_tildae() {
[[ "$1" =~ (^~) ]]
}
_strategy_exists() {
local pane_full_command="$1"
local strategy="$(_get_command_strategy "$pane_full_command")"

View File

@ -2,8 +2,10 @@
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/variables.sh"
source "$CURRENT_DIR/helpers.sh"
source "$CURRENT_DIR/process_restore_helpers.sh"
source "$CURRENT_DIR/spinner_helpers.sh"
is_line_type() {
local line_type="$1"
@ -15,8 +17,8 @@ is_line_type() {
check_saved_session_exists() {
local saved_session="$(last_session_path)"
if [ ! -f $saved_session ]; then
display_message "Saved session not found!"
exit
display_message "Saved tmux session not found!"
return 1
fi
}
@ -40,10 +42,6 @@ tmux_socket() {
echo $TMUX | cut -d',' -f1
}
remove_first_char() {
echo "$1" | cut -c2-
}
new_window() {
local session_name="$1"
local window_number="$2"
@ -149,8 +147,8 @@ restore_active_and_alternate_sessions() {
}
main() {
if supported_tmux_version_ok; then
check_saved_session_exists
if supported_tmux_version_ok && check_saved_session_exists; then
start_spinner
restore_all_sessions
restore_all_pane_processes
restore_pane_layout_for_each_window >/dev/null 2>&1
@ -158,6 +156,7 @@ main() {
restore_active_pane_for_each_window
restore_active_and_alternate_windows
restore_active_and_alternate_sessions
stop_spinner
display_message "Restored all Tmux sessions!"
fi
}

View File

@ -0,0 +1,8 @@
start_spinner() {
$CURRENT_DIR/tmux_spinner.sh "Restoring sessions..." "Restored all Tmux sessions!" &
export SPINNER_PID=$!
}
stop_spinner() {
kill $SPINNER_PID
}

29
scripts/tmux_spinner.sh Executable file
View File

@ -0,0 +1,29 @@
#!/usr/bin/env bash
# This script shows tmux spinner with a message. It is intended to be running
# as a background process which should be `kill`ed at the end.
#
# Example usage:
#
# ./tmux_spinner.sh "Working..." "End message!" &
# SPINNER_PID=$!
# ..
# .. execute commands here
# ..
# kill $SPINNER_PID # Stops spinner and displays 'End message!'
MESSAGE="$1"
END_MESSAGE="$2"
SPIN='-\|/'
trap "tmux display-message $END_MESSAGE; exit" SIGINT SIGTERM
main() {
local i=0
while true; do
i=$(( (i+1) %4 ))
tmux display-message " ${SPIN:$i:1} $MESSAGE"
sleep 0.1
done
}
main

23
scripts/variables.sh Normal file
View File

@ -0,0 +1,23 @@
# key bindings
default_save_key="M-s"
save_option="@session-saver-save"
default_restore_key="M-r"
restore_option="@session-saver-restore"
# default processes that are restored
default_proc_list_option="@session-saver-default-processes"
default_proc_list='vi vim emacs man less more tail top htop irssi irb pry "~rails console"'
# User defined processes that are restored
# 'false' - nothing is restored
# ':all:' - all processes are restored
#
# user defined list of programs that are restored:
# 'my_program foo another_program'
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-"

View File

@ -2,14 +2,9 @@
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/scripts/variables.sh"
source "$CURRENT_DIR/scripts/helpers.sh"
default_save_key="M-s"
save_option="@session-saver-save"
default_restore_key="M-r"
restore_option="@session-saver-restore"
set_save_bindings() {
local key_bindings=$(get_tmux_option "$save_option" "$default_save_key")
local key
@ -26,8 +21,13 @@ set_restore_bindings() {
done
}
set_default_strategies() {
tmux set-option -g "${restore_process_strategy_option}irb" "default_strategy"
}
main() {
set_save_bindings
set_restore_bindings
set_default_strategies
}
main

View File

@ -0,0 +1,23 @@
#!/usr/bin/env bash
# "irb default strategy"
#
# Example irb process with junk variables:
# irb RBENV_VERSION=1.9.3-p429 GREP_COLOR=34;47 TERM_PROGRAM=Apple_Terminal
#
# When executed, the above will fail. This strategy handles that.
ORIGINAL_COMMAND="$1"
DIRECTORY="$2"
original_command_wo_junk_vars() {
echo "$ORIGINAL_COMMAND" |
sed 's/RBENV_VERSION[^ ]*//' |
sed 's/GREP_COLOR[^ ]*//' |
sed 's/TERM_PROGRAM[^ ]*//'
}
main() {
echo "$(original_command_wo_junk_vars)"
}
main

View File

@ -4,7 +4,7 @@
#
# 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.
# command (withouth the `-S` flag).
ORIGINAL_COMMAND="$1"
DIRECTORY="$2"
@ -13,9 +13,18 @@ vim_session_file_exists() {
[ -e "${DIRECTORY}/Session.vim" ]
}
original_command_contains_session_flag() {
[[ "$ORIGINAL_COMMAND" =~ "-S" ]]
}
main() {
if vim_session_file_exists; then
echo "vim -S"
elif original_command_contains_session_flag; then
# Session file does not exist, yet the orignal vim command contains
# session flag `-S`. This will cause an error, so we're falling back to
# starting plain vim.
echo "vim"
else
echo "$ORIGINAL_COMMAND"
fi