Enable inline strategies when restoring programs

pull/54/head
Bruno Sutic 2014-09-01 19:41:33 +02:00
parent 20c5fc40cc
commit 8368355240
No known key found for this signature in database
GPG Key ID: 66D96E4F2F7EF26C
4 changed files with 59 additions and 12 deletions

View File

@ -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

View File

@ -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'

View File

@ -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")"

View File

@ -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="->"