From cfe8e7979ba2f9505ecc8e1a217f94f81eed939f Mon Sep 17 00:00:00 2001 From: Bruno Sutic Date: Thu, 28 Aug 2014 23:39:53 +0200 Subject: [PATCH] Restoring programs with arguments; improve process matching Closes #20, closes #19 --- CHANGELOG.md | 4 +++- README.md | 12 ++++++++++-- scripts/helpers.sh | 4 ++++ scripts/process_restore_helpers.sh | 25 +++++++++++++++++++------ scripts/session_restorer.sh | 4 ---- 5 files changed, 36 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d619d7..a571af0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,9 @@ ### master - bugfix: with vim 'session' strategy, if the session file does not exist - make - sure wim does not contain `-S` flag + sure vim does not contain `-S` flag +- enable restoring programs with arguments (e.g. "rails console") and also + processes that contain program name ### v0.1.0, 2014-08-28 - refactor checking if saved tmux session exists diff --git a/README.md b/README.md index 0021b82..25e0718 100644 --- a/README.md +++ b/README.md @@ -67,13 +67,21 @@ You should now be able to use the plugin. ### Configuration Only a conservative list of programs is restored by default: -`vi vim emacs man less more tail top htop irssi irb pry`. +`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 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: set -g @session-saver-processes 'false' diff --git a/scripts/helpers.sh b/scripts/helpers.sh index 092ef6a..94ad539 100644 --- a/scripts/helpers.sh +++ b/scripts/helpers.sh @@ -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() { diff --git a/scripts/process_restore_helpers.sh b/scripts/process_restore_helpers.sh index 6e35b3f..4facfb3 100644 --- a/scripts/process_restore_helpers.sh +++ b/scripts/process_restore_helpers.sh @@ -1,6 +1,6 @@ # 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" +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 @@ -71,12 +71,21 @@ _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 - # regex matching the command makes sure process is a "word" - if [[ "$pane_full_command" =~ (^${proc} ) ]] || [[ "$pane_full_command" =~ (^${proc}$) ]]; then - return 0 + 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 @@ -93,6 +102,10 @@ _restore_list() { fi } +_proc_starts_with_tildae() { + [[ "$1" =~ (^~) ]] +} + _strategy_exists() { local pane_full_command="$1" local strategy="$(_get_command_strategy "$pane_full_command")" diff --git a/scripts/session_restorer.sh b/scripts/session_restorer.sh index 65bf0cb..d9409b3 100755 --- a/scripts/session_restorer.sh +++ b/scripts/session_restorer.sh @@ -41,10 +41,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"