From 83683552403031d3a1b307f9449c7155e9037c1d Mon Sep 17 00:00:00 2001 From: Bruno Sutic Date: Mon, 1 Sep 2014 19:41:33 +0200 Subject: [PATCH] Enable inline strategies when restoring programs --- CHANGELOG.md | 1 + README.md | 5 +++ scripts/process_restore_helpers.sh | 63 ++++++++++++++++++++++++------ scripts/variables.sh | 2 + 4 files changed, 59 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f6f4ba..73e1813 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Changelog ### master +- new feature: inline strategies when restoring a program ### v1.1.0, 2014-08-31 - bugfix: sourcing `variables.sh` file in save script diff --git a/README.md b/README.md index 3044536..0c59034 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,11 @@ Open a GitHub issue if you think some other program should be on the default lis set -g @resurrect-processes 'irb pry "~rails server" "~rails console"' +- If the wrong command is restored, try specifying inline strategy for the + program with `->`: + + set -g @resurrect-processes 'some_program "grunt->grunt development"' + - Don't restore any programs: set -g @resurrect-processes 'false' diff --git a/scripts/process_restore_helpers.sh b/scripts/process_restore_helpers.sh index 7681a0d..f849d30 100644 --- a/scripts/process_restore_helpers.sh +++ b/scripts/process_restore_helpers.sh @@ -17,7 +17,11 @@ restore_pane_process() { tmux switch-client -t "${session_name}:${window_number}" tmux select-pane -t "$pane_index" - if _strategy_exists "$pane_full_command"; then + local inline_strategy="$(_get_inline_strategy "$pane_full_command")" # might not be defined + if [ -n "$inline_strategy" ]; then + # inline strategy exists + tmux send-keys "$inline_strategy" "C-m" + elif _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" @@ -62,23 +66,42 @@ _process_on_the_restore_list() { # TODO: make this work without eval eval set $(_restore_list) local proc + local match 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 + match="$(_get_proc_match_element "$proc")" + if _proc_matches_full_command "$pane_full_command" "$match"; then + return 0 fi done return 1 } +_proc_matches_full_command() { + local pane_full_command="$1" + local match="$2" + if _proc_starts_with_tildae "$match"; then + match="$(remove_first_char "$match")" + # regex matching the command makes sure `$match` string is somewhere in the command string + if [[ "$pane_full_command" =~ ($match) ]]; then + return 0 + fi + else + # regex matching the command makes sure process is a "word" + if [[ "$pane_full_command" =~ (^${match} ) ]] || [[ "$pane_full_command" =~ (^${match}$) ]]; then + return 0 + fi + fi + return 1 +} + +_get_proc_match_element() { + echo "$1" | sed "s/${inline_strategy_token}.*//" +} + +_get_proc_restore_element() { + echo "$1" | sed "s/.*${inline_strategy_token}//" +} + _restore_list() { local user_processes="$(get_tmux_option "$restore_processes_option" "$restore_processes")" local default_processes="$(get_tmux_option "$default_proc_list_option" "$default_proc_list")" @@ -94,6 +117,22 @@ _proc_starts_with_tildae() { [[ "$1" =~ (^~) ]] } +_get_inline_strategy() { + local pane_full_command="$1" + # TODO: make this work without eval + eval set $(_restore_list) + local proc + local match + for proc in "$@"; do + if [[ "$proc" =~ "$inline_strategy_token" ]]; then + match="$(_get_proc_match_element "$proc")" + if _proc_matches_full_command "$pane_full_command" "$match"; then + echo "$(_get_proc_restore_element "$proc")" + fi + fi + done +} + _strategy_exists() { local pane_full_command="$1" local strategy="$(_get_command_strategy "$pane_full_command")" diff --git a/scripts/variables.sh b/scripts/variables.sh index 1759fad..5945986 100644 --- a/scripts/variables.sh +++ b/scripts/variables.sh @@ -21,3 +21,5 @@ restore_processes="" # Defines part of the user variable. Example usage: # set -g @resurrect-strategy-vim "session" restore_process_strategy_option="@resurrect-strategy-" + +inline_strategy_token="->"